-
Notifications
You must be signed in to change notification settings - Fork 1
/
clpm.R
131 lines (98 loc) · 5.24 KB
/
clpm.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# R Script for running cross-lagged panel models (CLPMs) for the discovery and replication samples
# Manuscript: Cross-Sectional and Longitudinal Relations Among Irritability, Attention-Deficit/Hyperactivity Disorder Symptoms, and Inhibitory Control
# Authors: Cameron C. McKay, Alethea V. de Jesus
# Date: 2024
# Table of Contents:
# - Install and load required packages
# - Load in data
# - CLPM for discovery sample
# - CLPM for replication sample
# Note: CLPM code originally modified from: https://rpubs.com/leomartinsjf/Ch5Newson
#########################################################
### Install and load required packages
#########################################################
if (!require("here")) {
install.packages("here", dependencies = TRUE)
library(here)
}
if (!require("lavaan")) {
install.packages("lavaan", dependencies = TRUE)
library("lavaan")
}
#########################################################
### Load in data
#########################################################
# Set filepath
filepath <- "path_to_data"
# Discovery sample
analysis_sample_1 <- read.csv(here::here(filepath, 'Project Code/analysis_sample_1.csv'), header = TRUE, sep = ",", dec = ".")
# Replication sample
analysis_sample_2 <- read.csv(here::here(filepath, 'Project Code/analysis_sample_2.csv'), header = TRUE, sep = ",", dec = ".")
#########################################################
### CLPM for discovery sample
#########################################################
# Make default data frame
attach(analysis_sample_1)
# Cross lagged panel model
# Regressions ON (~) "y regressed on x"; co-variation WITH (~~)
lavaan_model_1 <-
'Year2_CBCL_irritability_sum ~ baseline_CBCL_irritability_sum + baseline_cbcl_scr_dsm5_adhd_r + baseline_nihtbx_flanker_cs
Year2_cbcl_scr_dsm5_adhd_r ~ baseline_CBCL_irritability_sum + baseline_cbcl_scr_dsm5_adhd_r + baseline_nihtbx_flanker_cs
Year2_nihtbx_flanker_cs ~ baseline_CBCL_irritability_sum + baseline_cbcl_scr_dsm5_adhd_r + baseline_nihtbx_flanker_cs
baseline_CBCL_irritability_sum ~~ baseline_cbcl_scr_dsm5_adhd_r
baseline_CBCL_irritability_sum ~~ baseline_nihtbx_flanker_cs
baseline_cbcl_scr_dsm5_adhd_r ~~ baseline_nihtbx_flanker_cs
Year2_CBCL_irritability_sum ~~ Year2_cbcl_scr_dsm5_adhd_r
Year2_CBCL_irritability_sum ~~ Year2_nihtbx_flanker_cs
Year2_cbcl_scr_dsm5_adhd_r ~~ Year2_nihtbx_flanker_cs'
# Model fit
lavaan_fit_1 <- lavaan::sem(model = lavaan_model_1,
data = analysis_sample_1,
estimator = "ML")
# Print model summary
lavaan::summary(lavaan_fit_1, fit.measures=TRUE, standardized=TRUE)
# Model fit measures: clean output
d_lavaan_fit_measures_1 <- as.data.frame(
lavaan::fitmeasures(lavaan_fit_1, fit.measures = c("chisq", "df", "pvalue", "rmsea",
"rmsea.ci.lower", "rmsea.ci.upper",
"cfi", "nnfi", "aic", "bic")))
names(d_lavaan_fit_measures_1)[1]<-"Model fit measures"
# Parameter estimates: clean output
fit_parameter_1 <-lavaan::parameterestimates(lavaan_fit_1,output ="text",
standardized =T,
rsquare=T)
fit_parameter_1_df <- as.data.frame(fit_parameter_1)
#########################################################
### CLPM for replication sample
#########################################################
# Make default data frame
attach(analysis_sample_2)
# Cross lagged panel model
# Regressions ON (~) "y regressed on x"; co-variation WITH (~~)
lavaan_model_2 <-
'Year2_CBCL_irritability_sum ~ baseline_CBCL_irritability_sum + baseline_cbcl_scr_dsm5_adhd_r + baseline_nihtbx_flanker_cs
Year2_cbcl_scr_dsm5_adhd_r ~ baseline_CBCL_irritability_sum + baseline_cbcl_scr_dsm5_adhd_r + baseline_nihtbx_flanker_cs
Year2_nihtbx_flanker_cs ~ baseline_CBCL_irritability_sum + baseline_cbcl_scr_dsm5_adhd_r + baseline_nihtbx_flanker_cs
baseline_CBCL_irritability_sum ~~ baseline_cbcl_scr_dsm5_adhd_r
baseline_CBCL_irritability_sum ~~ baseline_nihtbx_flanker_cs
baseline_cbcl_scr_dsm5_adhd_r ~~ baseline_nihtbx_flanker_cs
Year2_CBCL_irritability_sum ~~ Year2_cbcl_scr_dsm5_adhd_r
Year2_CBCL_irritability_sum ~~ Year2_nihtbx_flanker_cs
Year2_cbcl_scr_dsm5_adhd_r ~~ Year2_nihtbx_flanker_cs'
# Model fit
lavaan_fit_2 <- lavaan::sem(model = lavaan_model_2,
data = analysis_sample_2,
estimator = "ML")
# Print model summary
lavaan::summary(lavaan_fit_2, fit.measures=TRUE, standardized=TRUE)
# Model fit measures: clean output
d_lavaan_fit_measures_2 <- as.data.frame(
lavaan::fitmeasures(lavaan_fit_2, fit.measures = c("chisq", "df", "pvalue", "rmsea",
"rmsea.ci.lower", "rmsea.ci.upper",
"cfi", "nnfi", "aic", "bic")))
names(d_lavaan_fit_measures_2)[1]<-"Model fit measures"
# Parameter estimates: clean output
fit_parameter_2 <-lavaan::parameterestimates(lavaan_fit_2,output ="text",
standardized =T,
rsquare=T)
fit_parameter_2_df <- as.data.frame(fit_parameter_2)