# NOT RUN {
# Load bfi data from psych package:
library("psychTools")
data(bfi)
# Also load dplyr for the pipe operator:
library("dplyr")
# Let's take the agreeableness items, and gender:
ConsData <- bfi %>%
select(A1:A5, gender) %>%
na.omit # Let's remove missingness (otherwise use Estimator = "FIML)
# Define variables:
vars <- names(ConsData)[1:5]
# Let's fit an empty GGM:
mod0 <- ggm(ConsData, vars = vars, omega = "empty")
# Run the model:
mod0 <- mod0 %>% runmodel
# }
# NOT RUN {
# We can look at the modification indices:
mod0 %>% MIs
# To automatically add along modification indices, we can use stepup:
mod1 <- mod0 %>% stepup(greedy = TRUE, alpha = 0.005, greedyadjust = "fdr")
# Let's also prune all non-significant edges to finish:
mod1 <- mod1 %>% prune(alpha = 0.005)
# Look at the fit:
mod1 %>% fit
# Compare to original (baseline) model:
compare(baseline = mod0, adjusted = mod1)
# We can also look at the parameters:
mod1 %>% parameters
# Or obtain the network as follows:
getmatrix(mod1, "omega")
# We may also be interested in the stability of our search algorithm.
# We can bootstrap our data and repeat the search as follows:
mod_boot <- ggm(ConsData, vars = vars, omega = "empty", storedata = TRUE) %>%
bootstrap %>% # bootstrap data
runmodel %>% # Run model
stepup(greedy = TRUE, alpha = 0.005, greedyadjust = "fdr") %>% # Search algorithm 1
prune(alpha = 0.005) # Search algorithm 2
# Which may give some different results:
getmatrix(mod_boot, "omega")
# This can be repeated (ideally 100 - 1000 times):
bootstraps <- replicate(10,simplify = FALSE,expr = {
mod_boot <- ggm(ConsData, vars = vars, omega = "empty", storedata = TRUE) %>%
bootstrap %>% # bootstrap data
runmodel %>% # Run model
stepup(greedy = TRUE, alpha = 0.005, greedyadjust = "fdr") %>% # Search algorithm 1
prune(alpha = 0.005) # Search algorithm 2
getmatrix(mod_boot, "omega")
})
# Now we can look at, for example, the inclusion probability:
inclusionProportion <- 1/length(bootstraps) * Reduce("+",lapply(bootstraps,function(x)1*(x!=0)))
inclusionProportion
# }
Run the code above in your browser using DataLab