###############Statistics
###############call libraries
library(psych)
library(lmerTest)
library(nlme)
library(geepack)
library(gee)
###############call data
data<-read.csv("data.csv", head=TRUE, sep=",")
#######Codebook
#######type = type of experiments, labels: SP(Spine-PD), EXTSP(Near-Spine-PD), AB(Near Bouton-PD)
#######treatment= labels:  PLX=1, control=0
#######time= treatment time; labels:  Post-treatment=1, Pre-treatment=0
#######outcomes= turnover%, gained%, lost%, filopodia ocurrence
data$y<-data$parameter ####### select type and parameter
head(data)
> head(data)
>    type    treatment time  miceID        y
>27 EXTSP        1             1    14769    3.703704
>28 EXTSP        1             1    16801    2.101806
>29 EXTSP        1             1    16944    2.941176
>30 EXTSP        1             1    17307    2.941176
>31 EXTSP        1             1    17357    1.449275
>32 EXTSP        1             1    18203    0.000000
#######ANOVA repeated measures
aov1 <- aov(y ~ treatment * time + Error(miceID), data = data)
summary(aov1)
>Error: miceID
>                   Df   SumSq   MeanSq F value Pr(>F)
>treatment   1     0.003     0.0034    0.008   0.929
>Residuals   10    4.071     0.4071               
>
>Error: Within
>                           Df    SumSq      MeanSq   F value    Pr(>F)   
>time                       1     12.07      12.075    6.752     0.02656 * 
>treatment:time             1     26.91      26.911    15.050    0.00306 **
>Residuals                        10         17.88     1.788                   
>---
>Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
###############In order to avoid inflate Type I error we used flexible models that do not assume equal variance and are suitable for smaller sample and larger standard errors. 
###############Linear Mixed Model
###############Model: time as repeated measures, treatment as fixed factor, time as covariate, and their interaction treatment*time
lme1 <- lmer(y ~ treatment + group + time : group + (1|miceID), data=data)
Anova(lme1)
>Type III Analysis of Variance Table with Satterthwaite's method
>                   Sum Sq   Mean Sq     NumDF      DenDF      F value       Pr(>F)    
>treatment          0.0034    0.0034       1          20        0.0031      0.956208    
>time               12.0745   12.0745      1          20        11.0005     0.003442   ** 
>treatment:time     26.9115   26.9115      1          20        24.5178     7.683e-05 ***
>---
>Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
###############To explore in more detail the estimation of variability between subjects and to avoid the influence
###############of correlation structure in predictors a Robust standard error estimation analysis was performed.  
###############Assuming an unstructured covariance structure between pre- and post-treatment measurements
###############the Generalized estimating equation Model was: time as repeated measures, treatment as fixed factor,
###############time as covariate, and their interaction treatment*time.
Model1<-gee(y ~  treatment *time  , data = data, id=miceID, family= gaussian, corstr="unstructured")
summary(Model1)
Model1 <-geese( y ~ treatment * time, id=miceID, data=data, corstr="unstructured", family=gaussian )
summary(Model1)
>Call:
>geese(formula = y ~ drug_cat * condition_cat, id = miceID, data = newdatax, 
>   family = gaussian, corstr = "unstructured")
>
>Mean Model:
> Mean Link:                 identity 
> Variance to Mean Relation: gaussian 
>
> Coefficients:
>                         estimate   san.se     wald         p
> (Intercept)            2.1895230  0.4936368 19.673610  9.186124e-06
> treatment              2.1416230  0.6042113 12.563432  3.933686e-04
> time                   0.6992424  0.5902751  1.403288  2.361739e-01
> treatment:time        -4.2356795  0.7808927 29.421409  5.823079e-08
>
> Scale Model:
> Scale Link:                identity 
>
> Estimated Scale Parameters:
>             estimate    san.se     wald            p
> (Intercept) 0.9146901 0.2275302 16.16106 5.817783e-05
>
>Correlation Model:
> Correlation Structure:     unstructured 
>
>Returned Error Value:    0 
>Number of clusters:   24   Maximum cluster size: 1
###############Post hoc pairwise comparisons
emmip (Model1, treatment ~ time)
emmeans(Model1, pairwise ~ treatment : time)
>$emmeans
>drug_cat condition_cat emmean    SE  df asymp.LCL asymp.UCL
> 1        1              2.190 0.494 Inf    1.2220      3.16
> 2        1              4.331 0.348 Inf    3.6483      5.01
> 1        2              2.889 0.324 Inf    2.2544      3.52
> 2        2              0.795 0.374 Inf    0.0614      1.53
>
>Covariance estimate used: vbeta 
>Confidence level used: 0.95 
>
>$contrasts
>contrast  estimate    SE  df z.ratio p.value
>
>1,1 - 1,2   -0.699 0.590 Inf -1.185  0.6366 
>1,1 - 2,2    1.395 0.619 Inf  2.252  0.1096 
>2,1 - 1,2    1.442 0.476 Inf  3.033  0.0129 
>2,1 - 2,2    3.536 0.511 Inf  6.917  <.0001 
>1,2 - 2,2    2.094 0.495 Inf  4.233  0.0001 
>
>P value adjustment: tukey method for comparing a family of 4 estimates



