#### EXAMPLE 1: FIGURES 1 - 3 #### # Load packages library("mvtnorm") library("dplyr") library("magrittr") library("qgraph") library("IsingSampler") library("IsingFit") # Function to estimate 3 networks on a subset of data compNets <- function(n,Data){ library("IsingSampler") library("IsingFit") # Load function for low-rank approximation source("http://sachaepskamp.com/files/MarsmanFun.R") source("http://sachaepskamp.com/files/Marsmancodes.R") Data <- Data[1:n,] Data[Data==-1] <- 0 Res <- EstimateIsing(Data,method = "uni") ResLLTrans <- LinTransform(Res$graph,Res$thresholds) ResIFit <- IsingFit(Data, plot=FALSE) ResIFtrans <- LinTransform(ResIFit$weiadj,ResIFit$thresholds) MarsmanRes <- Marsman(Data, 2, nIter = 100000)$graph return(list(ll = ResLLTrans, lasso = ResIFtrans, marsman = MarsmanRes )) } # True network structures library("igraph") set.seed(1) # Curie-Weiss model CW <- matrix(0.2,10,10) # Sparse network Sparse <- 0.2*as.matrix(get.adjacency(erdos.renyi.game(10,0.2,directed = FALSE))) # Plot both (Figure 1) pdf("Figure1.pdf",height=5,width=10) layout(t(1:2)) qgraph(CW,layout = "circle",maximum = 0.3, title="Network 1: Curie-Weiss") qgraph(Sparse,layout = "circle",maximum = 0.3, title="Network 2: Sparse") dev.off() # Simulate data Curie-Weiss model Data <- IsingSampler(5000,graph=CW,responses=c(-1,1),thresholds = rep(0,ncol(CW))) # Sample sizes: sampleSizes <- c(100,250,500,1000,2500,5000) # Estimate networks: ResultsCW <- lapply(seq_along(sampleSizes), function(i){ compNets(sampleSizes[i],Data) }) # Maximums: maxEdge <- function(x) max(abs(x[upper.tri(x,diag=FALSE)])) Max <- list( ll = max(sapply(lapply(ResultsCW, '[[', 'll'), function(x) maxEdge(x$graph))), lasso = max(sapply(lapply(ResultsCW, '[[', 'lasso'), function(x) maxEdge(x$graph))), marsman = max(sapply(lapply(ResultsCW, '[[', 'marsman'), maxEdge)) ) # Figure 2 pdf("Figure2.pdf",height=5,width=10) layout(matrix(c( 1,4,7,10,13,16, 2,5,8,11,14,17, 3,6,9,12,15,18 ),3,byrow=TRUE)) for (i in seq_along(sampleSizes)){ qgraph(ResultsCW[[i]]$ll$graph,weighted=TRUE,title=sprintf("Unregularized (n = %s)",sampleSizes[i])) qgraph(ResultsCW[[i]]$lasso$graph,weighted=TRUE,title=sprintf("LASSO (n = %s)",sampleSizes[i])) qgraph(ResultsCW[[i]]$marsman,weighted=TRUE,title=sprintf("Rank-2 (n = %s)",sampleSizes[i]),diag=FALSE) } dev.off() # Simulate data sparse model Data <- IsingSampler(5000,graph=Sparse ,responses=c(-1,1),thresholds = rep(0,ncol(Sparse))) # Figure 3 sampleSizes <- c(100,250,500,1000,2500,5000) # Estimate networks: ResultsSparse <- lapply(seq_along(sampleSizes), function(i){ compNets(sampleSizes[i],Data) }) # Maximums: maxEdge <- function(x) max(abs(x[upper.tri(x,diag=FALSE)])) Max <- list( ll = max(sapply(lapply(ResultsSparse, '[[', 'll'), function(x) maxEdge(x$graph))), lasso = max(sapply(lapply(ResultsSparse, '[[', 'lasso'), function(x) maxEdge(x$graph))), marsman = max(sapply(lapply(ResultsSparse, '[[', 'marsman'), maxEdge)) ) # Figure 3 pdf("Figure3.pdf",height=5,width=10) layout(matrix(c( 1,4,7,10,13,16, 2,5,8,11,14,17, 3,6,9,12,15,18 ),3,byrow=TRUE)) for (i in seq_along(sampleSizes)){ qgraph(ResultsSparse[[i]]$ll$graph,weighted=TRUE,title=sprintf("Unregularized (n = %s)",sampleSizes[i])) qgraph(ResultsSparse[[i]]$lasso$graph,weighted=TRUE,title=sprintf("LASSO (n = %s)",sampleSizes[i])) qgraph(ResultsSparse[[i]]$marsman,weighted=TRUE,title=sprintf("Rank-2 (n = %s)",sampleSizes[i]),diag=FALSE) } dev.off() #### EXAMPLE 2: FIGURE 5 + SUPPLEMENTARY #### # Load packages: library("mvtnorm") library("dplyr") library("magrittr") library("qgraph") # Load function for low-rank approximation: source("http://sachaepskamp.com/files/MarsmanFun.R") source("http://sachaepskamp.com/files/Marsmancodes.R") ### Latent-variable model example ### # Discrimination matrix: A <- structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1.1, -1.1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.1, -1.1, 0, 0, 0, 0, 0, 0, 0, 0, 0.75, 0, 0, 0, 0, 0, 0, 0, 0, 0.75, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.75, 0, 0, 0, 0, 0, 0, 0, 0, 0.75, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.75, 0, 0, 0, 0, 0, 0, 0, 0, 0.75, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.75, 0, 0, 0, 0, 0, 0, 0, 0, 0.75 ), .Dim = c(19L, 8L)) # Latent variance-covariance matrix: Psi <- structure(c(1, 0.55, 0, 0, 0, 0, 0, 0, 0.55, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1), .Dim = c(8L, 8L)) # Function to simulate data: simData <- function(n, A, Psi){ nFact <- ncol(A) nItem <- nrow(A) # Simulate theta: Theta <- rmvnorm(n,rep(0,nFact),Psi) # Simulate data: Data <- 1*(matrix(runif(n*nItem),n,nItem) < exp(Theta %*% t(A)) / (1 + exp(Theta %*% t(A)))) return(Data) } # List to store results: Results <- list() # Simulate 100.000 rows: set.seed(3) Data1 <- simData(10000000, A, Psi) # Estimate using IsingSampler: library("IsingSampler") Results$true <- EstimateIsing(Data1, method = "ll") # Second data (N = 1000): set.seed(3) Data2 <- simData(1000, A, Psi) # Estimate using IsingFit: library("IsingFit") Results$IsingFit <- IsingFit(Data2,plot = FALSE) # IsingFit with OR rule: Results$IsingFitOR <- IsingFit(Data2,plot = FALSE,AND = FALSE) # IsingFit with BIC: Results$IsingFitBIC <- IsingFit(Data2,plot = FALSE, gamma = 0) # Estimate using rank-2 approximation: Results$lowrank <- Marsman(Data2, 2)$graph # Estimate using max likelihood: Results$loglin <- EstimateIsing(Data2, method = "ll") # Estimate using elasticIsing library("elasticIsing") Results$elastic <- elasticIsing(Data2) ### Plot results ### # Layout of graph Layout <- structure(c(-0.91700922593243, -0.71607690882459, -1, -0.841970887068207, -0.506700378019609, -0.582211268833235, -0.369952210868241, -0.195488670971024, -0.562974697146571, -0.206747554869184, 1, 0.730922969999289, 0.508551176680195, 0.997018453214546, 0.975224165663054, 0.361933177193966, 0.581268889708378, 0.275354076567066, 0.603799511172853, 0.0163223231591647, 0.265137401568571, 0.464523298446456, 0.831639167414529, 1, 0.644030028900333, 0.09473424352082, 0.776176800606203, -0.178569692561694, 0.411366413888367, -0.218782058787729, -0.684466225777628, -1, -0.588461855039565, 0.227595353053499, -0.301670310592772, 0.231174642779848, -0.639366548957921, -0.114897738179063), .Dim = c(19L, 2L)) # Labels labels <- c( 1:10, 1:9 ) # Groups groups <- list( Dysthymia = 1:10, GAD = 11:19 ) # Larger sample size (N = 5000) # List to store results: Results2 <- list() # Second data (N = 1000): set.seed(3) Data3 <- simData(5000, A, Psi) # Estimate using IsingFit: library("IsingFit") Results2$IsingFit <- IsingFit(Data3,plot = FALSE) # IsingFit with OR rule: Results2$IsingFitOR <- IsingFit(Data3,plot = FALSE,AND = FALSE) # IsingFit with BIC: Results2$IsingFitBIC <- IsingFit(Data3,plot = FALSE, gamma = 0) # Estimate using rank-2 approximation: Results2$lowrank <- Marsman(Data3, 2)$graph # Estimate using max likelihood: Results2$loglin <- EstimateIsing(Data3, method = "ll") # Estimate using elasticIsing Results2$elastic <- elasticIsing(Data3) # Store all graphs in the PDF: library("qgraph") pdf("EstimatedNetworks.pdf", height=5 ,width=5) qgraph(Results$true$graph, layout = Layout, title = "Loglinear model (N = 10,000,000)", vsize = 6, labels = labels, groups = groups, legend = FALSE, pastel = TRUE, mar = c(2,2,2,2)) qgraph(Results$loglin$graph, layout = Layout, title = "Loglinear model (N = 1,000)", vsize = 6, labels = labels, groups = groups, legend = FALSE, pastel = TRUE, mar = c(2,2,2,2)) qgraph(Results$IsingFit$weiadj, layout = Layout, title = "IsingFit (N = 1,000)", vsize = 6, labels = labels, groups = groups, legend = FALSE, pastel = TRUE, mar = c(2,2,2,2)) qgraph(Results$IsingFit$weiadj, layout = Layout, title = "IsingFit (N = 1,000), gamma = 0.25 & AND-rule", vsize = 6, labels = labels, groups = groups, legend = FALSE, pastel = TRUE, mar = c(2,2,2,2)) qgraph(Results$IsingFitOR$weiadj, layout = Layout, title = "IsingFit (N = 1,000), gamma = 0.25 & OR-rule", vsize = 6, labels = labels, groups = groups, legend = FALSE, pastel = TRUE, mar = c(2,2,2,2)) qgraph(Results$IsingFitBIC$weiadj, layout = Layout, title = "IsingFit (N = 1,000), gamma = 0 & AND-rule", vsize = 6, labels = labels, groups = groups, legend = FALSE, pastel = TRUE, mar = c(2,2,2,2)) qgraph(Results$lowrank, layout = Layout,diag=FALSE, title = "Rank 2 approximation (N = 1,000)", vsize = 6, labels = labels, groups = groups, legend = FALSE, pastel = TRUE, mar = c(2,2,2,2)) plot(Results$elastic, layout = Layout,diag=FALSE, title = "Elastic-net (N = 1,000)", vsize = 6, labels = labels, groups = groups, legend = FALSE, pastel = TRUE, mar = c(2,2,2,2)) qgraph(Results2$loglin$graph, layout = Layout, title = "Loglinear model (N = 5,000)", vsize = 6, labels = labels, groups = groups, legend = FALSE, pastel = TRUE, mar = c(2,2,2,2)) qgraph(Results2$IsingFit$weiadj, layout = Layout, title = "IsingFit (N = 5,000)", vsize = 6, labels = labels, groups = groups, legend = FALSE, pastel = TRUE, mar = c(2,2,2,2)) qgraph(Results2$IsingFit$weiadj, layout = Layout, title = "IsingFit (N = 5,000), gamma = 0.25 & AND-rule", vsize = 6, labels = labels, groups = groups, legend = FALSE, pastel = TRUE, mar = c(2,2,2,2)) qgraph(Results2$IsingFitOR$weiadj, layout = Layout, title = "IsingFit (N = 5,000), gamma = 0.25 & OR-rule", vsize = 6, labels = labels, groups = groups, legend = FALSE, pastel = TRUE, mar = c(2,2,2,2)) qgraph(Results2$IsingFitBIC$weiadj, layout = Layout, title = "IsingFit (N = 5,000), gamma = 0 & AND-rule", vsize = 6, labels = labels, groups = groups, legend = FALSE, pastel = TRUE, mar = c(2,2,2,2)) qgraph(Results2$lowrank, layout = Layout,diag=FALSE, title = "Rank 2 approximation (N = 5,000)", vsize = 6, labels = labels, groups = groups, legend = FALSE, pastel = TRUE, mar = c(2,2,2,2)) plot(Results2$elastic, layout = Layout,diag=FALSE, title = "Elastic-net (N = 5,000)", vsize = 6, labels = labels, groups = groups, legend = FALSE, pastel = TRUE, mar = c(2,2,2,2)) dev.off()