Skip to content

Instantly share code, notes, and snippets.

View SachaEpskamp's full-sized avatar

Sacha Epskamp SachaEpskamp

View GitHub Profile
@SachaEpskamp
SachaEpskamp / factorToVAR.R
Created June 10, 2017 21:58
Function to translate dynamic factor models into epxected graphical VAR models.
# This function computes the equivalent graphical VAR model given a dynamical factor model. It requires graphicalVAR to be installed.
factorToVAR <- function(lambda, beta, psi, theta){
if (missing(lambda) | missing(beta) | missing(psi) | missing(theta)){
stop("'lambda', 'beta', 'psi' and 'theta' may not be missing.")
}
# Number of observed:
nObs <- nrow(lambda)
# Number of latents:
nLat <- ncol(lambda)
@SachaEpskamp
SachaEpskamp / app.R
Last active September 7, 2017 09:44
qgraph network comparison app
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library("BDgraph")
library("glasso")
# Seed to reproduce results:
set.seed(1)
# Simulate 20% sparse network and data:
bdres <- bdgraph.sim(20, n = 10000, prob = 0.8, b = 20, D = diag(20, 20))
# Sparsify inverse:
library("BDgraph")
library("glasso")
library("bootnet") # Install from github: devtools::install_github("sachaepskamp/bootnet)
# Seed to reproduce results:
set.seed(1)
# Simulate 20% sparse network:
bnres <- genGGM(20, 0.8, graph = "random")
library(qgraph)
library(bootnet)
net <- genGGM(10)
Sigma <- cov2cor(solve(diag(10) - net))
D <- diag(1, 10) # Change the value here to add or remove measurment error
qgraph(Sigma + D, graph = "pcor")
@SachaEpskamp
SachaEpskamp / app.R
Created June 2, 2018 20:48
Statcheck web app
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
@SachaEpskamp
SachaEpskamp / global.R
Last active June 29, 2018 14:29
Shiny App change scores Cross-sectional networks
library("bootnet")
library("qgraph")
library("dplyr")
library("ggplot2")
library("knitr")
nNodes <- 12
neighborhood <- 2
# 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)
# Install psychonetrics:
# devtools::install_github("sachaepskamp/psychonetrics")
library("psychonetrics")
# Set the seed:
set.seed(1)
# Let's simulate some data, 2-factor model with 10 indicators each and residual chain graph.
# Generate factor loadings:
lambda <- matrix(0,20,2)
library("foreign")
Data <- read.spss("vHB_SESP_Datafile_for_sharing 29.8.15.sav", to.data.frame = TRUE)
# Relabel data:
names(Data) <- gsub("\\.","",names(Data))
# Lavaan model:
mod <- '
f1 =~ SexDifG1 + GenVio1 + UAtt1 + MenUnf1 + SexHorm1 + EvoPsy1
f2 =~ SexDifG2 + GenVio2 + UAtt2 + MenUnf2 + SexHorm2 + EvoPsy2