-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNetwork_analysis.R
127 lines (96 loc) · 4.49 KB
/
Network_analysis.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
#########################################################
# Practical Introduction Psychological Network Analysis
#########################################################
# Install packages:
library("bootnet")
library("qgraph")
library("psych")
# Load data:
data("bfi")
head(bfi)
bfi <- na.omit(bfi[,1:25])
# Some items need to be recoded:
bfi$A1 <- 7 - bfi$A1
bfi$C4 <- 7 - bfi$C4
bfi$C5 <- 7 - bfi$C5
bfi$E1 <- 7 - bfi$E1
bfi$E2 <- 7 - bfi$E2
bfi$O2 <- 7 - bfi$O2
bfi$O5 <- 7 - bfi$O5
# Sum-scores:
bfi_traits <- data.frame(Agreeableness = rowSums(bfi[,1:5]),
Conscientiousness = rowSums(bfi[,6:10]),
Extraversion = rowSums(bfi[,11:15]),
Neuroticism = rowSums(bfi[,16:20]),
Openness = rowSums(bfi[,21:25]))
Traits <- c("Agreeableness", "Conscientiousness", "Extraversion", "Neuroticism", "Openness")
#########################################################
# Exercise 1: Estimating a partial correlation network
#########################################################
??estimateNetwork
Network <- estimateNetwork(bfi_traits, default = "...")
# Show edge weights:
Network$graph
#########################################################
# Exercise 2: Plotting a network model
#########################################################
par(mfrow=c(1,2))
qgraph(Network$graph, layout = "...", theme = "colorblind")
qgraph(Network$graph, layout = "...", theme = "colorblind")
# Extra plot options:
par(mfrow=c(1,3))
qgraph(Network$graph, layout = "circle", edge.labels = TRUE, theme = "colorblind", edge.label.cex = 2)
qgraph(Network$graph, layout = "circle", vsize = 15, theme = "colorblind")
qgraph(Network$graph, layout = "circle", vsize = 20, labels = Traits, theme = "colorblind")
#########################################################
# Exercise 3: Estimate and plot a partial correlation
# network on all 25 items
#########################################################
# Estimate a network with bootnet:
Network_25Items <- estimateNetwork(bfi, default = "...")
# Plotting a network with qgraph:
qgraph(..., layout = "...", theme = "colorblind")
#########################################################
# EXTRA: Estimate and plot a partial correlation
# network on all 25 items
#########################################################
# Form item clusters:
Traits <- rep(c('Agreeableness','Conscientiousness',
'Extraversion','Neuroticism','Opennness'), each = 5)
qgraph(Network_25Items$graph, layout = "spring", theme = "colorblind",
groups = Traits, legend.cex = 1, vsize = 5)
#########################################################
# Exercise 4: Estimate (and plot) a network with model selection
#########################################################
Network_regularized <- estimateNetwork(bfi, default = "...")
# Plot network:
qgraph(Network_regularized$graph, layout = "spring", theme = "colorblind",
groups = Traits)
#########################################################
# Exercise 5: Compare partial correlation network with
# a network with model selection
#########################################################
plot_network <- qgraph(Network_25Items$graph, layout = "spring")
plot_network_regularized <- qgraph(Network_regularized$graph, layout = "spring")
Layout <- averageLayout(plot_network, plot_network_regularized)
par(mfrow=c(1,2))
qgraph(Network_25Items$graph, layout = Layout, title = "Partial Correlation", theme = "colorblind", groups = Traits)
qgraph(Network_regularized$graph, layout = Layout, title = "Model selection", theme = "colorblind", groups = Traits)
#########################################################
# Exercise 6: Assess Stability of your network
#########################################################
# Assess stability:
Stability <- bootnet(bfi, default = "...", nBoots = ...)
plot(..., order = "...")
#########################################################
# Exercise 7: Investigate the node strength, closeness, and betweenness
#########################################################
# Obtain centrality plot
??centralityPlot
centralityPlot(Network_regularized, include = "...")
#########################################################
# Exercise 8: Assess stability for the centrality measures
#########################################################
??bootnet
Boots2 <- bootnet()
plot()