# # R syntax for continuous time structural equation modelling of cross-lag panel data in the NZAVS # Use the corresponding SPSS syntax file to first create the data structure needed by the ctsem package # This default is set to work with 5 waves of pannel data, with 2 variables assessed accross waves, # allowing for missing data, assuming a stationary process, and adjusting for unobserved heterogenity or # differences across persons. # Sibley, C. G., & Osborne, D. O. (2016). Ideology and post-colonial society. # Advances in Political Psychology, 37, 115-161. # # Data Structure # Y1_T0 Y2_T0 Y1_T1 Y2_T1 Y1_T2 Y2_T2 Y1_T3 Y2_T3 Y1_T4 Y2_T4 dT1 dT2 dT3 dT4 # [.........................Manifest indicators, T0, T1, and so on............] [......Time intervals.....] # # Write CSV in R # write.csv(MyData, file = "MyData.csv") #install ctsem package install.packages("ctsem", repos = "http://r-forge.r-project.org", type = 'source') #load ctsem package library('ctsem') # Set the working directory setwd("C:/NZAVS Mplus Data") #load dataset, call it 'nzavs' nzavs_ctsem <- read.table("C:/NZAVS Mplus Data/NZAVS_ctsem_restructure.txt", header=TRUE, sep="\t", na.strings = "-9999") #check on number of rows nrow(nzavs_ctsem) #specifies ctModel with 2 processes over 5 waves, and controlling for unobserved heterogenity or differences across persons (akin to a random effect) nzavs_model <- ctModel(n.latent = 2, n.manifest = 2, Tpoints = 5, manifestNames = c('Y1', 'Y2'), latentNames = c('HRN', 'SPE'), LAMBDA = diag(2), TRAITVAR = "auto") #plot means for(i in 1:2){ plot(colMeans(nzavs_ctsem[,paste0('Y',i,'_T',0:4)],na.rm=T),type='b') } #assumes stationary model, with first observation a random point in an ongoing (stationary) process. model_fit <- ctFit(datawide = nzavs_ctsem, ctmodelobj = nzavs_model, stationary = c('T0VAR', 'T0MEANS')) #summary statistics summary(model_fit) #estimate drift at conditional values of time expm(summary(model_fit)$DRIFT * .20) #estimate drift at conditional values of time expm(summary(model_fit)$DRIFT * .40) #estimate drift at conditional values of time expm(summary(model_fit)$DRIFT * .60) #estimate drift at conditional values of time expm(summary(model_fit)$DRIFT * .80) #estimate drift at conditional values of time expm(summary(model_fit)$DRIFT * 1.00) #estimate drift at conditional values of time expm(summary(model_fit)$DRIFT * 1.20) #estimate drift at conditional values of time expm(summary(model_fit)$DRIFT * 1.40) #estimate drift at conditional values of time expm(summary(model_fit)$DRIFT * 1.60) #estimate drift at conditional values of time expm(summary(model_fit)$DRIFT * 1.80) #estimate drift at conditional values of time expm(summary(model_fit)$DRIFT * 2) #generates drift plots plot(model_fit)