-
Notifications
You must be signed in to change notification settings - Fork 3
/
ResidualNetwork.R
162 lines (134 loc) · 4.78 KB
/
ResidualNetwork.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env Rscript
library(argparse)
x <- c("comprehenr", "mgm", "qgraph", "bootnet", "OpenMx", "bootnet", "argparse")
new.packages <- x[!(x %in% installed.packages()[, "Package"])]
if (length(new.packages)) {
install.packages(new.packages, repos = "http://cran.us.r-project.org")
}
lapply(x, require, character.only = TRUE)
residual_network <- function(args) {
################## --------------------------------------- ##################
################## --------------- Network --------------- ##################
################## --------------------------------------- ##################
data <- read.csv(file = args$file)
var_types <- rep(c("g"), each = 28)
var_levels <- rep(1, 28)
var_groups <- list(
"YMRS" = c(1:11),
"HDRS" = c(12:28)
)
var_labels <- c(
c(to_vec(for (i in c(1:11)) paste("Y", as.character(i), sep = ""))),
c(to_vec(for (i in c(1:17)) paste("H", as.character(i), sep = "")))
)
ymrs_item_names <- c(
"Elevated mood", "Increased motor activity-energy", "Sexual interest", "Sleep",
"Irritability", "Speech (rate and amount)", "Language-thought disorder",
"Content", "Disruptive-aggressive behavior", "Appearance", "Insight_y"
)
hdrs_item_names <- c(
"Depressed mood", "Feelings of guilt", "Suicide",
"Early insomnia", "Middle insomnia",
"Late insomnia", "Work and activities",
"Retardation", "Agitation", "Anxiety psychic", "Anxiety somatic",
"Somatic symptoms gastrointestinal", "General somatic symptoms",
"Genital symptoms", "Hypochondriasis", "Loss of weight", "Insight_h"
)
var_names <- c(ymrs_item_names, hdrs_item_names)
group_cols <- c("#DC2F02", "#0077B6") #c("#D00000", "#00B4D8")
data_n <- list(
"data" = NULL,
"type" = NULL,
"level" = NULL,
"names" = NULL,
"labels" = NULL,
"grouplabels" = NULL
)
# Fill in
data_n$data <- as.matrix(data)
data_n$type <- var_types
data_n$level <- var_levels
data_n$labels <- var_labels
data_n$grouplabels <- var_groups
# Fit model
fit <- mgm(
data = data_n$data,
type = data_n$type,
level = data_n$level,
lambdaSel = "EBIC",
lambdaGam = 0.25,
scale = TRUE
)
# Compute Predictability
Pred_cov <- predict(fit, data_n$data)
Pred_cov$errors
# predictability estimates
pie <- as.numeric(as.character(Pred_cov$errors[, 3]))
mean(pie)
# Plot Network
graph_plot <- qgraph(fit$pairwise$wadj,
vsize = 5,
layout = "spring",
color = group_cols,
border.width = 1,
border.color = "black",
groups = var_groups,
nodeNames = var_names,
labels = var_labels,
legend = FALSE,
lcolor = c(rep("white", 28)),
cut = 0,
pie = pie
)
pdf(
file = file.path(dirname(args$file), "residual_network.pdf"),
width = 10, height = 10, pointsize = 18
)
plot(graph_plot)
dev.off()
wadj <- as.data.frame(fit$pairwise$wadj)
rownames(wadj) <- var_names
colnames(wadj) <- var_labels
write.csv(wadj, file.path(dirname(args$file), "wadj.csv"), row.names = TRUE)
############ --------------------------------------------------- ############
############ --------- Re-estimate network via bootnet --------- ############
############ --------------------------------------------------- ############
colnames(data_n$data) <- var_labels
# Then re-estimate all networks via bootnet
n <- estimateNetwork(
data = data_n$data,
type = data_n$type,
level = data_n$level,
default = "mgm",
criterion = "EBIC",
tuning = 0.25
)
### check if bootnet results match mgm estimation results, using absolute values
### for estimateNetwork because this is the way pairwise$wadj is encoded in mgm
# 1, good to go
stopifnot(cor(vech(fit$pairwise$wadj), vech(abs(n$graph))) == 1)
# Set bootstrap number
nB <- 500
# Bootstrap:
bs <- bootnet(n, nBoots = nB, nCores = 8)
pdf(file = file.path(dirname(args$file), "bs.pdf"), width = 6, height = 6)
plot(bs, order = "sample", plot = "area", labels = FALSE)
dev.off()
pdf(file.path(dirname(args$file), "bs_diff.pdf"), width = 4, height = 4)
plot(bs,
order = "sample", plot = "difference", onlyNonZero = TRUE,
labels = FALSE
)
dev.off()
}
# Parser
parser <- ArgumentParser(description = "MGM Residuals")
parser$add_argument("-f", "--file",
type = "character", dest = "file",
help = "Provide path to residuals file",
required = TRUE
)
args <- parser$parse_args()
residual_network(args = args)
# use:
# Rscript ResidualNetwork.R -f runs/<best_model>/residuals.csv