-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqMethodV03.R
311 lines (286 loc) · 8.3 KB
/
qMethodV03.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#### Q-method through R
#### FrK; 2019-10-04; created
#### FrK; 2019-10-10; updated
#### Content:
## 1) Loading bootStrapped datafiles
## 2) Performing QMethod
## 3) Storing important results
## 4) Apropriatng data
## 5) Producing graphs
## 6) Just for sure, doing the same with whole file
## Erasing memory and setting the working directory
setwd("c:/Users/Kalvas/ownCloud/!Luznak/")
rm(list=ls())
getwd()
## Important variables
# distances - dataFrame storing distances of sentences over all 150 bootStrapped files
# scores - dataFrame storing factor scores of sentences over all 150 bS files
# factor1 - dataFrame storing scores of Factor1 over all 150bS files
# factor2 - dataFrame storing scores of Factor2 over all 150bS files
#### 1) Loading bootStrapped datafiles
#### 2) Performing QMethod
#### 3) Storing important results
## Needed libraries
library(foreign)
library(qmethod)
## The first columns of two files
# Loading data prepared in Stata
results = qmethod(read.dta("RQM1.dta"), nfactors = 3, rotation = "cluster")
# Storing scores from 1st file
scores = results[[6]]
names(scores) = c("fac1_fil1", "fac2_fil1", "fac3_fil1")
#str(scores)
# Storing Factor1,2 from 1st file
factor1 = as.data.frame(rbind(scores$fac1_fil1))
factor2 = as.data.frame(rbind(scores$fac2_fil1))
factor3 = as.data.frame(rbind(scores$fac3_fil1))
#str(factor1)
#str(factor2)
# Storing distances from 1st file
x = results[[8]]
distances.XY = as.data.frame(rbind(abs(x$f1_f2)))
distances.XZ = as.data.frame(rbind(abs(x$f1_f3)))
distances.ZY = as.data.frame(rbind(abs(x$f2_f3)))
#str(distances)
## Loading all other columns
for(n in 2:1500) {
# Loading data
file = read.dta(paste0("RQM", n, ".dta"))
results = qmethod(file, nfactors = 3, rotation = "cluster")
# Adding scores and factor1,2
x = results[[6]]
factor1 = rbind.data.frame(factor1, x$fsc_f1)
factor2 = rbind.data.frame(factor2, x$fsc_f2)
factor3 = rbind.data.frame(factor3, x$fsc_f3)
names(x) = c(paste0("fac1_fil", n), paste0("fac2_fil", n), paste0("fac3_fil", n))
scores = data.frame(scores, x)
# Adding distances
x = results[[8]]
distances.XY = rbind.data.frame(distances.XY, abs(x$f1_f2))
distances.XZ = rbind.data.frame(distances.XZ, abs(x$f1_f3))
distances.ZY = rbind.data.frame(distances.ZY, abs(x$f2_f3))
}
# Vector with all variable names
vNames = c("V01", "V02", "V03", "V04", "V05", "V06", "V07", "V08", "V09", "V10", "V11", "V12")
vNames = c(vNames, "V13", "V14", "V15", "V16", "V17", "V18", "V19", "V20", "V21", "V22", "V23", "V24")
vNames = c(vNames, "V25", "V26", "V27", "V28", "V29", "V30", "V31", "V32", "V33", "V34", "V35", "V36")
vNames = c(vNames, "V37", "V38", "V39", "V40", "V41", "V42", "V43", "V44", "V45", "V46", "V47", "V48")
names(distances) = vNames
names(factor1) = vNames
names(factor2) = vNames
names(factor3) = vNames
#str(scores)
#str(factor1)
#str(factor2)
#str(distances)
#### 4) Apropriatng data
#### 5) Producing graphs
## Needed libraries
library(tidyverse)
library(stats)
library(magrittr)
library(ggplot2)
library(ggpubr)
#sNames = c("V01", "V02", "V03", "V04", "V05", "V06", "V07", "V08", "V09", "V10", "V11", "V12")
#sNames = c(sNames, "V13", "V14", "V15", "V16", "V17", "V18", "V19", "11-V20", "10-V21", "06-V22", "12-V23", "07-V24")
#sNames = c(sNames, "03-V25", "V26", "02-V27", "05-V28", "V29", "V30", "V31", "V32", "V33", "01-V34", "08-V35", "V36")
#sNames = c(sNames, "V37", "V38", "V39", "V40", "V41", "V42", "V43", "09-V44", "04-V45", "V46", "V47", "V48")
#names(distances) = sNames
## Graphs with differences of sentences in z-scores
dxy = distances.XY %>%
gather("Sentence", "Distance") %>%
ggplot(aes(x = reorder(Sentence, -Distance), y = Distance)) +
xlab("Sentence") +
geom_boxplot() +
geom_hline(aes(yintercept = 2)) +
geom_hline(aes(yintercept = 1.5)) +
geom_hline(aes(yintercept = 1)) +
theme_minimal()
dxy
dxz = distances.XZ %>%
gather("Sentence", "Distance") %>%
ggplot(aes(x = reorder(Sentence, -Distance), y = Distance)) +
xlab("Sentence") +
geom_boxplot() +
geom_hline(aes(yintercept = 2)) +
geom_hline(aes(yintercept = 1.5)) +
geom_hline(aes(yintercept = 1)) +
theme_minimal()
dxz
dzy = distances.ZY %>%
gather("Sentence", "Distance") %>%
ggplot(aes(x = reorder(Sentence, -Distance), y = Distance)) +
xlab("Sentence") +
geom_boxplot() +
geom_hline(aes(yintercept = 2)) +
geom_hline(aes(yintercept = 1.5)) +
geom_hline(aes(yintercept = 1)) +
theme_minimal()
dzy
## Scatterplot of factors' scores
# Factor3 averages ...
z = factor3 %>%
gather("var", "z") %>%
group_by(var) %>%
summarise(
z = mean(z)
)
# Factor2 averages ...
y = factor2 %>%
gather("var", "y") %>%
group_by(var) %>%
summarise(
y = mean(y)
)
# Factor1 averages ...
x = factor1 %>%
gather("var", "x") %>%
group_by(var) %>%
summarise(
x = mean(x)
)
# Combining both factors ...
xy = cbind.data.frame(x, y$y)
names(xy) = c("var", "factor1", "factor2")
xz = cbind.data.frame(x, z$z)
names(xz) = c("var", "factor1", "factor3")
zy = cbind.data.frame(z, y$y)
names(zy) = c("var", "factor3", "factor2")
# Scatterplot1:
sxy = xy %>%
ggplot(aes(x = factor1, y = factor2)) +
geom_point() +
geom_text(aes(label = var), nudge_y = -0.25) +
geom_curve(
aes(x = -4, y = -1, xend = 1, yend = 4),
curvature = -0.1,
angle = 90,
ncp = 15,
colour = "red"
) +
geom_curve(
aes(x = 4, y = 1, xend = -1, yend = -4),
curvature = -0.1,
angle = 90,
ncp = 15,
colour = "red"
) +
geom_curve(
aes(x = -3, y = -4, xend = 4, yend = 3),
curvature = 0.05,
angle = 90,
ncp = 15,
colour = "blue"
) +
geom_curve(
aes(x = 3, y = 4, xend = -4, yend = -3),
curvature = 0.05,
angle = 90,
ncp = 15,
colour = "blue"
) +
geom_hline(aes(yintercept = 3)) +
geom_hline(aes(yintercept = -3)) +
geom_vline(aes(xintercept = 3)) +
geom_vline(aes(xintercept = -3)) +
theme_minimal()
sxy
# Scatterplot2:
sxz = xz %>%
ggplot(aes(x = factor1, y = factor3)) +
geom_point() +
geom_text(aes(label = var), nudge_y = -0.25) +
geom_curve(
aes(x = -4, y = -1, xend = 1, yend = 4),
curvature = -0.1,
angle = 90,
ncp = 15,
colour = "red"
) +
geom_curve(
aes(x = 4, y = 1, xend = -1, yend = -4),
curvature = -0.1,
angle = 90,
ncp = 15,
colour = "red"
) +
geom_curve(
aes(x = -3, y = -4, xend = 4, yend = 3),
curvature = 0.05,
angle = 90,
ncp = 15,
colour = "blue"
) +
geom_curve(
aes(x = 3, y = 4, xend = -4, yend = -3),
curvature = 0.05,
angle = 90,
ncp = 15,
colour = "blue"
) +
geom_hline(aes(yintercept = 3)) +
geom_hline(aes(yintercept = -3)) +
geom_vline(aes(xintercept = 3)) +
geom_vline(aes(xintercept = -3)) +
theme_minimal()
sxz
# Scatterplot3:
szy = zy %>%
ggplot(aes(x = factor3, y = factor2)) +
geom_point() +
geom_text(aes(label = var), nudge_y = -0.25) +
geom_curve(
aes(x = -4, y = -1, xend = 1, yend = 4),
curvature = -0.1,
angle = 90,
ncp = 15,
colour = "red"
) +
geom_curve(
aes(x = 4, y = 1, xend = -1, yend = -4),
curvature = -0.1,
angle = 90,
ncp = 15,
colour = "red"
) +
geom_curve(
aes(x = -3, y = -4, xend = 4, yend = 3),
curvature = 0.05,
angle = 90,
ncp = 15,
colour = "blue"
) +
geom_curve(
aes(x = 3, y = 4, xend = -4, yend = -3),
curvature = 0.05,
angle = 90,
ncp = 15,
colour = "blue"
) +
geom_hline(aes(yintercept = 3)) +
geom_hline(aes(yintercept = -3)) +
geom_vline(aes(xintercept = 3)) +
geom_vline(aes(xintercept = -3)) +
theme_minimal()
szy
## Combining and exporting graphs
ggarrange(
dxy, dxz, dzy,
ncol = 1,
labels = c("Factor 1 vs. Factor 2",
"Factor 1 vs. Factor 3",
"Factor 3 vs. Factor 2")) %>%
ggexport(
filename = "distances3Factors.png",
width = 1800,
height = 1800)
ggarrange(
sxy, sxz, szy,
ncol = 3,
labels = c("Factor 1 vs. Factor 2",
"Factor 1 vs. Factor 3",
"Factor 3 vs. Factor 2")) %>%
ggexport(
filename = "scores3Factors.png",
width = 1800,
height = 600)