# NOT RUN {
library("dplyr")
library("graphicalVAR")
beta <- matrix(c(
0,0.5,
0.5,0
),2,2,byrow=TRUE)
kappa <- diag(2)
simData <- graphicalVARsim(50, beta, kappa)
# Form model:
model <- gvar(simData)
# Evaluate model:
model <- model %>% runmodel
# Parameter estimates:
model %>% parameters
# Note: this example is wrapped in a dontrun environment because the data is not
# available locally.
# }
# NOT RUN {
# Longer example:
#
# Obtain the data from:
#
# Epskamp, S., van Borkulo, C. D., van der Veen, D. C., Servaas, M. N., Isvoranu, A. M.,
# Riese, H., & Cramer, A. O. (2018). Personalized network modeling in psychopathology:
# The importance of contemporaneous and temporal connections. Clinical Psychological
# Science, 6(3), 416-427.
#
# Available here: https://osf.io/c8wjz/
tsdata <- read.csv("Supplementary2_data.csv")
# Encode time variable in a way R understands:
tsdata$time <- as.POSIXct(tsdata$time, tz = "Europe/Amsterdam")
# Extract days:
tsdata$Day <- as.Date(tsdata$time, tz = "Europe/Amsterdam")
# Variables to use:
vars <- c("relaxed", "sad", "nervous", "concentration", "tired", "rumination",
"bodily.discomfort")
# Estimate, prune with FDR, and perform stepup search:
model_FDRprune <- gvar(
tsdata,
vars = vars,
dayvar = "Day",
estimator = "FIML"
) %>%
runmodel %>%
prune(adjust = "fdr", recursive = FALSE) %>%
stepup(criterion = "bic")
# Estimate with greedy stepup search:
model_stepup <- gvar(
tsdata,
vars = vars,
dayvar = "Day",
estimator = "FIML",
omega_zeta = "empty",
beta = "empty"
) %>%
runmodel %>%
stepup(greedy = TRUE, greedyadjust = "bonferroni", criterion = "bic")
# Compare models:
compare(
FDRprune = model_FDRprune,
stepup = model_stepup
)
# Very similar but not identical. Stepup is prefered here according to AIC and BIC
# Stepup results:
temporal <- getmatrix(model_stepup, "PDC") # PDC = Partial Directed Correlations
contemporaneous <- getmatrix(model_stepup, "omega_zeta")
# Average layout:
library("qgraph")
L <- averageLayout(temporal, contemporaneous)
# Labels:
labs <- gsub("\\.","\n",vars)
# Plot:
layout(t(1:2))
qgraph(temporal, layout = L, theme = "colorblind", directed=TRUE, diag=TRUE,
title = "Temporal", vsize = 12, mar = rep(6,4), asize = 5,
labels = labs)
qgraph(contemporaneous, layout = L, theme = "colorblind",
title = "Contemporaneous", vsize = 12, mar = rep(6,4), asize = 5,
labels = labs)
# }
Run the code above in your browser using DataLab