1 Overview

This demo illustrates how to fit a linear growth curve model with random intercept and slope using dynr.

2 Preliminary

Loading the libraries used in this script and setting the path.

library(dynr)
#setwd()

Getting the simulated data.

d = read.table("./Data/GrowthCurveExample.csv",
               sep=",", header = TRUE)

3 Model description

The simulated data were generated from the following model. The data contain 25 subjects (N = 25) and 50 time points (T = 50) for each subject. \[\begin{bmatrix} y1(t) \end{bmatrix} = \begin{bmatrix} 1 & 0 \\ \end{bmatrix} \begin{bmatrix} Level(t)\\ Slope(t) \end{bmatrix} + \epsilon\]

\[\epsilon \sim N(0, 0.25)\] \[Level(t+1) = Level(t) + Deltat \times Slope(t) \] \[Slope(t+1) = Slope(t)\] \[\begin{bmatrix} Level(0)\\ Slope(0) \end{bmatrix} \sim N( \begin{bmatrix} 0.5\\ 0.25 \end{bmatrix}, \begin{bmatrix} 10 & 1\\ 1 & 2 \end{bmatrix} )\]

4 Fitting a linear growth curve model

4.1 Declare the data with the dynr.data() function

Specify names of variables to be used for modeling.

data <- dynr.data(d, id="subject", time="time", 
                  observed=c("y1"), covariates="Deltat")

4.2 Define elements of the measurement model

The values provided in values.XXX are starting values for the corresponding parameters. If an entry is specified as “fixed” under params.XXX, then it is fixed at the value specified.

meas <- prep.measurement(
  values.load=matrix(c(1,0),ncol=2,byrow=TRUE), 
  params.load=matrix(rep("fixed",2),ncol=2),
  state.names=c("Level","Slope"),
  obs.names=c("y1")
)

4.3 Define the initial conditions of the model

initial <- prep.initial(
  values.inistate=matrix(c(-1,
                           .5),ncol=1,byrow=TRUE),
  params.inistate=c('mu_Level0',
                    'mu_Slope0'), 
  values.inicov=matrix(c(5,1,
                         1,5),byrow=TRUE,ncol=2),
  params.inicov=matrix(c("v_11","c_12",
                         "c_12","v_22"),
                       byrow=TRUE,ncol=2)) 

4.4 Define the structures of the measurement noise covariance matrix and the dynamic noise covariance matrix

mdcov <- prep.noise(
  values.latent=diag(rep(0,2)), 
  params.latent=diag(rep("fixed",2)), 
  values.observed=.5, 
  params.observed='ErrorV')

4.5 Define elements of the dynamic model

formula2 =list(
  Level~ Level + Deltat*Slope,
  Slope~ Slope
  )
dynm  <- prep.formulaDynamics(formula=formula2,
                              isContinuousTime=FALSE)

4.6 Pass data and submodels to dynrModel object

GCmodel <- dynr.model(dynamics=dynm, measurement=meas,
                    noise=mdcov, initial=initial, data=data,#transform=trans,
                    outfile="GrowthCurve.c")

4.7 Plot the formula

Create a LaTeX file showing all the equations. Don’t run if you don’t already use LaTeX on your machine and have all the dependencies set up. Use plotFormula instead (see below).

printex(GCmodel, ParameterAs=GCmodel$'param.names', show=FALSE, printInit=TRUE, 
        printRS=FALSE,
        outFile="GrowthCurve.tex")
#tools::texi2pdf("GrowthCurve.tex")
#system(paste(getOption("pdfviewer"), "GrowthCurve.pdf"))
plotFormula(GCmodel, ParameterAs=GCmodel$'param.names')

4.8 Cook the model

GrowthCurveResults <- dynr.cook(GCmodel,debug_flag=TRUE)
summary(GrowthCurveResults)
## Coefficients:
##           Estimate Std. Error t value ci.lower ci.upper Pr(>|t|)    
## ErrorV     0.24422    0.00998  24.470  0.22466  0.26378  < 2e-16 ***
## mu_Level0  0.46814    0.61249   0.764 -0.73233  1.66859  0.22241    
## mu_Slope0  0.25123    0.27647   0.909 -0.29064  0.79311  0.18184    
## v_11       9.58439    2.71524   3.530  4.26262 14.90615  0.00022 ***
## c_12       0.99115    0.88110   1.125 -0.73577  2.71808  0.13042    
## v_22       1.91937    0.54210   3.541  0.85688  2.98186  0.00021 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## -2 log-likelihood value at convergence = 2257.64
## AIC = 2269.64
## BIC = 2300.42

5 Some plotting functions in dynr

dynr.ggplot(GrowthCurveResults,GCmodel,style=2)

plot(GrowthCurveResults,GCmodel)