# NOT RUN {
## load the psychNET library
library(psychNET)
## load the Canada dataset from the 'vars' package
data("Canada", package = "vars")
Canada_data_frame <- data.frame(Canada)
## fit a VAR model
VAR_model <- psychNET(Canada_data_frame, model = "VAR", lag = 1, type = "const")
# print the result
VAR_model
# summarize the resulting network
summary(VAR_model)
# summarize the VAR model using the original summary method
vars:::summary.varest(VAR_model$fit)
# plot the VAR model results using the original plot method
vars:::plot.varest(VAR_model$fit)
# plot the resulting network
plot(VAR_model)
## fit a sparse VAR model
sparse_VAR_model <- psychNET(Canada_data_frame, model = "SVAR", lag = 1)
# print the result
sparse_VAR_model
# summarize the resulting network
summary(sparse_VAR_model)
# plot the resulting network
plot(sparse_VAR_model)
## fit a sparse VAR model as the one in the 'bigtime' package
sparse_lassVAR_model <- psychNET(Canada_data_frame,
model = "SVARHL",penalty = "LASSO", lag = 1, VARgran=c(500,1000))
# print the result
sparse_lassVAR_model
# summarize the resulting network
summary(sparse_lassVAR_model)
# plot the resulting network
plot(sparse_lassVAR_model)
# }
# NOT RUN {
## Load the psychNET package
library(psychNET)
################################## N=1 models ####################################
##################################################################################
####################################################################
######## REPRODUCE EXAMPLE OF A VAR FROM THE 'vars' PACKAGE ########
####################################################################
## load the 'vars' package
library(vars)
## Load the Canada dataset from the vars package
data(Canada)
## check the structure of the data
str(Canada)
## The data is in time series format. It needs to be transformed into
## a matrix, data.frame or longitudinal object for the psychNET package
Canada_data_frame <- data.frame(Canada)
## fitting a VAR using the vars package
varmod <- vars::VAR(Canada, p = 2, type = "none")
## fitting the same VAR using the psychNET package
psychvar <- psychNET(Canada_data_frame, model = "VAR", lag = 2, type = "none")
## Check if the results are the same
all.equal(Acoef(varmod), psychvar$results$A, check.attributes = FALSE)
################################################################################
####### Fit A DYNAMIC FACTOR TO THE CANADA DATA FROM THE 'vars' PACKAGE #######
################################################################################
## install and load the 'devtools' package and the
## by using install.packages("devtools") and then
## library(devtools)
## install the 'dynfactoR' package available on github
## by using devtools::install_github("rbagd/dynfactoR")
## library(dynfactoR)
## Load the Canada dataset from the vars package
data(Canada, package = "vars")
## check the structure of the data
str(Canada)
## The data is in time series format. It needs to be transformed into
## a matrix, data.frame or longitudinal object for the psychNET package
Canada_data_frame <- data.frame(Canada)
## fitting a DFM using the dynfactoR package
dfmmod <- psychNET:::dfm(Canada_data_frame, r=2, p = 1,
rQ= "identity", rC= "upper",max_iter = 100000)
## fitting the same DFM using the psychNET package
psychdfm <- psychNET(Canada_data_frame, model = "DFM", nFact = 2, lag = 1)
## Check if the results are the same
all.equal(dfmmod$A, psychdfm$results$A_fact[[1]], check.attributes = FALSE)
all.equal(dfmmod$C, psychdfm$results$B_fac_symptoms, check.attributes = FALSE)
all.equal(dfmmod$Q, psychdfm$results$System_Covariance, check.attributes = FALSE)
all.equal(dfmmod$R, psychdfm$results$Obs_Covariance, check.attributes = FALSE)
#####################################################################################
########### FIT A SPARSE VAR TO THE CANADA DATA FROM THE 'vars' PACKAGE ############
#####################################################################################
## Load the Canada dataset from the vars package
data(Canada, package = "vars")
## The data is in time series format. It needs to be transformed into
## a matrix, data.frame or longitudinal object for the psychNET package
Canada_data_frame <- data.frame(Canada)
## fitting a SVAR using the sparsevar package
set.seed(1)
svarmod <- psychNET:::fitVAR(Canada, p = 1, penalty="SCAD", method="cv", logScale=FALSE)
## fitting the same SVAR using the psychNET package
set.seed(1)
psychsvar <- psychNET(Canada_data_frame, model = "SVAR",penalty = "SCAD", lag = 1, criterion="CV")
## Check if the results are the same
all.equal(svarmod$A, psychsvar$results$A, check.attributes = FALSE)
########################################################################################
####### EXAMPLE OF A SPARSE VAR WITH HIERARCHICAL LAGS FROM THE 'bigtime' PACKAGE ######
########################################################################################
## load the 'bigtime' package
library(bigtime)
## Load the Y dataset from the bigtime package
data(Y, package = "bigtime")
## fitting a SVAR with hierarchical lags
## using the bigtime package
svarhlmod <- sparseVAR(Y, VARpen ="HLag")
## fitting the same model using the psychNET package
psychsvarhl <- psychNET(Y, model = "SVARHL", penalty = "HLag", criterion="CV")
## Check if the results are the same
all.equal(svarhlmod$Phihat, psychsvarhl$fit$Phihat, check.attributes = FALSE)
######################################################################################
########### REPRODUCE EXAMPLE OF A SPARSE MIXED VAR FROM THE 'mgm' PACKAGE ###########
######################################################################################
## load the 'mgm' package
library(mgm)
# 1) Define mVAR model as in the mgm manual
p <- 6 # Six variables
type <- c("c", "c", "c", "c", "g", "g") # 4 categorical, 2 gaussians
level <- c(2, 2, 4, 4, 1, 1) # 2 categoricals with m=2, 2 categoricals with m=4, two continuous
max_level <- max(level)
lags <- 1:3 # include lagged effects of order 1-3
n_lags <- length(lags)
# Specify thresholds
thresholds <- list()
thresholds[[1]] <- rep(0, level[1])
thresholds[[2]] <- rep(0, level[2])
thresholds[[3]] <- rep(0, level[3])
thresholds[[4]] <- rep(0, level[4])
thresholds[[5]] <- rep(0, level[5])
thresholds[[6]] <- rep(0, level[6])
# Specify standard deviations for the Gaussians
sds <- rep(NULL, p)
sds[5:6] <- 1
# Create coefficient array
coefarray <- array(0, dim=c(p, p, max_level, max_level, n_lags))
# a.1) interaction between continuous 5<-6, lag=3
coefarray[5, 6, 1, 1, 2] <- .4
# a.2) interaction between 1<-3, lag=1
m1 <- matrix(0, nrow=level[2], ncol=level[4])
m1[1,1:2] <- 1
m1[2,3:4] <- 1
coefarray[1, 3, 1:level[2], 1:level[4], 1] <- m1
# a.3) interaction between 1<-5, lag=9
coefarray[1, 5, 1:level[1], 1:level[5], 3] <- c(0, 1)
# 2) Sample
set.seed(1)
dlist <- mvarsampler(coefarray = coefarray,
lags = lags,
thresholds = thresholds,
sds = sds,
type = type,
level = level,
N = 200,
pbar = TRUE)
# 3) Transform data into a data.frame for psychoNET suitability
# each categorical variable is coded as factor, each poisson as integer, gaussian as numeric
d1 <- as.data.frame(dlist$data)
d1$V1 <- as.factor(d1$V1)
d1$V2 <- as.factor(d1$V2)
d1$V3 <- as.factor(d1$V3)
d1$V4 <- as.factor(d1$V4)
dat <- d1
## fitting the SMVAR model using the mgm package
smvarmod <- mvar(data = dlist$data,
type = type,
level = level,
lambdaSel = "EBIC",
lags = 1:3,
scale = FALSE,
signInfo = FALSE,
overparameterize = FALSE)
## fitting the same model using the psychNET package
psychsmvar <- psychNET(dat, model = "SMVAR",
lag = 3,
criterion = "EBIC",
signInfo = FALSE,
overparameterize = FALSE)
all.equal(smvarmod$wadj,psychsmvar$fit$wadj)
################################### N>1 models ########################################
#######################################################################################
######################################################################################
########### REPRODUCE EXAMPLE OF A MULTILEVEL VAR FROM THE 'mlVAR' PACKAGE ###########
######################################################################################
## load the 'mlVAR' package
library(mlVAR)
## Simulate data:
Model <- mlVARsim(nPerson = 50, nNode = 3, nTime = 50, lag=1)
# Estimate an MLVAR with correlated random effects using the mlVAR package
mlvarmod <- mlVAR(Model$Data, vars = Model$vars,
idvar = Model$idvar, lags = 1, temporal = "correlated")
# Estimate the same MLVAR using the psychNET package
psychmlvar <- psychNET(Model$Data, model="MLVAR", lag = 1, temporal = "correlated")
# Check if the results are equal
all.equal(mlvarmod$results, psychmlvar$fit$results)
#######################################################################################
########### REPRODUCE EXAMPLE OF A GGVAR VAR FROM THE 'SparseTSCGM' PACKAGE ###########
#######################################################################################
## load the 'SparseTSCGM' package
library(SparseTSCGM)
## Simulate data:
seed = 321
datas <- sim.data(model="ar1", time=10,n.obs=10, n.var=5,seed=seed,
prob0=0.35, network="random")
data.fit <- datas$data1
# Estimate a group graphical VAR (also known as time series chain graphical model)
# using the SparseTSCGM package
ggvarmod <- sparse.tscgm(data=data.fit,
lam1=NULL, lam2=NULL, nlambda=NULL,
model="ar1", penalty="scad",optimality="bic",
control=list(maxit.out = 10, maxit.in = 100))
# Estimate the same model using the psychNET package
psychggvar <- psychNET(data.fit, model="GGVAR", lag=1, penalty="SCAD", criterion="BIC",
control=list(maxit.out = 10, maxit.in = 100))
# Check if the results are equal
all.equal(ggvarmod$theta, psychggvar$fit$theta, check.attributes = FALSE)
all.equal(ggvarmod$gamma, psychggvar$fit$gamma, check.attributes = FALSE)
# }
Run the code above in your browser using DataLab