Skip to content

Commit 1306df6

Browse files
authored
Update RDoC_pt1.Rmd
Changes after Reviewer Comments. - During the factor assignment phase of the validation process, the initial code erroneously considered non-absolute values when determining the greatest product sums between each map and the factors. We have corrected this error; because the number of high negative coefficients was relatively small, the results remained unchanged.
1 parent 15fd318 commit 1306df6

File tree

1 file changed

+69
-24
lines changed

1 file changed

+69
-24
lines changed

RDoC_pt1.Rmd

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -494,15 +494,15 @@ highest_sums <- list()
494494
for (var_name in colnames(data_ns)) {
495495
# Initialize variables to keep track of the best factor and its sum of products
496496
best_factor <- NULL
497-
best_sum_of_products <- -Inf
497+
best_sum_of_products <- 0
498498
499499
# Calculate the sums of products for each factor
500500
for (i in 1:length(colnames(dd.cbfa.fscores_nog))) {
501501
factor_name <- colnames(dd.cbfa.fscores_nog)[i]
502502
product_sum <- sum(data_ns[, var_name] * dd.cbfa.fscores_nog[, factor_name])
503-
503+
504504
# Check if this factor has a higher sum of products
505-
if (product_sum > best_sum_of_products) {
505+
if (abs(product_sum) > abs(best_sum_of_products)) {
506506
best_factor <- factor_name
507507
best_sum_of_products <- product_sum
508508
}
@@ -538,6 +538,18 @@ cfa.testfit.model <- sapply(names(assignments_list), function(factor_name) {
538538
# Print the final CFA model specification
539539
cat(paste(cfa.testfit.model, collapse = "\n"))
540540
```
541+
```{r, fig.height = 8, fig.width = 8}
542+
# Calculate the general factor 'g' as the sum of all other factors
543+
all_variables <- colnames(data_ns)
544+
g_definition <- paste("g =~", paste(all_variables, collapse = " + "))
545+
546+
# Include the general factor in the CFA model specification
547+
cfa.testfit2.model <- c(g_definition, cfa.testfit.model)
548+
cfa.testfit2.model <- paste(cfa.testfit2.model, collapse = "\n")
549+
550+
# Print the complete CFA model with the general factor
551+
cat("cfa.testfit2.model:\n", cfa.testfit2.model, "\n\n")
552+
```
541553

542554
```{r, fig.height = 8, fig.width = 8}
543555
# Initialize a list to store assignments for each factor
@@ -550,20 +562,20 @@ highest_sums <- list()
550562
for (var_name in colnames(data_ns)) {
551563
# Initialize variables to keep track of the best factor and its sum of products
552564
best_factor <- NULL
553-
best_sum_of_products <- -Inf
565+
best_sum_of_products <- 0
554566
555567
# Calculate the sums of products for each factor
556568
for (i in 1:length(colnames(rdoc.cfa.fscores))) {
557569
factor_name <- colnames(rdoc.cfa.fscores)[i]
558570
product_sum <- sum(data_ns[, var_name] * rdoc.cfa.fscores[, factor_name])
559571
560572
# Check if this factor has a higher sum of products
561-
if (product_sum > best_sum_of_products) {
573+
if (abs(product_sum) > abs(best_sum_of_products)) {
562574
best_factor <- factor_name
563575
best_sum_of_products <- product_sum
564576
}
565-
}
566-
577+
}
578+
567579
# Store the highest sum of products for each factor
568580
if (!(best_factor %in% names(highest_sums)) || best_sum_of_products > highest_sums[[best_factor]]) {
569581
highest_sums[[best_factor]] <- best_sum_of_products
@@ -595,6 +607,15 @@ rdoc.testfit.model <- sapply(names(assignments_list), function(factor_name) {
595607
cat(paste(rdoc.testfit.model, collapse = "\n"))
596608
```
597609

610+
```{r, fig.height = 8, fig.width = 8}
611+
# Include the general factor in the CFA model specification
612+
rdoc.testfit2.model <- c(g_definition, rdoc.testfit.model)
613+
rdoc.testfit2.model <- paste(rdoc.testfit2.model, collapse = "\n")
614+
615+
# Print the complete CFA model with the general factor
616+
cat("rdoc.testfit2.model:\n", rdoc.testfit2.model, "\n\n")
617+
```
618+
598619
```{r, fig.height = 8, fig.width = 8}
599620
cfa.testfit = cfa(cfa.testfit.model, data_ns, estimator = "MLR", std.lv = TRUE, check.gradient = FALSE)
600621
semPaths(cfa.testfit, whatLabels = "std", layout="tree", edge.label.cex=1)
@@ -604,7 +625,7 @@ cfa.testfit.r2 = inspect(cfa.testfit, 'r2')
604625

605626
remove variables with negative ov variances
606627
```{r, fig.height = 8, fig.width = 8}
607-
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")
628+
cfa.testfit.model <- c(cfa.testfit.model, "CSWM_working_memory_maintenance_working_memory_capacity ~~ 0*CSWM_working_memory_maintenance_working_memory_capacity")
608629
609630
cfa.testfit = cfa(cfa.testfit.model, data_ns, estimator = "MLR", std.lv = TRUE, check.gradient = FALSE)
610631
semPaths(cfa.testfit, whatLabels = "std", layout="tree", edge.label.cex=1)
@@ -614,7 +635,7 @@ cfa.testfit.r2 = inspect(cfa.testfit, 'r2')
614635
cfa.testfit.loadings = inspect(cfa.testfit,what="std")$lambda
615636
cfa.testfit.loadings = cfa.testfit.loadings[order(row.names(cfa.testfit.loadings)), ]
616637
cfa.testfit.loadings = cfa.testfit.loadings[, order(colnames(cfa.testfit.loadings))]
617-
plot = heatmap.2(data.matrix(cfa.testfit.loadings), dendrogram = c("none"), Rowv = NA, Colv = NA, trace="none",col = colorRampPalette(c("white", "red"))(100))
638+
plot = heatmap.2(data.matrix(cfa.testfit.loadings), dendrogram = c("none"), Rowv = NA, Colv = NA, trace="none",col="bluered")
618639
619640
cfa.testfit.rmsea = fitMeasures(cfa.testfit, c("rmsea.robust", "rmsea.ci.lower.robust", "rmsea.ci.upper.robust", "rmsea.pvalue.robust", "rmsea"))
620641
cfa.testfit.cfi = fitMeasures(cfa.testfit, c("cfi.robust", "tli.robust", "srmr.robust"))
@@ -631,7 +652,7 @@ rdoc.testfit.r2 = inspect(rdoc.testfit, 'r2')
631652
```
632653

633654
```{r, fig.height = 8, fig.width = 8}
634-
rdoc.testfit.model <- c(rdoc.testfit.model, "CSP_visual_perception ~~ 0*CSP_visual_perception ", "CSCC_response_selection ~~ 0*CSCC_response_selection ")
655+
rdoc.testfit.model <- c(rdoc.testfit.model, "CSCC_response_selection ~~ 0*CSCC_response_selection", "SPAP_animacy ~~ 0*SPAP_animacy")
635656
636657
rdoc.testfit = cfa(rdoc.testfit.model, data_ns, estimator = "MLR", std.lv = TRUE,check.gradient = FALSE)
637658
semPaths(rdoc.testfit, whatLabels = "std", layout="tree", edge.label.cex=1)
@@ -653,41 +674,65 @@ lavInspect(rdoc.testfit, "cov.lv")
653674
#Bootstrap by resampling parcels
654675
```{r, fig.height = 8, fig.width = 6}
655676
set.seed(5)
656-
rdoc.testfit.bs <- bootstrapLavaan(rdoc.testfit, R = 5000, type = "yuan", FUN = function(x) {
677+
cfa.testfit.bs <- bootstrapLavaan(cfa.testfit, R = 5000, type = "yuan", FUN = function(x) {
657678
fitMeasures(x, fit.measures = c("cfi.robust","tli.robust", "aic", "bic","rmsea.robust","srmr")) })
658679
659-
cfa.testfit.bs <- bootstrapLavaan(cfa.testfit, R = 5000, type = "yuan", FUN = function(x) {
680+
rdoc.testfit.bs <- bootstrapLavaan(rdoc.testfit, R = 5000, type = "yuan", FUN = function(x) {
660681
fitMeasures(x, fit.measures = c("cfi.robust","tli.robust", "aic", "bic","rmsea.robust","srmr")) })
661682
662-
save(rdoc.testfit.bs, file = "rdoc42.testfit.ns_5kbsyuan.RData")
663683
save(cfa.testfit.bs, file = "cfa42.testfit.ns_5kbsyuan.RData")
684+
save(rdoc.testfit.bs, file = "rdoc42.testfit.ns_5kbsyuan.RData")
664685
```
665686

666687
```{r, fig.height = 3, fig.width = 3}
667-
#removal of nonadmissible solutions
688+
remove_outliers <- function(data) {
689+
initial_count <- nrow(data)
690+
691+
q1 <- apply(data, 2, quantile, 0.25, na.rm = TRUE)
692+
q3 <- apply(data, 2, quantile, 0.75, na.rm = TRUE)
693+
iqr <- q3 - q1
694+
lower_bound <- q1 - 1.5 * iqr
695+
upper_bound <- q3 + 1.5 * iqr
696+
697+
filtered_data <- data
698+
for (i in 1:ncol(data)) {
699+
filtered_data <- filtered_data[filtered_data[, i] >= lower_bound[i] & filtered_data[, i] <= upper_bound[i], ]
700+
}
701+
702+
final_count <- nrow(filtered_data)
703+
preserved_percentage <- (final_count / initial_count) * 100
704+
705+
cat("Initial number of data points:", initial_count, "\n")
706+
cat("Number of data points after removing outliers:", final_count, "\n")
707+
cat("Percentage of data preserved:", preserved_percentage, "%\n")
708+
709+
return(filtered_data)
710+
}
711+
668712
rdoc.testfit.bs <- rdoc.testfit.bs[rdoc.testfit.bs[, 1] < 1, ]
669713
rdoc.testfit.bs <- rdoc.testfit.bs[!(rdoc.testfit.bs[, 2] < 0 | rdoc.testfit.bs[, 2] > 1), ]
670714
rdoc.testfit.bs <- rdoc.testfit.bs[rdoc.testfit.bs[, 5] > 0, ]
671-
rdoc.testfit.bs <- rdoc.testfit.bs[rdoc.testfit.bs[, 6] < 1, ]
715+
716+
cfa.testfit.bs <- cfa.testfit.bs[cfa.testfit.bs[, 1] < 1, ]
717+
cfa.testfit.bs <- cfa.testfit.bs[!(cfa.testfit.bs[, 2] < 0 | cfa.testfit.bs[, 2] > 1), ]
718+
cfa.testfit.bs <- cfa.testfit.bs[cfa.testfit.bs[, 5] > 0, ]
719+
720+
cfa.testfit.bs <- remove_outliers(cfa.testfit.bs)
721+
rdoc.testfit.bs <- remove_outliers(rdoc.testfit.bs)
722+
cfa.testfit2.bs <- remove_outliers(cfa.testfit2.bs)
723+
rdoc.testfit2.bs <- remove_outliers(rdoc.testfit2.bs)
672724
673725
rdoc.testfit.cfi.ci = quantile(rdoc.testfit.bs[, 1], probs = c(.025, .975), na.rm = TRUE)
674726
rdoc.testfit.tli.ci = quantile(rdoc.testfit.bs[, 2], probs = c(.025, .975), na.rm = TRUE)
675727
rdoc.testfit.aic.ci = quantile(rdoc.testfit.bs[, 3], probs = c(.025, .975), na.rm = TRUE)
676728
rdoc.testfit.bic.ci = quantile(rdoc.testfit.bs[, 4], probs = c(.025, .975), na.rm = TRUE)
677729
rdoc.testfit.rmsea.ci = quantile(rdoc.testfit.bs[, 5], probs = c(.025, .975), na.rm = TRUE)
678-
rdoc.testfit.srmr.ci = quantile(rdoc.testfit.bs[, 6], probs = c(.025, .975), na.rm = TRUE)
679-
680-
cfa.testfit.bs <- cfa.testfit.bs[cfa.testfit.bs[, 1] < 1, ]
681-
cfa.testfit.bs <- cfa.testfit.bs[!(cfa.testfit.bs[, 2] < 0 | cfa.testfit.bs[, 2] > 1), ]
682-
cfa.testfit.bs <- cfa.testfit.bs[cfa.testfit.bs[, 5] > 0, ]
683-
cfa.testfit.bs <- cfa.testfit.bs[cfa.testfit.bs[, 6] < 1, ]
684730
685731
cfa.testfit.cfi.ci = quantile(cfa.testfit.bs[, 1], probs = c(.025, .975), na.rm = TRUE)
686732
cfa.testfit.tli.ci = quantile(cfa.testfit.bs[, 2], probs = c(.025, .975), na.rm = TRUE)
687733
cfa.testfit.aic.ci = quantile(cfa.testfit.bs[, 3], probs = c(.025, .975), na.rm = TRUE)
688734
cfa.testfit.bic.ci = quantile(cfa.testfit.bs[, 4], probs = c(.025, .975), na.rm = TRUE)
689735
cfa.testfit.rmsea.ci = quantile(cfa.testfit.bs[, 5], probs = c(.025, .975), na.rm = TRUE)
690-
cfa.testfit.srmr.ci = quantile(cfa.testfit.bs[, 6], probs = c(.025, .975), na.rm = TRUE)
691736
```
692737

693738
```{r, fig.height = 4, fig.width = 4}
@@ -760,7 +805,7 @@ print(fitdata_nstestfit)
760805
plot_metric <- function(metric_name, real_metric = NULL, real_metric_name = NULL, title = "") {
761806
combined_metric <- rbind(
762807
data.frame(Model = "RDoC", Metric = rdoc.testfit.bs[, metric_name]),
763-
data.frame(Model = "CFA", Metric = cfa.testfit.bs[, metric_name])
808+
data.frame(Model = "DD", Metric = cfa.testfit.bs[, metric_name])
764809
)
765810
766811
p <- ggplot(combined_metric, aes(x = Model, y = Metric, fill = Model)) +
@@ -779,7 +824,7 @@ plot_metric <- function(metric_name, real_metric = NULL, real_metric_name = NULL
779824
780825
if (!is.null(real_metric)) {
781826
real_metric_df <- data.frame(
782-
Model = c("RDoC", "CFA"),
827+
Model = c("RDoC", "DD"),
783828
RealMetric = real_metric
784829
)
785830
p <- p + geom_quasirandom(data = real_metric_df, aes(x = Model, y = RealMetric), color = "black", size = 3)

0 commit comments

Comments
 (0)