set wd, install libraries

setwd("~/Desktop/ExD/ApA HW 2")
library(reshape)
## Warning: package 'reshape' was built under R version 3.2.5
library(nlme)
## Warning: package 'nlme' was built under R version 3.2.5

load data

weight<-read.table("weight.txt", header=T)
wm<-melt(weight, id.vars=1:2)

wm$day<- as.factor(ifelse(wm$variable=="MS2", 2,
               ifelse(wm$variable=="MS4", 4,
                      ifelse(wm$variable=="MS6", 6,
                             ifelse(wm$variable=="MS8", 8,
                                    ifelse(wm$variable=="MS10",10,
                                           ifelse(wm$variable=="MS12",12,
                                                  ifelse(wm$variable=="MS14",14,
                                    NA  ))))))))

Part I

#model with unstructured covariance 
US<-gls(model=value~Trt*day, 
        data=wm, 
        correlation=corSymm(form=~1|Subject),
        weights=varIdent(form=~1|day))
round(getVarCov(US),2)
## Marginal variance covariance matrix
##      [,1] [,2] [,3] [,4]  [,5]  [,6]  [,7]
## [1,] 7.01 7.04 7.30 6.80  6.95  6.58  6.62
## [2,] 7.04 7.81 7.85 7.23  7.53  7.15  6.98
## [3,] 7.30 7.85 9.14 8.70  9.06  8.55  8.59
## [4,] 6.80 7.23 8.70 9.14  9.32  8.68  8.75
## [5,] 6.95 7.53 9.06 9.32 10.40  9.74  9.66
## [6,] 6.58 7.15 8.55 8.68  9.74 10.24 10.05
## [7,] 6.62 6.98 8.59 8.75  9.66 10.05 11.06
##   Standard Deviations: 2.6476 2.7946 3.0232 3.0232 3.2249 3.2 3.3257

I expect that measurements taken close together in time should be more similar that those taken at more disparate time points This is supported by the covariance matrix. Covariance estimates decrease as distance between timepoints increases.

I think a heterogenous first-order autoregressive covariance structure will be most appropriate for the data. If a motivation for the experiment is that at least one of the treatment programs differs from control, I would expect strength measurements among treatment groups to increase in variablilty over time so that time point variances will tend to increase.

I can see this pattern in the unstructured covariance matrix too. While covariance estimates decrease with increasing distances between time points, variance estimates get larger over time.

Part II

#compound symmetry
cs<-gls(model=value~Trt*day, data=wm, correlation=corCompSymm(form=~1|Subject))
round(getVarCov(cs),2)
## Marginal variance covariance matrix
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 9.26 8.05 8.05 8.05 8.05 8.05 8.05
## [2,] 8.05 9.26 8.05 8.05 8.05 8.05 8.05
## [3,] 8.05 8.05 9.26 8.05 8.05 8.05 8.05
## [4,] 8.05 8.05 8.05 9.26 8.05 8.05 8.05
## [5,] 8.05 8.05 8.05 8.05 9.26 8.05 8.05
## [6,] 8.05 8.05 8.05 8.05 8.05 9.26 8.05
## [7,] 8.05 8.05 8.05 8.05 8.05 8.05 9.26
##   Standard Deviations: 3.043 3.043 3.043 3.043 3.043 3.043 3.043
 #first order autoregressive 
ar<-gls(model=value~Trt*day, data=wm, correlation=corAR1(form=~1|Subject))
round(getVarCov(ar),2)
## Marginal variance covariance matrix
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 9.10 8.59 8.11 7.65 7.22 6.82 6.43
## [2,] 8.59 9.10 8.59 8.11 7.65 7.22 6.82
## [3,] 8.11 8.59 9.10 8.59 8.11 7.65 7.22
## [4,] 7.65 8.11 8.59 9.10 8.59 8.11 7.65
## [5,] 7.22 7.65 8.11 8.59 9.10 8.59 8.11
## [6,] 6.82 7.22 7.65 8.11 8.59 9.10 8.59
## [7,] 6.43 6.82 7.22 7.65 8.11 8.59 9.10
##   Standard Deviations: 3.0166 3.0166 3.0166 3.0166 3.0166 3.0166 3.0166
  #hetero first order autoregressive
het<-gls(model=value~Trt*day, data=wm, correlation=corAR1(form=~1|Subject), weights=varIdent(form=~1|day))
round(getVarCov(het),2)
## Marginal variance covariance matrix
##      [,1] [,2] [,3] [,4]  [,5]  [,6]  [,7]
## [1,] 7.03 7.06 7.20 6.71  6.78  6.41  6.31
## [2,] 7.06 7.93 8.09 7.54  7.61  7.20  7.09
## [3,] 7.20 8.09 9.21 8.59  8.67  8.20  8.08
## [4,] 6.71 7.54 8.59 8.95  9.04  8.55  8.42
## [5,] 6.78 7.61 8.67 9.04 10.20  9.64  9.50
## [6,] 6.41 7.20 8.20 8.55  9.64 10.19 10.04
## [7,] 6.31 7.09 8.08 8.42  9.50 10.04 11.05
##   Standard Deviations: 2.6514 2.816 3.0348 2.9917 3.1937 3.1922 3.3242
##          AIC      BIC
## US  1318.022 1510.832
## CS  1459.239 1549.742
## AR  1297.583 1388.086
## HET 1299.054 1413.166

The first order autoregressive is actually a better fit for the data! It’s not a huge improvement over the hetergoenous AR1 model though, so I think it’s probably because both criteria I used penalize the hetAR1 model for including more parameters (8 for hetAR1 and only 1 for AR1)

Part III

Data is potentially log

log_ar1<-gls(model=value~Trt*log(day), data=wm, correlation=corAR1(form=~1|Subject))
round(getVarCov(log_ar1),2)
## Marginal variance covariance matrix
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 9.11 8.60 8.12 7.66 7.23 6.83 6.44
## [2,] 8.60 9.11 8.60 8.12 7.66 7.23 6.83
## [3,] 8.12 8.60 9.11 8.60 8.12 7.66 7.23
## [4,] 7.66 8.12 8.60 9.11 8.60 8.12 7.66
## [5,] 7.23 7.66 8.12 8.60 9.11 8.60 8.12
## [6,] 6.83 7.23 7.66 8.12 8.60 9.11 8.60
## [7,] 6.44 6.83 7.23 7.66 8.12 8.60 9.11
##   Standard Deviations: 3.0183 3.0183 3.0183 3.0183 3.0183 3.0183 3.0183
exp_ar2<-gls(model=value~Trt*(day^2), data=wm, correlation=corAR1(form=~1|Subject))
round(getVarCov(exp_ar2),2)
## Marginal variance covariance matrix
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 9.12 8.60 8.10 7.64 7.20 6.79 6.40
## [2,] 8.60 9.12 8.60 8.10 7.64 7.20 6.79
## [3,] 8.10 8.60 9.12 8.60 8.10 7.64 7.20
## [4,] 7.64 8.10 8.60 9.12 8.60 8.10 7.64
## [5,] 7.20 7.64 8.10 8.60 9.12 8.60 8.10
## [6,] 6.79 7.20 7.64 8.10 8.60 9.12 8.60
## [7,] 6.40 6.79 7.20 7.64 8.10 8.60 9.12
##   Standard Deviations: 3.0199 3.0199 3.0199 3.0199 3.0199 3.0199 3.0199
##                AIC      BIC
## log(time) 1266.117 1297.908
## time^2    1278.556 1310.346

Non-linear transformations of the time variable generally improve the model, with the log/linear model being the best fit.