---
title: "Two-Stage Summary Statistics approach: flip2sss"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{TDP}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```


# Info data

Original data are in Kaggle
<https://www.kaggle.com/datasets/jboysen/mri-and-alzheimers>

**Context:**

The Open Access Series of Imaging Studies (OASIS) is a project aimed at making MRI data sets of the brain freely available to the scientific community. By compiling and freely distributing MRI data sets, we hope to facilitate future discoveries in basic and clinical neuroscience. OASIS is made available by the Washington University Alzheimer's Disease Research Center, Dr. Randy Buckner at the Howard Hughes Medical Institute (HHMI)( at Harvard University, the Neuroinformatics Research Group (NRG) at Washington University School of Medicine, and the Biomedical Informatics Research Network (BIRN).

**Content:**

Longitudinal MRI Data in Nondemented and Demented Older Adults: This set consists of a longitudinal collection of 150 subjects aged 60 to 96. Each subject was scanned on two or more visits, separated by at least one year for a total of 373 imaging sessions. For each subject, 3 or 4 individual T1-weighted MRI scans obtained in single scan sessions are included. The subjects are all right-handed and include both men and women. 72 of the subjects were characterized as nondemented throughout the study. 64 of the included subjects were characterized as demented at the time of their initial visits and remained so for subsequent scans, including 51 individuals with mild to moderate Alzheimer's disease. Another 14 subjects were characterized as nondemented at the time of their initial visit and were subsequently characterized as demented at a later visit.

**Acknowledgements:**

When publishing findings that benefit from OASIS data, please include the following grant numbers in the acknowledgements section and in the associated Pubmed Central submission: P50 AG05681, P01 AG03991, R01 AG021910, P20 MH071616, U24 RR0213

# Import data

```{r}

data("oasis",package="jointest")

# from here on 0 means Age=77 (which is about the mean)
oasis$Age=oasis$Age-77
```

```{r}
library(ggplot2)
p <- ggplot(oasis,aes(Group,nWBV,color=Group))
p+geom_point(size = 3) +geom_boxplot(alpha=.1) + theme_bw()

p <- ggplot(oasis,aes(Age,nWBV,color=Group))
p+geom_point(size = 3) +geom_smooth(method = lm) + theme_bw()

p=ggplot(oasis, aes(x = Age, y = nWBV, colour = Group, group = Subject.ID)) + geom_line(aes(linetype=Gender)) + geom_point()
p + theme_bw()

```

# Mixed Model Approach

```{r}
library(lme4)
library(ggplot2)


mod=lmer(nWBV ~ Age*Group+Gender*Group+ (1+Age|Subject.ID),data=oasis)
summary(mod)
anova(mod)


```

REMARK: Note that without the centering `oasis$Age=oasis$Age-77`:  **Model failed to converge with max|grad| = 0.640671 (tol = 0.002, component 1)**




# Two-Stage Summary Statistics approach: flip2sss (= Second-level, Group-level Analisys)

```{r}
library(jointest)

mod=flip2sss(nWBV ~ Age*Group+Gender*Group, 
             cluster =oasis$Subject.ID,data=oasis)
summary(mod)

#ANOVA-like combination
#Overall
summary(combine(mod))
#by Variables
summary(combine_contrasts(mod))


# p <- ggplot(oasis1lev,aes(Group,Interc,color=Group))
# p+geom_point(size = 3) +geom_boxplot(alpha=.1) + theme_bw()
# 
# 
# p <- ggplot(oasis1lev,aes(Group,Slope,color=Group))
# p+geom_point(size = 3) +geom_boxplot(alpha=.1) + theme_bw()
# 

```
