---
title: "GLM + GEE Models (Notes)"
subtitle: "Stat 452"
author: "Your Name"
format:
  html:
    toc: true
    toc-depth: 2
    embed-resources: true
---



# Small Group Activity {.unnumbered .smaller}

**Download a template Qmd file to start from [here](https://bcheggeseth.github.io/452_fall_2025/student_notes/L11-glm-gee-notes.qmd).**


## Introduction to ACTIVE study {.unnumbered .smaller}

For this section of class, you'll work on analyzing longitudinal data from the clinical trial, the Advanced Cognitive Training for Independent and Vital Elderly (ACTIVE) trial. 

[Video Summary of ACTIVE Trial](https://www.youtube.com/watch?v=Ql4T4JVWpo0)

To get access to the data, go to HW5 and click on the Github Classroom link. You will create an individual repository, rather than a team repository, for this homework. Place the template qmd file in that folder. Open it up.


```{r}
source('Cleaning.R') #Open Cleaning.R for information about variables.
head(activeLong)
```


### Explore the Data {.unnumbered .smaller}

1. The wide format data is named `active` and the long format data is named `activeLong`. Look at these two data sets and describe the difference between them.



```{r}
head(active)
head(activeLong)
```

2. The wide format data is useful for comparing the treatment groups, `INTGRP`, at baseline. Create a plot to compare the baseline cognitive function, `MMSE_1` between the randomized treatment groups, `INTGRP`. Describe what you observe and whether they match what you'd expect. 



```{r}

```

3. The long format data is useful to fit models and look at the relationship of variables over time. Create a plot to compare the overall `Memory` score across `Years` from the study baseline, grouping lines by the subject identifier, `AID`, and coloring them by treatment group, `INTGRP`. 



```{r}

```

4. Now, create a plot to compare the mean `Memory` score across `Years` from the study baseline by treatment group, `INTGRP`. You'll first need to summarize the data within groups prior to plotting.



```{r}

```

### Discuss Models {.unnumbered .smaller}

5. Consider modeling `Memory` as a function of `Years` and treatment group `INTGRP`. Discuss with people around you how you'd model that relationship using `lm()`. Make sure you think about the assumptions you are making about the relationship when you write a formula, Y~X, for `lm()`. 




6. Discuss with people around you the potential issues of using `lm()` for this data. What part of the output will be valid to interpret and which part of the output will not be valid to interpret? 




### Fit Models {.unnumbered .smaller}

7. Fit the model you discussed in 5 using `lm()`. Comment on the output of interest (based on your discussion above). 


```{r}
activeLong <- activeLong %>% mutate(INTGRP = relevel(INTGRP, ref='Control'))

lm(??, data = activeLong) %>% summary()
```


8. Fit the model you discussed in 5 using `geeM::geem()`. You'll need to run `install.packages('geeM')`. Compare the Std. Error from `lm()` with the Model SE and Robust SE when using a independent working correlation structure. 



```{r}
library(geeM)
library(tidyr)

activeLong %>% drop_na(Memory,Years,INTGRP) %>% 
  geem(??, data = ., id = AID, corstr = 'independence') %>% #independent working correlation
  summary()
```


9. Fit the model you fit above but not with an exponential decay (ar1) working correlation structure. Compare the Model SE and Robust SE to each other. Compare the difference you notice with the difference in Model v. Robust SE when assuming independence with GEE (in Q8). When the Model SE is close to the Robust SE it indicates that the working correlation structure model is close to the truth. 



```{r}
activeLong %>%  drop_na(Memory,Years,INTGRP) %>% 
  geem(??, data = ., id = AID, corstr = 'ar1') %>% #ar1 working correlation
  summary()
```

10. Using this GEE model with an exponential decay working correlation structure, give some general conclusions about the treatment and its impact on the Memory Score over time by interpreting the coefficients and using Robust SE's to provide uncertainty estimates. 




## Done!

- Render your notes.
- Check the solutions in the course website (Solution drop downs).
- If you finish all that during class, start your homework!


