I am using principal() function from psych package to replicate SPSS principal component analysis results in R. (as recommended in: https://stats.stackexchange.com/questions/612/is-pca-followed-by-a-rotation-e-g-varimax-still-pca)
I'm using the code below:
com<-principal(ws, nfactors = 1, residuals = FALSE,rotate="varimax",n.obs=NA, covar=TRUE,
scores=TRUE,missing=TRUE,impute="mean",oblique.scores=TRUE,
method="correlation")
But I'm getting the following error:
Error in solve.default(model, r) :
Lapack routine dgesv: system is exactly singular: U[104,104] = 0
In addition: Warning message:
In cor.smooth(r) : Matrix was not positive definite, smoothing was done
Any advice would be much appreciated. I'm attaching a sample of my data (by dput(ws)) below. Thank you so much!
structure(list(hv001 = c(906, 3101, 4001, 6801, 1502, 3508, 4301,
2205, 508, 901, 906, 2304, 4913, 3803, 5704, 2902, 4701, 2303,
3602, 5905, 4805, 1405, 6301, 4007, 904, 302, 7003, 1602, 7107,
4801, 1803, 3403, 6904, 3002, 509, 6904, 1407, 5803, 5802, 4504,
405, 502, 7207, 2706, 2703, 4007, 3403, 703, 2304, 4806, 403,
502, 5607, 902, 2707, 2709, 6402, 6902, 1902, 7501, 2202, 2201,
7205, 4901, 2204, 3504, 5608, 3202, 3505, 2202, 5608, 2306, 7402,
7109, 7303, 5904, 3602, 6102, 101, 7302, 906, 3506, 6003, 6302,
909, 4801, 4806, 3104, 5708, 6802, 404, 4903, 5609, 2101, 3502,
4002, 1602, 7206, 6903, 7103), hv002 = c(26, 372, 147, 215, 23,
48, 130, 17, 48, 214, 93, 15, 122, 27, 172, 5, 53, 100, 177,
341, 279, 42, 47, 10, 15, 47, 90, 106, 218, 110, 95, 86, 69,
77, 47, 49, 43, 55, 324, 88, 120, 177, 112, 462, 347, 3, 69,
36, 16, 561, 109, 322, 28, 47, 123, 8, 174, 66, 101, 55, 212,
201, 57, 231, 53, 128, 211, 136, 102, 246, 154, 5, 59, 231, 263,
108, 137, 35, 98, 165, 27, 205, 8, 70, 147, 189, 417, 71, 23,
56, 139, 302, 106, 83, 24, 12, 56, 95, 4, 198), hv005 = c(346129,
1051441, 458555, 513316, 2337718, 2526226, 1956333, 420415, 2337718,
106202, 346129, 420415, 1181939, 1956333, 852471, 420415, 1956333,
420415, 1956333, 811088, 1181939, 1042611, 513316, 1956333, 346129,
1042611, 595684, 2337718, 249256, 1181939, 2526226, 671551, 595684,
1566584, 2337718, 595684, 1042611, 852471, 264004, 1956333, 2337718,
537817, 1238001, 1051441, 1051441, 1956333, 671551, 1042611,
420415, 1181939, 2337718, 537817, 852471, 106202, 1051441, 1051441,
513316, 595684, 2526226, 513316, 162660, 162660, 1238001, 333940,
420415, 671551, 852471, 2526226, 2526226, 162660, 852471, 420415,
214934, 1238001, 595684, 811088, 1956333, 811088, 346129, 595684,
346129, 2526226, 811088, 513316, 346129, 1181939, 1181939, 1566584,
852471, 513316, 2337718, 333940, 852471, 1566584, 671551, 458555,
2337718, 1238001, 595684, 249256), hv025 = c(2, 1, 1, 2, 2, 2,
2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 1, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 1, 2, 1, 1, 2, 1, 2,
2, 2, 2, 1, 2, 1, 1, 1, 2, 2, 2, 2, 1, 1, 2, 1, 2, 1, 2, 2, 2,
1, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 1, 2, 2, 1, 1, 2, 2, 2, 1), hv009 = c(7, 3, 4, 5, 4, 3, 4,
2, 5, 2, 8, 3, 4, 4, 2, 3, 7, 4, 3, 5, 9, 5, 6, 1, 3, 5, 7, 5,
6, 2, 4, 4, 4, 3, 5, 5, 3, 5, 3, 4, 4, 5, 3, 5, 4, 4, 5, 9, 3,
3, 4, 3, 4, 3, 5, 4, 9, 8, 6, 1, 5, 4, 6, 1, 8, 3, 3, 5, 2, 6,
5, 2, 3, 1, 7, 3, 7, 3, 7, 7, 3, 4, 5, 5, 3, 3, 4, 5, 8, 2, 3,
5, 2, 7, 2, 2, 7, 10, 6, 5), hv012 = c(7, 3, 4, 5, 4, 3, 4, 2,
5, 2, 8, 3, 4, 4, 2, 3, 3, 4, 3, 5, 9, 5, 6, 1, 3, 5, 7, 5, 6,
2, 4, 4, 4, 3, 5, 5, 3, 5, 3, 4, 4, 5, 3, 5, 4, 4, 5, 9, 3, 3,
4, 3, 4, 3, 5, 2, 9, 8, 6, 1, 5, 4, 6, 1, 8, 3, 3, 5, 2, 6, 5,
2, 3, 1, 7, 3, 7, 3, 7, 7, 3, 4, 5, 5, 3, 3, 4, 5, 6, 2, 3, 5,
2, 7, 2, 2, 7, 10, 6, 4), hv013 = c(7, 3, 4, 4, 4, 3, 4, 2, 5,
2, 8, 3, 4, 4, 2, 3, 7, 4, 3, 5, 9, 5, 4, 1, 3, 3, 7, 5, 6, 2,
0, 4, 3, 3, 5, 5, 3, 5, 3, 4, 4, 3, 3, 5, 4, 4, 5, 9, 3, 3, 4,
3, 4, 3, 5, 4, 9, 8, 6, 1, 4, 4, 6, 1, 8, 3, 3, 5, 2, 6, 5, 2,
3, 1, 5, 3, 7, 3, 7, 7, 3, 4, 5, 5, 3, 2, 4, 5, 8, 2, 3, 3, 1,
7, 2, 2, 7, 10, 6, 5), hv201 = c(44, 12, 12, 43, 21, 41, 13,
43, 21, 12, 44, 12, 13, 31, 21, 13, 12, 12, 11, 31, 21, 21, 12,
13, 13, 43, 41, 21, 21, 12, 21, 21, 13, 12, 12, 13, 21, 12, 21,
13, 21, 21, 21, 31, 11, 13, 21, 12, 12, 21, 21, 21, 21, 44, 13,
21, 44, 13, 21, 13, 12, 12, 21, 11, 12, 13, 13, 21, 13, 12, 43,
13, 12, 21, 13, 13, 13, 13, 12, 43, 44, 13, 42, 13, 13, 11, 21,
13, 21, 43, 12, 11, 32, 12, 11, 13, 21, 21, 44, 21), hv204 = c(15,
996, 996, 20, 996, 5, 15, 30, 996, 996, 30, 996, 5, 25, 5, 20,
996, 996, 996, 10, 996, 10, 996, 20, 60, 60, 30, 996, 996, 996,
30, 10, 15, 996, 996, 10, 996, 996, 996, 996, 996, 996, 5, 996,
996, 10, 996, 996, 996, 10, 996, 996, 996, 20, 10, 15, 20, 5,
10, 10, 996, 996, 996, 996, 996, 996, 5, 996, 5, 996, 90, 5,
996, 996, 5, 996, 15, 20, 996, 15, 30, 9, 30, 5, 10, 996, 996,
20, 996, 15, 996, 996, 10, 996, 996, 5, 996, 996, 5, 996), hv205 = c(23,
12, 12, 22, 31, 22, 31, 31, 12, 31, 31, 12, 23, 23, 31, 12, 13,
13, 23, 12, 13, 31, 22, 13, 31, 23, 31, 13, 31, 11, 31, 12, 31,
23, 12, 31, 31, 23, 31, 12, 31, 12, 31, 12, 12, 12, 13, 23, 41,
12, 12, 12, 13, 23, 12, 14, 31, 22, 31, 22, 12, 12, 31, 12, 13,
12, 31, 12, 12, 12, 31, 22, 13, 31, 31, 31, 22, 13, 13, 31, 23,
12, 31, 31, 13, 12, 12, 31, 13, 31, 12, 12, 31, 23, 12, 12, 31,
12, 22, 12)), .Names = c("hv001", "hv002", "hv005", "hv025",
"hv009", "hv012", "hv013", "hv201", "hv204", "hv205"), row.names = c(10678L,
3179L, 4323L, 8790L, 835L, 3936L, 4789L, 1696L, 6299L, 10495L,
10703L, 1909L, 5863L, 4156L, 7311L, 2912L, 5073L, 1857L, 3991L,
7840L, 5254L, 690L, 8477L, 4520L, 10597L, 3056L, 9158L, 889L,
9572L, 5096L, 1178L, 3598L, 9040L, 2989L, 6336L, 9030L, 768L,
7583L, 7539L, 5005L, 4713L, 6064L, 10013L, 2486L, 2392L, 4531L,
3596L, 9322L, 1910L, 5317L, 4631L, 6080L, 7070L, 10542L, 2501L,
2594L, 8582L, 8957L, 1223L, 10372L, 1587L, 1547L, 9960L, 5458L,
1667L, 3749L, 7100L, 3355L, 3783L, 1591L, 7087L, 1991L, 10264L,
9655L, 10147L, 7781L, 3982L, 8396L, 120L, 10106L, 10679L, 3841L,
8033L, 8519L, 10789L, 5111L, 5313L, 3309L, 7464L, 8835L, 4668L,
5526L, 7117L, 1537L, 3698L, 4355L, 916L, 10006L, 8995L, 9434L
), class = "data.frame")
If you leave the method default (which is "regression") you should be able to get the PCA results. However, I doubt if you will get the same results as SPSS (I have seen discrepancy between SPSS and R with "varimax" rotation results).
I went through the code. It seems that scores cannot be passed as an argument to the function so please change the value of the score to FALSE.
Try the code below....
res = principal(ws, nfactors = 1, residuals = F, rotate = "varimax", n.obs = NA, covar = T, scores = F, missing = T, impute = "mean", oblique.scores = T, method = "correlation")
I think it will help to continue your work.
Related
I am using ggpubr to combine multiple graphs in a single plot, but cannot seem to correctly generate one graph with the title that I would like. I would like the title to say "Customized legend," given that it is a common legend for both graphs. Does anybody know how I can do this?
Here is my data:
data1 = data.frame(var1 = c(1,
1,
1,
1,
2,
2,
2,
2,
3,
3,
3,
3,
4,
4,
4,
4,
5,
5,
5,
5,
6,
6,
6,
6,
7,
7,
7,
7,
8,
8,
8,
8,
9,
9,
9,
9,
10,
10,
10,
10,
11,
11,
11,
11,
12,
12,
12,
12,
13,
13,
13,
13,
14,
14,
14,
14,
15,
15,
15,
15,
16,
16,
16,
16,
17,
17,
17,
17,
18,
18,
18,
18,
19,
19,
19,
19,
20,
20,
20,
20,
21,
21,
21,
21,
22,
22,
22,
22,
23,
23,
23,
23,
24,
24,
24,
24,
25,
25,
25,
25,
26,
26,
26,
26,
27,
27,
27,
27,
28,
28,
28,
28,
29,
29,
29,
29,
30,
30,
30,
30,
31,
31,
31,
31,
32,
32,
32,
32,
33,
33,
33,
33),
var2 = c(1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4),
var3 = c(113,
89,
99,
41,
72,
64,
39,
139,
135,
17,
3,
135,
63,
126,
34,
87,
84,
125,
123,
18,
115,
11,
68,
85,
48,
95,
56,
129,
41,
78,
82,
122,
124,
4,
60,
132,
67,
128,
46,
79,
110,
88,
19,
88,
88,
126,
30,
11,
52,
66,
15,
52,
6,
74,
14,
101,
88,
70,
58,
20,
104,
76,
134,
23,
40,
1,
47,
25,
49,
110,
96,
100,
106,
26,
93,
19,
87,
41,
13,
40,
63,
87,
137,
105,
89,
95,
24,
49,
112,
92,
45,
105,
112,
105,
114,
129,
84,
33,
95,
95,
15,
90,
1,
62,
20,
7,
18,
96,
4,
71,
42,
94,
45,
102,
55,
98,
124,
80,
76,
97,
41,
31,
25,
21,
135,
138,
121,
93,
17,
13,
49,
26))
data2 <- data.frame(var1a = c(1,
1,
1,
1,
2,
2,
2,
2,
3,
3,
3,
3,
4,
4,
4,
4,
5,
5,
5,
5,
6,
6,
6,
6,
7,
7,
7,
7,
8,
8,
8,
8,
9,
9,
9,
9,
10,
10,
10,
10,
11,
11,
11,
11,
12,
12,
12,
12,
13,
13,
13,
13,
14,
14,
14,
14,
15,
15,
15,
15,
16,
16,
16,
16,
17,
17,
17,
17,
18,
18,
18,
18,
19,
19,
19,
19,
20,
20,
20,
20,
21,
21,
21,
21,
22,
22,
22,
22,
23,
23,
23,
23,
24,
24,
24,
24,
25,
25,
25,
25,
26,
26,
26,
26,
27,
27,
27,
27,
28,
28,
28,
28,
29,
29,
29,
29,
30,
30,
30,
30,
31,
31,
31,
31,
32,
32,
32,
32,
33,
33,
33,
33),
var2a = c(1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4,
1,
2,
3,
4),
var3a = c(113,
89,
99,
41,
72,
64,
39,
139,
135,
17,
3,
135,
63,
126,
34,
87,
84,
125,
123,
18,
115,
11,
68,
85,
48,
95,
56,
129,
41,
78,
82,
122,
124,
4,
60,
132,
67,
128,
46,
79,
110,
88,
19,
88,
88,
126,
30,
11,
52,
66,
15,
52,
6,
74,
14,
101,
88,
70,
58,
20,
104,
76,
134,
23,
40,
1,
47,
25,
49,
110,
96,
100,
106,
26,
93,
19,
87,
41,
13,
40,
63,
87,
137,
105,
89,
95,
24,
49,
112,
92,
45,
105,
112,
105,
114,
129,
84,
33,
95,
95,
15,
90,
1,
62,
20,
7,
18,
96,
4,
71,
42,
94,
45,
102,
55,
98,
124,
80,
76,
97,
41,
31,
25,
21,
135,
138,
121,
93,
17,
13,
49,
26))
Here is the code that I am using:
#Open packages
library(ggplot2)
library(ggpubr)
#Set the theme
theme_set(theme_pubr())
#Change necessary columns to factor
data1$var2 <- factor(data1$var2, levels = c(1,2,3,4))
data2$var2a <- factor(data2$var2a, levels = c(1,2,3,4))
#Generate the plots
#Generate plots
plot1 <- ggplot(data1, aes(x = var1, y = var3, group = var2)) +
geom_line(size = 1.5, aes(linetype = var2, color = var2)) +
xlab('x_label') +
ylab('y_label')+
scale_fill_discrete(name = 'customized legend')
plot2 <- ggplot(data2, aes(x = var1a, y = var3a, group = var2a)) +
geom_line(size = 1.5, aes(linetype = var2a, color = var2a)) +
xlab('x_label') +
ylab('y_label')+
scale_fill_discrete(name = 'customized legend')
#Combine both into one picture
fig <- ggarrange(plot1, plot2,
ncol = 2,
nrow = 1,
common.legend = TRUE,
legend = "bottom")
fig
Since you didn't use the fill aesthetic in your ggplot, you should not use scale_fill_discrete. What you need is to set the legend title of linetype and color to "customized legend", since those are the aesthetics that you used.
library(ggplot2)
library(ggpubr)
plot1 <- ggplot(data1, aes(x = var1, y = var3, group = var2)) +
geom_line(size = 1.5, aes(linetype = var2, color = var2)) +
xlab('x_label') +
ylab('y_label') +
labs(linetype = "customized legend", color = "customized legend")
plot2 <- ggplot(data2, aes(x = var1a, y = var3a, group = var2a)) +
geom_line(size = 1.5, aes(linetype = var2a, color = var2a)) +
xlab('x_label') +
ylab('y_label') +
labs(linetype = "customized legend", color = "customized legend")
#Combine both into one picture
ggarrange(plot1, plot2,
ncol = 2,
nrow = 1,
common.legend = TRUE,
legend = "bottom")
I have three different graphs that were produced by igraph package and graph_from_adjacency_matrix function. How can I store all three of these (g1, g2, g3) in one object so that later I can use this object as an input for another function. I want to preserve all the attributes of the graphs.
Here's the structure of the three graphs:
dput(g1)
structure(list(11, FALSE, c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2,
3, 4, 5, 6, 7, 8, 9, 10, 3, 4, 5, 6, 7, 8, 9, 10, 4, 5, 6, 7,
8, 9, 10, 5, 6, 7, 8, 9, 10, 6, 7, 8, 9, 10, 7, 8, 9, 10, 8,
9, 10, 9, 10, 10), c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4,
4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 9),
c(0, 1, 10, 2, 11, 19, 3, 12, 20, 27, 4, 13, 21, 28, 34,
5, 14, 22, 29, 35, 40, 6, 15, 23, 30, 36, 41, 45, 7, 16,
24, 31, 37, 42, 46, 49, 8, 17, 25, 32, 38, 43, 47, 50, 52,
9, 18, 26, 33, 39, 44, 48, 51, 53, 54), c(0, 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), c(0, 0, 1, 3, 6, 10, 15, 21, 28, 36, 45,
55), c(0, 10, 19, 27, 34, 40, 45, 49, 52, 54, 55, 55), list(
c(1, 0, 1), structure(list(), .Names = character(0)),
list(name = c("jpm", "gs", "ms", "bofa", "schwab", "brk",
"wf", "citi", "amex", "spgl", "pnc")), list(wt = c(10000,
3.16222797634994, 10000, 10000, 6.2838498029626, 1.93361060894155,
10000, 10000, 5.84323225364297, 7.44026659903325, 1.31111055012301,
10000, 10000, 4.30459269702548, 2.20457094344212, 3.49673898163627,
3.09239540712491, 3.43107254995375, 10000, 5.64499596383733,
10000, 10000, 3.72116985462354, 2.70273403225818, 2.35839869470134,
10000, 10000, 10000, 1.83130016032325, 1.99399002493476,
1.7644293974645, 1.88708226743269, 7.73257077502946,
10000, 10000, 10000, 10000, 10000, 10000, 10000, 6.94406536133693,
3.32018490900407, 2.0759886748923, 4.11734201102576,
6.193275571549, 2.85404877010956, 10000, 3.01093189825944,
10000, 10000, 7.07193471387249, 10000, 5.19453928016632,
10000, 10000))), <environment>), class = "igraph")
dput(g2)
structure(list(11, FALSE, c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2,
3, 4, 5, 6, 7, 8, 9, 10, 3, 4, 5, 6, 7, 8, 9, 10, 4, 5, 6, 7,
8, 9, 10, 5, 6, 7, 8, 9, 10, 6, 7, 8, 9, 10, 7, 8, 9, 10, 8,
9, 10, 9, 10, 10), c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4,
4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 9),
c(0, 1, 10, 2, 11, 19, 3, 12, 20, 27, 4, 13, 21, 28, 34,
5, 14, 22, 29, 35, 40, 6, 15, 23, 30, 36, 41, 45, 7, 16,
24, 31, 37, 42, 46, 49, 8, 17, 25, 32, 38, 43, 47, 50, 52,
9, 18, 26, 33, 39, 44, 48, 51, 53, 54), c(0, 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), c(0, 0, 1, 3, 6, 10, 15, 21, 28, 36, 45,
55), c(0, 10, 19, 27, 34, 40, 45, 49, 52, 54, 55, 55), list(
c(1, 0, 1), structure(list(), .Names = character(0)),
list(name = c("jpm", "gs", "ms", "bofa", "schwab", "brk",
"wf", "citi", "amex", "spgl", "pnc")), list(wt = c(1.72565213162016,
10000, 10000, 10000, 2.60988018061569, 3.37529546067647,
5.68789870362681, 2.44357606642214, 4.38114633403004,
10000, 10000, 2.49869325166531, 10000, 10000, 4.56956459390346,
3.52409742807134, 2.97961673322383, 3.42809851201881,
3.15481552530237, 7.32112737506667, 10000, 7.0852416616783,
3.99494740752879, 2.65955867194822, 10000, 10000, 10000,
10000, 5.73934520134914, 1.80740569361977, 1.5783164909029,
2.84567417160359, 10000, 10000, 10000, 10000, 5.30260309989479,
10000, 10000, 10000, 10000, 7.06161817483184, 6.9222112543713,
4.63691541477454, 3.48797079504012, 6.38029319494032,
10000, 2.48116694808653, 10000, 2.12352867446693, 3.04335319291233,
10000, 10000, 5.22409020671212, 10000))), <environment>), class = "igraph")
dput(g3)
structure(list(11, FALSE, c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2,
3, 4, 5, 6, 7, 8, 9, 10, 3, 4, 5, 6, 7, 8, 9, 10, 4, 5, 6, 7,
8, 9, 10, 5, 6, 7, 8, 9, 10, 6, 7, 8, 9, 10, 7, 8, 9, 10, 8,
9, 10, 9, 10, 10), c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4,
4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 9),
c(0, 1, 10, 2, 11, 19, 3, 12, 20, 27, 4, 13, 21, 28, 34,
5, 14, 22, 29, 35, 40, 6, 15, 23, 30, 36, 41, 45, 7, 16,
24, 31, 37, 42, 46, 49, 8, 17, 25, 32, 38, 43, 47, 50, 52,
9, 18, 26, 33, 39, 44, 48, 51, 53, 54), c(0, 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), c(0, 0, 1, 3, 6, 10, 15, 21, 28, 36, 45,
55), c(0, 10, 19, 27, 34, 40, 45, 49, 52, 54, 55, 55), list(
c(1, 0, 1), structure(list(), .Names = character(0)),
list(name = c("jpm", "gs", "ms", "bofa", "schwab", "brk",
"wf", "citi", "amex", "spgl", "pnc")), list(wt = c(10000,
4.14221420842333, 2.69857209553848, 5.77115055524614,
1.95672007809809, 2.98690863617922, 1.92161847347613,
2.34571882319417, 10000, 10000, 1.97201563662035, 5.4078452590091,
10000, 6.85345421615961, 3.51453278996926, 10000, 10000,
2.08964950396744, 10000, 2.78868220464486, 10000, 3.41857460835555,
4.57693796722718, 1.96044036389548, 10000, 6.69365386837721,
2.61525679780493, 7.34195637377719, 2.57334862699097,
3.54317409176484, 10000, 2.33889236077345, 2.49271973693215,
5.47858809426897, 10000, 5.25238753114071, 10000, 10000,
10000, 10000, 10000, 2.68400716970295, 2.49075030691088,
2.59993683645561, 10000, 10000, 2.49345951327313, 5.7338881554994,
1.73687483250752, 4.24032760636804, 3.11756167665892,
5.07827243244947, 10000, 1.69643890905687, 10000))),
<environment>), class = "igraph")
I think you can try list like below
g <- list(g1,g2,g3)
I am trying to get the marginal effects from a multinomial model derived from the mlogit package but it shows an error. Can anyone provide some guidance to solve the problem? Many thanks!
# data
df1 <- structure(list(Y = c(3, 4, 1, 2, 3, 4, 1, 5, 2, 3, 4, 2, 1, 4,
1, 5, 3, 3, 3, 5, 5, 4, 3, 5, 4, 2, 5, 4, 3, 2, 5, 3, 2, 5, 5,
4, 5, 1, 2, 4, 3, 1, 2, 3, 1, 1, 3, 2, 4, 2, 2, 4, 1, 5, 3, 1,
5, 2, 3, 4, 2, 4, 5, 2, 4, 1, 4, 2, 1, 5, 3, 2, 1, 4, 4, 1, 5,
1, 1, 1, 4, 5, 5, 3, 2, 3, 3, 2, 4, 4, 5, 3, 5, 1, 2, 5, 5, 1,
2, 3), D = c(12, 8, 6, 11, 5, 14, 0, 22, 15, 13, 18, 3, 5, 9,
10, 28, 9, 16, 17, 14, 26, 18, 18, 23, 23, 12, 28, 14, 10, 15,
26, 9, 2, 30, 18, 24, 27, 7, 6, 25, 13, 8, 4, 16, 1, 4, 5, 18,
21, 1, 2, 19, 4, 2, 16, 17, 23, 15, 13, 21, 24, 14, 27, 6, 20,
6, 19, 8, 7, 23, 11, 11, 1, 22, 21, 4, 27, 6, 2, 9, 18, 30, 26,
22, 10, 1, 4, 7, 26, 15, 26, 18, 30, 1, 11, 29, 25, 3, 19, 15
), x1 = c(13, 12, 4, 3, 16, 16, 15, 13, 1, 15, 10, 16, 1, 17,
7, 13, 12, 6, 8, 16, 16, 11, 7, 16, 5, 13, 12, 16, 17, 6, 16,
9, 14, 16, 15, 5, 7, 2, 8, 2, 9, 9, 15, 13, 9, 4, 16, 2, 11,
13, 11, 6, 4, 3, 7, 4, 12, 2, 16, 14, 3, 13, 10, 11, 10, 4, 11,
16, 8, 12, 14, 9, 4, 16, 16, 12, 9, 10, 6, 1, 3, 8, 7, 7, 5,
16, 17, 10, 4, 15, 10, 8, 3, 13, 9, 16, 12, 7, 4, 11), x2 = c(12,
19, 18, 19, 15, 12, 15, 16, 15, 11, 12, 16, 17, 14, 12, 17, 17,
16, 12, 20, 11, 11, 15, 14, 18, 10, 14, 13, 10, 14, 18, 18, 18,
17, 18, 14, 16, 19, 18, 16, 18, 14, 17, 10, 16, 12, 16, 15, 11,
18, 19, 15, 19, 11, 16, 10, 20, 14, 10, 12, 10, 15, 13, 15, 11,
20, 11, 12, 16, 16, 11, 15, 11, 11, 10, 10, 16, 11, 20, 17, 20,
17, 16, 11, 18, 19, 18, 14, 17, 11, 16, 11, 18, 14, 15, 16, 11,
14, 11, 13)), class = "data.frame", row.names = c(NA, -100L))
library(mlogit)
mld <- mlogit.data(df1, choice="Y", shape="wide") # shape data for `mlogit()`
mlfit <- mlogit(Y ~ 1 | D + x1 + x2, reflevel="1", data=ml.d) # fit the model
effects(mlfit) # this shows the following error:
Error in if (rhs %in% c(1, 3)) { : argument is of length zero
Called from: effects.mlogit(mlfit)
I believe you are missing the covariate information that needs to be put there, so if you use effects(mlfit, covariate = 'D'), It should work. Now the error is coming because the default of covariate is NULL. NULL is special in R, it has no(zero) length and hence you are getting argument of length zero. Please let me know if it fixes your issue.
As per documentation of effects.mlogit , it says:
covariate
the name of the covariate for which the effect should be computed,
I am getting this output at my end:
R>effects(mlfit, covariate = 'D')
1 2 3
-0.003585105992 -0.070921137682 -0.026032167377
4 5
0.078295227196 0.022243183855
So I have read in a network data in iGraph(R) and would like to store the nodes into a list. Here's what I have done:
G = read_graph("somegraph.graphml",format="graphml")
x = list(V(G))
> x
+ 15/15 vertices, from ecb3920:
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
My question is, how do I get the true value, i.e. the actually node id in my data, from V(G). Thanks.
> dput(G)
structure(list(15, FALSE, c(13, 7, 9, 14, 10, 5, 4, 11, 6, 7,
14, 4, 13, 9, 10, 5, 5, 13, 9, 6, 7, 14, 12, 10, 14, 10, 11,
13, 9, 10, 12, 14, 8, 7, 11, 12, 8, 13, 14, 9, 11, 13, 13, 12,
14, 10, 13, 12, 14, 12, 13, 13, 14, 14), c(0, 0, 2, 2, 2, 2,
2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 6,
6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 9, 9, 9, 9, 10,
10, 10, 11, 11, 12, 12, 13), c(6, 11, 5, 15, 16, 8, 19, 1, 9,
20, 33, 32, 36, 2, 13, 18, 28, 39, 4, 14, 23, 25, 29, 45, 7,
26, 34, 40, 22, 30, 35, 43, 47, 49, 0, 12, 17, 27, 37, 41, 42,
46, 50, 51, 3, 10, 21, 24, 31, 38, 44, 48, 52, 53), c(1, 0, 6,
5, 2, 4, 3, 11, 15, 8, 9, 13, 14, 7, 12, 10, 16, 19, 20, 18,
23, 22, 17, 21, 25, 24, 33, 32, 28, 29, 26, 30, 27, 31, 36, 39,
34, 35, 37, 38, 40, 41, 45, 43, 42, 44, 47, 46, 48, 49, 50, 51,
52, 53), c(0, 0, 0, 0, 0, 2, 5, 7, 11, 13, 18, 24, 28, 34, 44,
54), c(0, 2, 2, 7, 16, 24, 26, 34, 40, 42, 46, 49, 51, 53, 54,
54), list(c(1, 0, 1), structure(list(), .Names = character(0)),
structure(list(id = c("1351920706", "500102244", "1454425532",
"1625050630", "510838353", "1262640078", "681721364", "1351920717",
"1260750116", "1524975171", "1070293410", "727198538", "715215233",
"1351920666", "500920034")), .Names = "id"), list()), <environment>), class = "igraph")
Just for closure (and to summarise from our chat): Based on the sample data you give, you can extract additional data for every vertex by indexing the corresponding element.
So
V(g)$id
returns
#[1] "1351920706" "500102244" "1454425532" "1625050630" "510838353"
#[6] "1262640078" "681721364" "1351920717" "1260750116" "1524975171"
#[11] "1070293410" "727198538" "715215233" "1351920666" "500920034"
I have the same question as this how to merge two weighted graph and sum weigths.
But here ist my R code for better understanding:
g1 <- graph.full(10)
V(g1)$name <- letters[1:vcount(g1)]
E(g1)$weight <- 1
g3 <- graph.full(5)
V(g3)$name <- c("a", "b", "x", "y", "z")
E(g3)$weight <- 1
graph.union.by.name(g1, g3)
The weights in merged graph should be a 2 on same edges in g1 and g3 (a - b)
And the dput of graphs is:
> dput(g1)
structure(list(10, FALSE, c(1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 3,
4, 5, 6, 7, 8, 9, 3, 4, 5, 6, 7, 8, 9, 4, 5, 6, 7, 8, 9, 5, 6,
7, 8, 9, 6, 7, 8, 9, 7, 8, 9, 8, 9, 9), c(0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3,
3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 8), c(0, 1, 9,
2, 10, 17, 3, 11, 18, 24, 4, 12, 19, 25, 30, 5, 13, 20, 26, 31,
35, 6, 14, 21, 27, 32, 36, 39, 7, 15, 22, 28, 33, 37, 40, 42,
8, 16, 23, 29, 34, 38, 41, 43, 44), c(0, 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), c(0, 0, 1, 3, 6, 10, 15, 21, 28, 36, 45),
c(0, 9, 17, 24, 30, 35, 39, 42, 44, 45, 45), list(c(1, 0,
1), structure(list(name = "Full graph", loops = FALSE), .Names = c("name",
"loops")), structure(list(name = c("a", "b", "c", "d", "e",
"f", "g", "h", "i", "j")), .Names = "name"), structure(list(
weight = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = "weight"))), class = "igraph")
> dput(g2)
structure(list(10, FALSE, c(1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 3,
4, 5, 6, 7, 8, 9, 3, 4, 5, 6, 7, 8, 9, 4, 5, 6, 7, 8, 9, 5, 6,
7, 8, 9, 6, 7, 8, 9, 7, 8, 9, 8, 9, 9), c(0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3,
3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 8), c(0, 1, 9,
2, 10, 17, 3, 11, 18, 24, 4, 12, 19, 25, 30, 5, 13, 20, 26, 31,
35, 6, 14, 21, 27, 32, 36, 39, 7, 15, 22, 28, 33, 37, 40, 42,
8, 16, 23, 29, 34, 38, 41, 43, 44), c(0, 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), c(0, 0, 1, 3, 6, 10, 15, 21, 28, 36, 45),
c(0, 9, 17, 24, 30, 35, 39, 42, 44, 45, 45), list(c(1, 0,
1), structure(list(name = "Full graph", loops = FALSE), .Names = c("name",
"loops")), structure(list(name = c("a", "b", "c", "d", "e",
"f", "g", "h", "i", "j")), .Names = "name"), structure(list(
weight = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = "weight"))), class = "igraph")
Is it possible with igraph or do i need some workaround?
This will be supported in the next version, until then here is a workaround:
mymerge <- function(g1, g2) {
e1 <- get.data.frame(g1, what="edges")
e2 <- get.data.frame(g2, what="edges")
e <- merge(e1, e2, by=c("from", "to"), all=TRUE)
newe <- data.frame(e[,c("from", "to"), drop=FALSE],
weight=rowSums(e[, c("weight.x", "weight.y")], na.rm=TRUE))
graph.data.frame(newe, directed=is.directed(g1))
}
mymerge(g1, g3)
# IGRAPH UNW- 13 54 --
# + attr: name (v/c), weight (e/n)
mymerge(g1, g3)["a", "b"]
# [1] 2