Created
February 4, 2019 09:14
-
-
Save SachaEpskamp/f88f30415c973668796f7685834b2bc8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Needed packages: | |
library("qgraph") | |
library("psych") | |
library("huge") | |
data(bfi) | |
# Subset of 2000 to train model on: | |
bfiTrain <- huge.npn(bfi[1:1000,1:25]) | |
corMat_train <- cor_auto(bfiTrain, missing = "fiml", detectOrdinal = FALSE) | |
# Rest to evaluate model: | |
bfiTest <- huge.npn(bfi[-(1:1000),1:25]) | |
corMat_test <- cor_auto(bfiTest, missing = "fiml", detectOrdinal = FALSE) | |
# Fit network: | |
net <- ggmModSelect(corMat_train,nrow(bfiTrain)) | |
# Fit measures in training set (refit not needed): | |
fit_train <- ggmFit(pcor = net$graph, sampleSize = nrow(bfiTrain), covMat = corMat_train, refit = FALSE) | |
fit_train$fitMeasures$network <- "train" | |
# Fit measures in test set (not refitting parameters): | |
fit_test_noRefit <- ggmFit(pcor = net$graph, sampleSize = nrow(bfiTest), covMat = corMat_test, refit = FALSE) | |
fit_test_noRefit$fitMeasures$network <- "test (no refit)" | |
# Fit measures in test set (also refitting each edge): | |
fit_test_refit <- ggmFit(pcor = net$graph, sampleSize = nrow(bfiTest), covMat = corMat_test, refit = TRUE) | |
fit_test_refit$fitMeasures$network <- "test (refit)" | |
# Display them all: | |
rbind(as.data.frame(fit_train$fitMeasures), | |
as.data.frame(fit_test_noRefit$fitMeasures), | |
as.data.frame(fit_test_refit$fitMeasures))[,c("network","rmsea","cfi","nfi")] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment