Skip to content

Commit 3461e8b

Browse files
authored
Update ShaunRdoc-r01_fullbrain4.2.Rmd
1 parent d06f88f commit 3461e8b

File tree

1 file changed

+19
-32
lines changed

1 file changed

+19
-32
lines changed

ShaunRdoc-r01_fullbrain4.2.Rmd

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,12 @@ cfa.loadings = cfa.loadings[, order(colnames(cfa.loadings))]
125125
plot = heatmap.2(data.matrix(cfa.loadings), dendrogram = c("none"), Rowv = NA, Colv = NA, trace="none",col="bluered")
126126
127127
dd.cbfa.fscores = lavPredict(cfa.fit, method = "Bartlett")
128-
write.xlsx(dd.cbfa.fscores, "H:/Shared drives/BDL-Shaun/fscores_DDbifactorcfa.xlsx")
129128
dd.cbfa.fscorecoef <- inspect(cfa.fit, what = "std")$lambda
130129
131130
cfa.fit.rmsea = fitMeasures(cfa.fit, c("rmsea.robust", "rmsea.ci.lower.robust", "rmsea.ci.upper.robust", "rmsea.pvalue.robust", "rmsea"))
132131
cfa.fit.cfi = fitMeasures(cfa.fit, c("cfi.robust", "tli.robust", "srmr.robust"))
133132
cfa.fit.tli = fitMeasures(cfa.fit, c("tli.robust"))
134133
cfa.fit.aicbic = fitMeasures(cfa.fit, c("aic", "bic"))
135-
cfa.fit.srmr = fitMeasures(cfa.fit, c("srmr"))
136134
```
137135

138136
#CFA (not bifactor) based on EFA(F6)
@@ -146,9 +144,6 @@ cfanobi.fit.loadings = inspect(cfanobi.fit,what="std")$lambda
146144
cfanobi.fit.loadings = cfanobi.fit.loadings[order(row.names(cfanobi.fit.loadings)), ]
147145
cfanobi.fit.loadings[cfanobi.fit.loadings < .4 & cfanobi.fit.loadings > -.4] <- 0
148146
heatmap.2(data.matrix(cfanobi.fit.loadings), dendrogram = c("none"), Rowv = NA, Colv = NA, trace="none",col = colorRampPalette(c("white", "red"))(100))
149-
150-
fscores = lavPredict(cfanobi.fit, method = "Bartlett")
151-
factor_score_coefficients <- inspect(cfanobi.fit, what = "std")$lambda
152147
```
153148

154149
# SPPO4262 variance set to 0 for cfanobi.fit.model as estimated ov variances are negative
@@ -165,14 +160,10 @@ cfanobi.fit.loadings = cfanobi.fit.loadings[order(row.names(cfanobi.fit.loadings
165160
cfanobi.fit.loadings[cfanobi.fit.loadings < .4 & cfanobi.fit.loadings > -.4] <- 0
166161
heatmap.2(data.matrix(cfanobi.fit.loadings), dendrogram = c("none"), Rowv = NA, Colv = NA, trace="none",col = colorRampPalette(c("white", "red"))(100))
167162
168-
fscores = lavPredict(cfanobi.fit, method = "Bartlett")
169-
factor_score_coefficients <- inspect(cfanobi.fit, what = "std")$lambda
170-
171163
cfanobi.fit.rmsea = fitMeasures(cfanobi.fit, c("rmsea.robust", "rmsea.ci.lower.robust", "rmsea.ci.upper.robust", "rmsea.pvalue.robust", "rmsea"))
172164
cfanobi.fit.cfi = fitMeasures(cfanobi.fit, c("cfi.robust", "tli.robust", "srmr.robust"))
173165
cfanobi.fit.tli = fitMeasures(cfanobi.fit, c("tli.robust"))
174166
cfanobi.fit.aicbic = fitMeasures(cfanobi.fit, c("aic", "bic"))
175-
cfanobi.fit.srmr = fitMeasures(cfanobi.fit, c("srmr"))
176167
lavInspect(cfanobi.fit, "cov.lv")
177168
```
178169

@@ -181,22 +172,19 @@ lavInspect(cfanobi.fit, "cov.lv")
181172
rdoc.fit = cfa(rdoc.fit.model, data0, estimator = "MLR", std.lv = TRUE)
182173
semPaths(rdoc.fit, whatLabels = "std", layout="tree", edge.label.cex=1)
183174
summary(rdoc.fit, standardized=TRUE, rsquare=TRUE, fit.measures=TRUE)
175+
184176
rdoc.fit.rmsea = fitMeasures(rdoc.fit, c("rmsea.robust", "rmsea.ci.lower.robust", "rmsea.ci.upper.robust", "rmsea.pvalue.robust", "rmsea"))
185177
rdoc.fit.cfi = fitMeasures(rdoc.fit, c("cfi.robust", "tli.robust", "srmr.robust"))
186178
rdoc.fit.tli = fitMeasures(rdoc.fit, c("tli.robust"))
187179
rdoc.fit.r2 = inspect(rdoc.fit, 'r2')
188180
rdoc.fit.aicbic = fitMeasures(rdoc.fit, c("aic", "bic"))
189-
rdoc.fit.srmr = fitMeasures(rdoc.fit, c("srmr"))
190181
191182
rdoc.loadings = inspect(rdoc.fit,what="std")$lambda
192183
rdoc.loadings <- rdoc.loadings[order(row.names(rdoc.loadings)), ]
193-
# rdoc.loadings[rdoc.loadings < .4 & rdoc.loadings > -.4] <- 0
194184
heatmap.2(data.matrix(rdoc.loadings), dendrogram = c("none"), Rowv = NA, Colv = NA, trace="none",col="bluered")
195185
196186
rdoc.cfa.fscores = lavPredict(rdoc.fit, method = "Bartlett")
197-
write.xlsx(rdoc.cfa.fscores, "H:/Shared drives/BDL-Shaun/fscores_rdoccfa.xlsx")
198187
rdoc.cfa.fscorecoef <- inspect(rdoc.fit, what = "std")$lambda
199-
200188
lavInspect(rdoc.fit, "cov.lv")
201189
```
202190

@@ -208,7 +196,6 @@ summary(rdocbi.fit, standardized=TRUE, rsquare=TRUE, fit.measures=TRUE)
208196
209197
loadings = inspect(rdocbi.fit,what="std")$lambda
210198
loadings <- loadings[order(row.names(loadings)), ]
211-
#loadings[loadings < .4 & loadings > -.4] <- 0
212199
heatmap.2(data.matrix(loadings), dendrogram = c("none"), Rowv = NA, Colv = NA, trace="none",col="bluered")
213200
lavInspect(rdocbi.fit, "cov.lv")
214201
rdocbi.loadings = loadings
@@ -221,16 +208,15 @@ rdocbi.fit.model <- c(rdocbi.fit.model, "NVL982 ~~ 0*NVL982", "PVRR724 ~~ 0*PVRR
221208
rdocbi.fit = cfa(rdocbi.fit.model, data0, estimator = "MLR", orthogonal = TRUE, std.lv = TRUE)
222209
semPaths(rdocbi.fit, whatLabels = "std", layout="tree", edge.label.cex=1)
223210
summary(rdocbi.fit, standardized=TRUE, rsquare=TRUE, fit.measures=TRUE)
211+
224212
rdocbi.fit.rmsea = fitMeasures(rdocbi.fit, c("rmsea.robust", "rmsea.ci.lower.robust", "rmsea.ci.upper.robust", "rmsea.pvalue.robust", "rmsea"))
225213
rdocbi.fit.cfi = fitMeasures(rdocbi.fit, c("cfi.robust", "tli.robust", "srmr.robust"))
226214
rdocbi.fit.tli = fitMeasures(rdocbi.fit, c("tli.robust"))
227215
rdocbi.fit.r2 = inspect(rdocbi.fit, 'r2')
228216
rdocbi.fit.aicbic = fitMeasures(rdocbi.fit, c("aic", "bic"))
229-
rdocbi.fit.srmr = fitMeasures(rdocbi.fit, c("srmr"))
230217
231218
loadings = inspect(rdocbi.fit,what="std")$lambda
232219
loadings <- loadings[order(row.names(loadings)), ]
233-
#loadings[loadings < .4 & loadings > -.4] <- 0
234220
heatmap.2(data.matrix(loadings), dendrogram = c("none"), Rowv = NA, Colv = NA, trace="none",col="bluered")
235221
lavInspect(rdocbi.fit, "cov.lv")
236222
rdocbi.loadings = loadings
@@ -609,6 +595,14 @@ rdoc.testfit.model <- sapply(names(assignments_list), function(factor_name) {
609595
cat(paste(rdoc.testfit.model, collapse = "\n"))
610596
```
611597

598+
```{r, fig.height = 8, fig.width = 8}
599+
cfa.testfit = cfa(cfa.testfit.model, data_ns, estimator = "MLR", std.lv = TRUE, check.gradient = FALSE)
600+
semPaths(cfa.testfit, whatLabels = "std", layout="tree", edge.label.cex=1)
601+
summary(cfa.testfit, standardized=TRUE, rsquare=TRUE, fit.measures=TRUE)
602+
cfa.testfit.r2 = inspect(cfa.testfit, 'r2')
603+
```
604+
605+
remove variables with negative ov variances
612606
```{r, fig.height = 8, fig.width = 8}
613607
cfa.testfit.model <- c(cfa.testfit.model, "CSWM_working_memory_maintenance_working_memory_capacity ~~ 0*CSWM_working_memory_maintenance_working_memory_capacity", "CSDM_memory ~~ 0*CSDM_memory")
614608
@@ -620,17 +614,22 @@ cfa.testfit.r2 = inspect(cfa.testfit, 'r2')
620614
cfa.testfit.loadings = inspect(cfa.testfit,what="std")$lambda
621615
cfa.testfit.loadings = cfa.testfit.loadings[order(row.names(cfa.testfit.loadings)), ]
622616
cfa.testfit.loadings = cfa.testfit.loadings[, order(colnames(cfa.testfit.loadings))]
623-
# cfa.testfit.loadings[cfa.testfit.loadings < .4 & cfa.testfit.loadings > -.4] <- 0
624617
plot = heatmap.2(data.matrix(cfa.testfit.loadings), dendrogram = c("none"), Rowv = NA, Colv = NA, trace="none",col = colorRampPalette(c("white", "red"))(100))
625618
626619
cfa.testfit.rmsea = fitMeasures(cfa.testfit, c("rmsea.robust", "rmsea.ci.lower.robust", "rmsea.ci.upper.robust", "rmsea.pvalue.robust", "rmsea"))
627620
cfa.testfit.cfi = fitMeasures(cfa.testfit, c("cfi.robust", "tli.robust", "srmr.robust"))
628621
cfa.testfit.tli = fitMeasures(cfa.testfit, c("tli.robust"))
629622
cfa.testfit.aicbic = fitMeasures(cfa.testfit, c("aic", "bic"))
630-
cfa.testfit.srmr = fitMeasures(cfa.testfit, c("srmr"))
631623
lavInspect(cfa.testfit, "cov.lv")
632624
```
633625

626+
```{r, fig.height = 8, fig.width = 8}
627+
rdoc.testfit = cfa(rdoc.testfit.model, data_ns, estimator = "MLR", std.lv = TRUE,check.gradient = FALSE)
628+
semPaths(rdoc.testfit, whatLabels = "std", layout="tree", edge.label.cex=1)
629+
summary(rdoc.testfit, standardized=TRUE, rsquare=TRUE, fit.measures=TRUE)
630+
rdoc.testfit.r2 = inspect(rdoc.testfit, 'r2')
631+
```
632+
634633
```{r, fig.height = 8, fig.width = 8}
635634
rdoc.testfit.model <- c(rdoc.testfit.model, "CSP_visual_perception ~~ 0*CSP_visual_perception ", "CSCC_response_selection ~~ 0*CSCC_response_selection ")
636635
@@ -642,14 +641,12 @@ rdoc.testfit.r2 = inspect(rdoc.testfit, 'r2')
642641
rdoc.testfit.loadings = inspect(rdoc.testfit,what="std")$lambda
643642
rdoc.testfit.loadings = rdoc.testfit.loadings[order(row.names(rdoc.testfit.loadings)), ]
644643
rdoc.testfit.loadings = rdoc.testfit.loadings[, order(colnames(rdoc.testfit.loadings))]
645-
# rdoc.testfit.loadings[rdoc.testfit.loadings < .4 & rdoc.testfit.loadings > -.4] <- 0
646644
plot = heatmap.2(data.matrix(rdoc.testfit.loadings), dendrogram = c("none"), Rowv = NA, Colv = NA, trace="none",col="bluered")
647645
648646
rdoc.testfit.rmsea = fitMeasures(rdoc.testfit, c("rmsea.robust", "rmsea.ci.lower.robust", "rmsea.ci.upper.robust", "rmsea.pvalue.robust", "rmsea"))
649647
rdoc.testfit.cfi = fitMeasures(rdoc.testfit, c("cfi.robust", "tli.robust", "srmr.robust"))
650648
rdoc.testfit.tli = fitMeasures(rdoc.testfit, c("tli.robust"))
651649
rdoc.testfit.aicbic = fitMeasures(rdoc.testfit, c("aic", "bic"))
652-
rdoc.testfit.srmr = fitMeasures(rdoc.testfit, c("srmr"))
653650
lavInspect(rdoc.testfit, "cov.lv")
654651
```
655652

@@ -667,6 +664,7 @@ save(cfa.testfit.bs, file = "cfa42.testfit.ns_5kbsyuan.RData")
667664
```
668665

669666
```{r, fig.height = 3, fig.width = 3}
667+
#removal of nonadmissible solutions
670668
rdoc.testfit.bs <- rdoc.testfit.bs[rdoc.testfit.bs[, 1] < 1, ]
671669
rdoc.testfit.bs <- rdoc.testfit.bs[!(rdoc.testfit.bs[, 2] < 0 | rdoc.testfit.bs[, 2] > 1), ]
672670
rdoc.testfit.bs <- rdoc.testfit.bs[rdoc.testfit.bs[, 5] > 0, ]
@@ -708,10 +706,7 @@ fitdata <- data.frame(Models = c("RDoC", "DD"),
708706
aic.errmax = c(rdoc.testfit.aic.ci[[2]], cfa.testfit.aic.ci[[2]]),
709707
bic = c(rdoc.testfit.aicbic[2], cfa.testfit.aicbic[2]),
710708
bic.errmin = c(rdoc.testfit.bic.ci[[1]], cfa.testfit.bic.ci[[1]]),
711-
bic.errmax = c(rdoc.testfit.bic.ci[[2]], cfa.testfit.bic.ci[[2]]),
712-
srmr = c(rdoc.testfit.srmr[1], cfa.testfit.srmr[1]),
713-
srmr.errmin = c(rdoc.testfit.srmr.ci[[1]], cfa.testfit.srmr.ci[[1]]),
714-
srmr.errmax = c(rdoc.testfit.srmr.ci[[2]], cfa.testfit.srmr.ci[[2]])
709+
bic.errmax = c(rdoc.testfit.bic.ci[[2]], cfa.testfit.bic.ci[[2]])
715710
)
716711
717712
ggplot(fitdata, aes(x = Models, y = Robust.RMSEA)) +
@@ -754,14 +749,6 @@ ggplot(fitdata, aes(x = Models, y = bic)) +
754749
labs(y = "BIC")+
755750
theme_classic()
756751
757-
ggplot(fitdata, aes(x = Models, y = srmr)) +
758-
geom_bar(stat='identity', color="black", fill="skyblue", width=0.5) +
759-
geom_errorbar(aes(x = Models, ymin=srmr.errmin, ymax=srmr.errmax), width=0.4, colour="orange", linewidth=1.3) +
760-
scale_y_continuous(expand = expansion(mult = c(0, .1)))+
761-
ggtitle("srmr")+
762-
labs(y = "srmr")+
763-
theme_classic()
764-
765752
save(fitdata, file = "fitdata_nstestfit.RData")
766753
fitdata_nstestfit = fitdata
767754
fitdata_nstestfit[, -1] <- round(fitdata_nstestfit[, -1], 3)

0 commit comments

Comments
 (0)