-
Notifications
You must be signed in to change notification settings - Fork 0
/
final_code.R
314 lines (257 loc) · 13.1 KB
/
final_code.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
library(haven)
library(dplyr)
library(ggplot2)
library(reshape2)
library(tidyverse)
# Redirect text output to a file
sink("output_1.txt")
i<<-0
data <- read_sav("HXC23014 Harvard Poll Data.sav")
covid_questions <- select(data, "Q45","Q46", #Knowledge checks: 1 if true, 2 if false
"hQ47r1","hQ47r2","hQ47r3","hQ47r4", #eligibility for randomization on Q47, should all be 1
"hQ48r1","hQ48r2","hQ48r3","hQ48r4", #eligibility for randomization on Q48, should all be 1
"dQ47","dQ48", #treatment assignment for Q47, Q48
"hQ47Imgr1","hQ47Imgr2","hQ47Imgr3","hQ47Imgr4", #whether the respondent saw the img, condl on treatment assgm for Q47
"hQ48Imgr1","hQ48Imgr2", "hQ48Imgr3","hQ48Imgr4", #whether the respondent saw the img, condl on treatment assgm for Q48
"dQ47Imgr1","dQ47Imgr2", #id of img in each spot, condl on treatment assgm for Q47
"dQ48Imgr1","dQ48Imgr2", #id of img in each spot, condl on treatment assgm for Q48
"Q47","Q48") #whether the respondent clicked on the first or second image
# "Q38", #AI LIFE BETTER
# "Q41r6", #Social media
# "Q41r7", #mass media
# "Q42r1", #vax
# "Q42r2", #vax
#
#
# "Q1", #SEX
# "Q57", #age
# "Q59", #race
# "Q60", #hislat
# "Q61", #pid
# "Q65", #income
#+ controls$Q1 + controls$Q57 + controls$Q59 + controls$Q60 + controls$Q61 + controls$Q65 "classics"
#+
controls <- select(data,
"Q1", #SEX
"Q38", #AI LIFE BETTER
"Q41r6", #Social media
"Q41r7", #mass media
"Q42r1", #vax
"Q42r2", #vax
"Q49", #cheating
"Q57", #age
"Q59", #race
"Q60", #hislat
"Q61", #pid
"Q65", #income
"D103", #education
"QTimeStampQ47",
"QTimeStampQ48"
)
# "Q1", #SEX
# "Q57", #age
# "Q59", #race
# "Q60", #hislat
# "Q61", #pid
# "Q65", #income
controls$Q1 <- factor(controls$Q1, levels = c(1, 2, 3), labels = c("Male", "Female", "Something else"))
# controls$Q57 <- cut(controls$Q57,
# breaks = c(0, 18, 35, 50, 65, Inf),
# labels = c("Under 18", "18-34", "35-49", "50-64", "65 and over"),
# include.lowest = TRUE)
controls$Q59 <- factor(controls$Q59, levels = c(1, 2, 3, 4, 5, 6, 7, 8), labels = c("White", "Black", "Asian", "AIAN", "MENA", "NHPI", "Other", "NA"))
controls$Q60 <- factor(controls$Q60, levels = c(1, 2, 3), labels = c("HL", "!HL", "NA"))
controls$Q61 <- factor(controls$Q61, levels = c(1, 2, 3, 4, 5), labels = c("D", "R", "I", "O", "NS"))
controls$Q65 <- factor(controls$Q65,
levels = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17),
labels = c("Less than $10,000", "$10,000 - $19,999", "$20,000 - $29,999",
"$30,000 - $39,999", "$40,000 - $49,999", "$50,000 - $59,999",
"$60,000 - $69,999", "$70,000 - $79,999", "$80,000 - $99,999",
"$100,000 - $119,999", "$120,000 - $149,999", "$150,000 - $199,999",
"$200,000 - $249,999", "$250,000 - $349,999", "$350,000 - $499,999",
"$500,000 or more", "Prefer not to say"))
controls$Interest47 <- controls$QTimeStampQ47
controls$Interest48 <- controls$QTimeStampQ48
controls$Q38 <- factor(controls$Q61, levels = c(1, 2, 3, 4, 5, 6), labels = c("WW", "W", "-", "B", "BB", "?"))
controls$Q45 <- covid_questions$Q45-1
controls$Q46 <- covid_questions$Q46-1
# "Q38", #AI LIFE BETTER
# "Q41r6", #Social media
# "Q41r7", #mass media
analyse_data <- function(df) {
# Fit a one-way ANOVA
one.way <- aov(outcome ~ true_labeled * false_labeled, data = df)
print(summary(one.way))
# Fit a logistic regression model
fit <- glm(outcome ~ true_labeled * false_labeled, family = binomial(link = "logit"), data = df)
print(summary(fit))
# Aggregate data
data_agg <- aggregate(outcome ~ true_labeled + false_labeled, df, mean)
# Create and display the first plot
p1 <- ggplot(data_agg, aes(x = true_labeled, y = outcome, color = false_labeled, group = false_labeled)) +
geom_line(linewidth = 1) +
geom_point(size = 3) +
labs(x = "Fake News Labeled", y = "Pr(Fake News Selected)", color = "True News") +
scale_color_manual(values = c("blue", "red"), labels = c("Unlabeled", "Labeled")) +
theme_classic() +
theme(legend.position = "top")
ggsave(paste0("plot_", i, ".png"), p1)
print(i)
i<<-i+1
# Create and display the second plot
p2 <- ggplot(data_agg, aes(x = false_labeled, y = outcome, color = true_labeled, group = true_labeled)) +
geom_line(linewidth = 1) +
geom_point(size = 3) +
labs(x = "True News Labeled", y = "Pr(Fake News Selected)", color = "Fake News") +
scale_color_manual(values = c("blue", "red"), labels = c("Unlabeled", "Labeled")) +
theme_classic() +
theme(legend.position = "top")
ggsave(paste0("plot_", i, ".png"), p2)
print(i)
i<<-i+1
}
analyse_data_mycontrols <- function(df, time, know) {
# Fit a one-way ANOVA
one.way <- aov(outcome ~ true_labeled * false_labeled + time + know, data = df)
print(summary(one.way))
# Fit a logistic regression model
fit <- glm(outcome ~ true_labeled * false_labeled + time + know, family = binomial(link = "logit"), data = df)
print(summary(fit))
# Aggregate data
data_agg <- aggregate(outcome ~ true_labeled + false_labeled, df, mean)
# Create and display the first plot
p1 <- ggplot(data_agg, aes(x = true_labeled, y = outcome, color = false_labeled, group = false_labeled)) +
geom_line(linewidth = 1) +
geom_point(size = 3) +
labs(x = "Fake News Labeled", y = "Pr(Fake News Selected)", color = "True News") +
scale_color_manual(values = c("blue", "red"), labels = c("Unlabeled", "Labeled")) +
theme_classic() +
theme(legend.position = "top")
ggsave(paste0("plot_", i, ".png"), p1)
print(i)
i<<-i+1
# Create and display the second plot
p2 <- ggplot(data_agg, aes(x = false_labeled, y = outcome, color = true_labeled, group = true_labeled)) +
geom_line(linewidth = 1) +
geom_point(size = 3) +
labs(x = "True News Labeled", y = "Pr(Fake News Selected)", color = "Fake News") +
scale_color_manual(values = c("blue", "red"), labels = c("Unlabeled", "Labeled")) +
theme_classic() +
theme(legend.position = "top")
ggsave(paste0("plot_", i, ".png"), p2)
print(i)
i<<-i+1
}
analyse_data_with_controls_basic <- function(df, controls) {
# Fit a one-way ANOVA
one.way <- aov(outcome ~ true_labeled * false_labeled + controls$Q1 + controls$Q57 + controls$Q59 + controls$Q60 + controls$Q61 + controls$Q65, data = df)
print(summary(one.way))
# Fit a logistic regression model
fit <- glm(outcome ~ true_labeled * false_labeled + controls$Q1 + controls$Q57 + controls$Q59 + controls$Q60 + controls$Q61 + controls$Q65, family = binomial(link = "logit"), data = df)
print(summary(fit))
df$predicted <- predict(fit, type = "response")
data_pred <- aggregate(predicted ~ true_labeled + false_labeled, df, mean)
data_agg <- aggregate(outcome ~ true_labeled + false_labeled, df, mean)
# Create and display the first plot
p1 <- ggplot(data_agg, aes(x = true_labeled, y = outcome, color = false_labeled, group = false_labeled)) +
geom_line(linewidth = 1) +
geom_point(size = 3) +
geom_line(data = data_pred, aes(y = predicted), linetype = "dashed") +
labs(x = "Fake News Labeled", y = "Pr(Fake News Selected)", color = "True News") +
scale_color_manual(values = c("blue", "red"), labels = c("Unlabeled", "Labeled")) +
theme_classic() +
theme(legend.position = "top")
ggsave(paste0("plot_", i, ".png"), p1)
print(i)
i<<-i+1
# Create and display the second plot
p2 <- ggplot(data_agg, aes(x = false_labeled, y = outcome, color = true_labeled, group = true_labeled)) +
geom_line(linewidth = 1) +
geom_point(size = 3) +
geom_line(data = data_pred, aes(y = predicted), linetype = "dashed") +
labs(x = "True News Labeled", y = "Pr(Fake News Selected)", color = "Fake News") +
scale_color_manual(values = c("blue", "red"), labels = c("Unlabeled", "Labeled")) +
theme_classic() +
theme(legend.position = "top")
ggsave(paste0("plot_", i, ".png"), p2)
print(i)
i<<-i+1
}
analyse_data_with_allcontrols <- function(df, controls, time, know) {
# Fit a one-way ANOVA
one.way <- aov(outcome ~ true_labeled * false_labeled + controls$Q1 + controls$Q57 + controls$Q59 + controls$Q60 + controls$Q61 + controls$Q65 + time + know, data = df)
print(summary(one.way))
# Fit a logistic regression model
fit <- glm(outcome ~ true_labeled * false_labeled + controls$Q1 + controls$Q57 + controls$Q59 + controls$Q60 + controls$Q61 + controls$Q65 + time + know, family = binomial(link = "logit"), data = df)
print(summary(fit))
df$predicted <- predict(fit, type = "response")
data_pred <- aggregate(predicted ~ true_labeled + false_labeled, df, mean)
data_agg <- aggregate(outcome ~ true_labeled + false_labeled, df, mean)
# Create and display the first plot
p1 <- ggplot(data_agg, aes(x = true_labeled, y = outcome, color = false_labeled, group = false_labeled)) +
geom_line(linewidth = 1) +
geom_point(size = 3) +
geom_line(data = data_pred, aes(y = predicted), linetype = "dashed") +
labs(x = "Fake News Labeled", y = "Pr(Fake News Selected)", color = "True News") +
scale_color_manual(values = c("blue", "red"), labels = c("Unlabeled", "Labeled")) +
theme_classic() +
theme(legend.position = "top")
ggsave(paste0("plot_", i, ".png"), p1)
print(i)
i<<-i+1
# Create and display the second plot
p2 <- ggplot(data_agg, aes(x = false_labeled, y = outcome, color = true_labeled, group = true_labeled)) +
geom_line(linewidth = 1) +
geom_point(size = 3) +
geom_line(data = data_pred, aes(y = predicted), linetype = "dashed") +
labs(x = "True News Labeled", y = "Pr(Fake News Selected)", color = "Fake News") +
scale_color_manual(values = c("blue", "red"), labels = c("Unlabeled", "Labeled")) +
theme_classic() +
theme(legend.position = "top")
ggsave(paste0("plot_", i, ".png"), p2)
print(i)
i<<-i+1
}
# raw analysis
data_R <- data.frame(treatment = covid_questions$dQ47,
outcome = covid_questions$Q47-1,
true_labeled = covid_questions$dQ47Imgr1 - 1,
false_labeled = covid_questions$dQ47Imgr2 - 3)
data_R$true_labeled <- as.factor(data_R$true_labeled)
data_R$false_labeled <- as.factor(data_R$false_labeled)
data_R$outcome <- as.numeric(as.character(data_R$outcome)) # Outcome should be numeric
# to ensure 1 is the true, 2 is the false
data_L <- data.frame(treatment = covid_questions$dQ48,
outcome = ifelse(covid_questions$Q48 == 1, 2, ifelse(covid_questions$Q48 == 2, 1, covid_questions$Q48)) - 1 ,
true_labeled = covid_questions$dQ48Imgr2 - 3,
false_labeled = covid_questions$dQ48Imgr1 - 1)
data_L$true_labeled <- as.factor(data_L$true_labeled)
data_L$false_labeled <- as.factor(data_L$false_labeled)
data_L$outcome <- as.numeric(as.character(data_L$outcome)) # Outcome should be numeric
strength <- rbind(data_R, data_L)
print("analyse_data(data_R)")
analyse_data(data_R)
print("analyse_data(data_L)")
analyse_data(data_L)
print("analyse_data(strength)")
analyse_data(strength)
print("analyse_data_mycontrols(data_R, controls$Interest47, controls$Q45)")
analyse_data_mycontrols(data_R, controls$Interest47, controls$Q45)
print("analyse_data_mycontrols(data_L, controls$Interest48, controls$Q46)")
analyse_data_mycontrols(data_L, controls$Interest48, controls$Q46)
print("analyse_data_mycontrols(strength, c(controls$Interest47, controls$Interest48), c(controls$Q45, controls$Q46))")
analyse_data_mycontrols(strength, c(controls$Interest47, controls$Interest48), c(controls$Q45, controls$Q46))
print("analyse_data_with_controls_basic(data_R, controls)")
analyse_data_with_controls_basic(data_R, controls)
print("analyse_data_with_controls_basic(data_L, controls)")
analyse_data_with_controls_basic(data_L, controls)
strength_controls <- rbind(controls, controls)
print("analyse_data_with_controls_basic(strength, strength_controls)")
analyse_data_with_controls_basic(strength, strength_controls)
print("analyse_data_with_allcontrols(data_R, controls, controls$Interest47, controls$Q45)")
analyse_data_with_allcontrols(data_R, controls, controls$Interest47, controls$Q45)
print("analyse_data_with_allcontrols(data_L, controls, controls$Interest48, controls$Q46)")
analyse_data_with_allcontrols(data_L, controls, controls$Interest48, controls$Q46)
print("analyse_data_with_allcontrols(strength, strength_controls, c(controls$Interest47, controls$Interest48), c(controls$Q45, controls$Q46))")
analyse_data_with_allcontrols(strength, strength_controls, c(controls$Interest47, controls$Interest48), c(controls$Q45, controls$Q46))
sink()