-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.step_Korrelation Matrix.R
More file actions
364 lines (288 loc) · 11.6 KB
/
Copy path5.step_Korrelation Matrix.R
File metadata and controls
364 lines (288 loc) · 11.6 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# Forschungsprojekt Polarisierung & Generationen ---------------------------
# Edda Brandes
# last edit: 22.02.2022
# Step 5: Robustness Test, Korrelationsmatrix
## Load data and data subsetten ----
library(tidyverse)
library(dplyr)
#install.packages("Hmisc")
library(Hmisc)
#install.packages("corrplot")
library(corrplot)
#Links for corrplot:
#https://rdrr.io/cran/corrplot/man/corrplot.html
#https://stefaneng.github.io/corrplot-title-cut-off/
#http://www.sthda.com/english/wiki/correlation-matrix-a-quick-start-guide-to-analyze-format-and-visualize-a-correlation-matrix-using-r-software#compute-correlation-matrix
load(file="Data_Allbus,Sample_prep.Rdata")
Sys.setenv(LANG = "en")
items <- allbus_sample %>% dplyr::select(starts_with("item"))
options(digits=4)
##Items umkodieren in Subset items----
#hohe Zahl =linksliberal
#niedrige Zahl = rechtskonservativ
#trad. Familienbild:
#fr01 berufstätige Frau ebenso herzliches & vertrauensvolles Verhältnis zum Kind
items$item_fr01 <- recode(items$item_fr01, `4`=1, `3`=2, `2`=3, `1`=4)
#fr05a Frau berufstätig bessere Mutter?
items$item_fr05a <- recode(items$item_fr05a, `4`=1, `3`=2, `2`=3, `1`=4)
#Moral Abtreibung
all$item_fn02 <- recode(all$item_fn02, `1`=1, `3`=2, `2`=3)
items$item_abtreib1 <- recode(items$item_abtreib1, `2`=1, `1`=2)
items$item_abtreib2 <- recode(items$item_abtreib2, `2`=1, `1`=2)
items$item_abtreib3 <- recode(items$item_abtreib3, `2`=1, `1`=2)
items$item_abtreib4 <- recode(items$item_abtreib4, `2`=1, `1`=2)
items$item_sterbe <- recode(items$item_sterbe, `4`=1, `3`=2, `2`=3, `1`=4)
items$item_hasch <- recode(items$item_hasch, `4`=1, `3`=2, `2`=3, `1`=4)
items$item_homo <- recode(items$item_homo, `4`=1, `3`=2, `2`=3, `1`=4)
#Migration
items$item_mi02 <- recode(items$item_mi02, `3`=1, `2`=2, `1`=3)
items$item_mi04 <- recode(items$item_mi04, `3`=1, `2`=2, `1`=3)
## Korrelation berechnen ----
cor_items <- cor(items, method = "spearman", use="pairwise.complete.obs")
which.min(cor_items)
cor_items[348]
is.numeric(cor_items)
cor_items.r <- round(cor_items, digits=2)
cor_items.r
write.table(cor_items.r, file = "2.Output/Korrelationsmatrix.txt", sep= ";")
## Korrelation mit Signifikanzniveau berechnen ----
cor_data <- as.matrix(items[sapply(items, is.numeric)])
is.numeric(cor_data)
dim(cor_data)
cor_sig <- rcorr(as.matrix(items), type= "spearman")
view(cor_sig)
## extract the coefficients------
cor_sig$r
write.table(format(cor_sig$r, digits=2),file="2.Output/Korrelationskoeffizienten.txt", sep=";", col.names = TRUE, dec=",")
## extract the p-values----
cor_sig$P
write.table(format(cor_sig$P, digits=2), file="2.Output/Korrelationsmatrix p-value.txt", sep=";", col.names=TRUE, dec=",")
## format as a table-----
flattenCorrMatrix <- function(cormat, pmat) {
ut <- upper.tri(cormat)
data.frame(
row = rownames(cormat)[row(cormat)[ut]],
column = rownames(cormat)[col(cormat)[ut]],
cor =(cormat)[ut],
p = pmat[ut]
)
}
cor_sig_table <-flattenCorrMatrix(cor_sig$r, cor_sig$P)
write.table(format(cor_sig_table, digits=2), file="2.Output/Korrelationsmatrix as table.txt", sep=";", col.names=TRUE, dec=",")
## Tabelle vereinfachen und anstatt der Werte Zahlengrenzen eingeben----
which.min(cor_sig$r)
cor_sig$r[60]
cor_vtable <- symnum(cor_sig$r, cutpoints = c( 0, 0.1, 0.2, 0.4, 0.6, 0.8, 1),
symbols = c("+" , "++", "+++", "++++", "+++++", "++++++", "."),
na="NA",
abbr.colnames = TRUE)
#gibt mir keine Tabelle aus
## Korrelationsmatrix visualisieren------
cor <- cor_sig$r
is.matrix(cor_data)
str(cor_data)
cor_p <- cor_sig$P
dim(cor_p)
dim(cor)
table(is.na(cor_p))
table(is.na(cor))
is.matrix(cor_p)
#cor Data runden, damit bessere Skalen
cor2 <- round(cor, 1)
COL2(diverging = c("RdBu", "BrBG", "PiYG", "PRGn", "PuOr", "RdYlBu"), n = 200)
corrplot(cor2, method= "ellipse", type = "upper",
title="Korrelation der Items",
order="original",
col= COL2("RdBu",10),
tl.cex = 0.8,
mar=c(0,0,2,0),
col.lim = c(-0.2,1),
is.corr=TRUE,
tl.col = "black", tl.srt = 90)
png(file="2.Output/Korrelationsmatrix,visualisiert.png", height=1800, width = 1800, res = 300)
corrplot(cor2, method= "ellipse", type = "upper",
title="Korrelation der Items",
order="original",
col= COL2("RdBu",10),
tl.cex = 0.8,
mar=c(0,0,2,0),
col.lim = c(-0.2,1),
is.corr=TRUE,
tl.col = "black", tl.srt = 90)
dev.off()
corrplot(cor, method="circle", type="upper",
title="signifikante Korrelation der Items",
order="original",
tl.cex=1,
col.lim=c(-0.1,1),
tl.col="black",
p.mat = cor_p, sig.level = 0.01, insig = "blank")
#Signifikanzniveau einfügen funktioniert nicht, weil unterschiedliche NA´s
## Korrelationen für einzelne Dimensionen----
###traditionelles Familienbild Korrelation----
items_trad <- items %>% dplyr::select(starts_with("item_fr"))
items_trad <- as.matrix(items_trad[sapply(items_trad, is.numeric)])
is.numeric(items_trad)
dim(items_trad)
cor_trad <- rcorr(as.matrix(items_trad), type= "spearman")
cor_trad$r
write.table(format(cor_trad$r, digits=2),file="2.Output/Korrelationskoeffizienten_itemstrad.txt", sep=";", col.names = TRUE, dec=",")
#extract the p-values
cor_trad$P
write.table(format(cor_trad$P, digits=2), file="2.Output/Korrelationsmatrix_itemstrad p-value.txt", sep=";", col.names=TRUE, dec=",")
#format as a table
flattenCorrMatrix <- function(cormat, pmat) {
ut <- upper.tri(cormat)
data.frame(
row = rownames(cormat)[row(cormat)[ut]],
column = rownames(cormat)[col(cormat)[ut]],
cor =(cormat)[ut],
p = pmat[ut]
)
}
cor_trad_table <-flattenCorrMatrix(cor_trad$r, cor_trad$P)
write.table(format(cor_trad_table, digits=2), file="2.Output/Korrelationsmatrix as table trad .txt", sep=";", col.names=TRUE, dec=",")
#Korrelationsmatrix visualisieren
cor_trad.r <- cor_trad$r
cor_trad.p <- cor_trad$P
is.matrix(cor_trad.r)
str(cor_trad)
png(file="2.Output/Korrelationsmatrix trad,visualisiert.png", height=1800, width = 1800, res = 300)
cor_trad.r_plot1 <- corrplot(cor_trad.r, method= "circle", type = "upper",
tl.col = "black", tl.srt = 90)
dev.off()
### Korrelationsmatrix Moral----
items_mo <- items %>% dplyr::select(starts_with("item_a"), item_fn02,item_sterbe, item_hasch, item_homo)
items_mo <- as.matrix(items_mo[sapply(items_mo, is.numeric)])
is.numeric(items_mo)
dim(items_mo)
cor_mo <- rcorr(as.matrix(items_mo), type= "spearman")
cor_mo$r
write.table(format(cor_mo$r, digits=2),file="2.Output/Korrelationskoeffizienten_itemsmo.txt", sep=";", col.names = TRUE, dec=",")
#extract the p-values
cor_mo$P
write.table(format(cor_mo$P, digits=2), file="2.Output/Korrelationsmatrix_itemsmo p-value.txt", sep=";", col.names=TRUE, dec=",")
#format as a table
flattenCorrMatrix <- function(cormat, pmat) {
ut <- upper.tri(cormat)
data.frame(
row = rownames(cormat)[row(cormat)[ut]],
column = rownames(cormat)[col(cormat)[ut]],
cor =(cormat)[ut],
p = pmat[ut]
)
}
cor_mo_table <-flattenCorrMatrix(cor_mo$r, cor_mo$P)
write.table(format(cor_mo_table, digits=2), file="2.Output/Korrelationsmatrix moral, as table.txt", sep=";", col.names=TRUE, dec=",")
#Korrelationsmatrix visualisieren
cor_mo.r <- cor_mo$r
cor_mo.p <- cor_mo$P
dim(cor_mo$P)
dim(cor_mo$r)
is.matrix(cor_mo.r)
str(cor_mo)
png(file="2.Output/Korrelationsmatrix mo,visualisiert.png", height=1800, width = 1800, res = 300)
cor_mo.r_plot1 <- corrplot(cor_mo.r, method= "circle", type = "upper",
tl.col = "black", tl.srt = 90)
dev.off()
items_mo <- items %>% dplyr::select(starts_with("item_a"), item_fn02,item_sterbe, item_hasch, item_homo)
items_mo <- as.matrix(items_mo[sapply(items_mo, is.numeric)])
is.numeric(items_mo)
dim(items_mo)
cor_mo <- rcorr(as.matrix(items_mo), type= "spearman")
cor_mo$r
write.table(format(cor_mo$r, digits=2),file="2.Output/Korrelationskoeffizienten_itemsmo.txt", sep=";", col.names = TRUE, dec=",")
#extract the p-values
cor_mo$P
write.table(format(cor_mo$P, digits=2), file="2.Output/Korrelationsmatrix_itemsmo p-value.txt", sep=";", col.names=TRUE, dec=",")
#format as a table
flattenCorrMatrix <- function(cormat, pmat) {
ut <- upper.tri(cormat)
data.frame(
row = rownames(cormat)[row(cormat)[ut]],
column = rownames(cormat)[col(cormat)[ut]],
cor =(cormat)[ut],
p = pmat[ut]
)
}
cor_mo_table <-flattenCorrMatrix(cor_mo$r, cor_mo$P)
write.table(format(cor_mo_table, digits=2), file="2.Output/Korrelationsmatrix moral, as table.txt", sep=";", col.names=TRUE, dec=",")
#Korrelationsmatrix visualisieren
cor_mo.r <- cor_mo$r
cor_mo.p <- cor_mo$P
dim(cor_mo$P)
dim(cor_mo$r)
is.matrix(cor_mo.r)
str(cor_mo)
png(file="2.Output/Korrelationsmatrix mo,visualisiert.png", height=1800, width = 1800, res = 300)
cor_mo.r_plot1 <- corrplot(cor_mo.r, method= "circle", type = "upper",
tl.col = "black", tl.srt = 90)
dev.off()
###Korrelationsmatrix Umverteilung----
items_i <- items %>% dplyr::select(starts_with("item_i"))
items_i <- as.matrix(items_i[sapply(items_i, is.numeric)])
is.numeric(items_i)
dim(items_i)
cor_i <- rcorr(as.matrix(items_i), type= "spearman")
cor_i$r
write.table(format(cor_i$r, digits=2),file="2.Output/Korrelationskoeffizienten_items Umverteil.txt", sep=";", col.names = TRUE, dec=",")
#extract the p-values
cor_i$P
write.table(format(cor_i$P, digits=2), file="2.Output/Korrelationsmatrix_itemsmo p-value.txt", sep=";", col.names=TRUE, dec=",")
#format as a table
flattenCorrMatrix <- function(cormat, pmat) {
ut <- upper.tri(cormat)
data.frame(
row = rownames(cormat)[row(cormat)[ut]],
column = rownames(cormat)[col(cormat)[ut]],
cor =(cormat)[ut],
p = pmat[ut]
)
}
cor_i_table <-flattenCorrMatrix(cor_i$r, cor_i$P)
write.table(format(cor_i_table, digits=2), file="2.Output/Korrelationsmatrix umvert, as table.txt", sep=";", col.names=TRUE, dec=",")
#Korrelationsmatrix visualisieren
cor_i.r <- cor_i$r
cor_i.p <- cor_i$P
dim(cor_i$P)
dim(cor_i$r)
is.matrix(cor_i.r)
str(cor_i)
png(file="2.Output/Korrelationsmatrix umvert,visualisiert.png", height=1800, width = 1800, res = 300)
cor_i.r_plot1 <- corrplot(cor_i.r, method= "circle", type = "upper",
tl.col = "black", tl.srt = 90)
dev.off()
###Korrelationsmatrix Migration----
items_mig <- items %>% dplyr::select(starts_with("item_m"))
items_mig <- as.matrix(items_mig[sapply(items_mig, is.numeric)])
is.numeric(items_mig)
dim(items_mig)
cor_mig <- rcorr(as.matrix(items_mig), type= "spearman")
cor_mig$r
write.table(format(cor_mig$r, digits=2),file="2.Output/Korrelationskoeffizienten_items Migration.txt", sep=";", col.names = TRUE, dec=",")
#extract the p-values
cor_mig$P
write.table(format(cor_mig$P, digits=2), file="2.Output/Korrelationsmatrix_items mig p-value.txt", sep=";", col.names=TRUE, dec=",")
#format as a table
flattenCorrMatrix <- function(cormat, pmat) {
ut <- upper.tri(cormat)
data.frame(
row = rownames(cormat)[row(cormat)[ut]],
column = rownames(cormat)[col(cormat)[ut]],
cor =(cormat)[ut],
p = pmat[ut]
)
}
cor_mig_table <-flattenCorrMatrix(cor_mig$r, cor_mig$P)
write.table(format(cor_mig_table, digits=2), file="2.Output/Korrelationsmatrix mig, as table.txt", sep=";", col.names=TRUE, dec=",")
#Korrelationsmatrix visualisieren
cor_mig.r <- cor_mig$r
cor_mig.p <- cor_mig$P
dim(cor_mig$P)
dim(cor_mig$r)
is.matrix(cor_mig.r)
str(cor_mig)
png(file="2.Output/Korrelationsmatrix mig,visualisiert.png", height=1800, width = 1800, res = 300)
cor_mig.r_plot1 <- corrplot(cor_mig.r, method= "circle", type = "upper",
tl.col = "black", tl.srt = 90)
dev.off()