Skip to content

Instantly share code, notes, and snippets.

@SachaEpskamp
Created September 1, 2019 03:44
Show Gist options
  • Save SachaEpskamp/22bbe8aa45747ad7cccc8e99475eee0f to your computer and use it in GitHub Desktop.
Save SachaEpskamp/22bbe8aa45747ad7cccc8e99475eee0f to your computer and use it in GitHub Desktop.
library("parSim")
parSim(
### SIMULATION CONDITIONS
# Vary sample size:
sampleSize = c(250, 500, 1000),
# Vary missingness:
missing = c(0, 0.1, 0.25),
reps = 100, # 5 repititions
write = TRUE, # Writing results to a file
name = "missingdata_sims_2", # Name of the file
nCores = 8, # I use 8 cores here
expression = {
### SIMULATION CODE ###
# Packages:
library("psychonetrics")
library("dplyr")
library("bootnet")
library("mice")
# Function needed:
source("compareNetworks.R")
# Generate true network:
trueNet <- genGGM(10, nei = 2, propPositive = 0.8, p = 0.25)
# Generate data:
generator <- ggmGenerator()
Data <- generator(sampleSize, trueNet)
# Add missings:
for (i in 1:ncol(Data)){
Data[runif(sampleSize) < missing,i] <- NA
}
## Method 1: psychonetrics with FIML
# Estimate model (FDR prune):
mod <- ggm(Data, estimator = "FIML") %>%
runmodel %>%
prune(adjust = "fdr", alpha = 0.05, recursive = FALSE)
# Extract network:
estNet <- getmatrix(mod, "omega")
# Compare to true network:
results_psychonetrics <- compareNetworks(trueNet, estNet)
# Add method:
results_psychonetrics$method <- "psychonetrics_FIML"
## Method 2: ggmModSelect with mice imputation
# Impute data using mice:
miceres <- mice(Data, m = 1)
imputedData <- complete(miceres)
# Run ggmModSelect:
net <- estimateNetwork(imputedData, default = "ggmModSelect")
estNet <- net$graph
# Compare to true network:
results_ggmModSelect <- compareNetworks(trueNet, estNet)
# Add method:
results_ggmModSelect$method <- "ggmModSelect_mice"
# Combine both in a data frame:
results <- bind_rows(results_psychonetrics,results_ggmModSelect)
# Return:
results
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment