-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrustExprimentsmt.Rmd
146 lines (126 loc) · 3.58 KB
/
trustExprimentsmt.Rmd
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
---
title: "Expriments for Trust"
author: "Group"
output:
pdf_document: default
html_document: default
---
```{r}
library(ggplot2)
library(ggthemr)
library(dplyr)
```
```{r}
# Load the expriment's data
dmt <- read.csv("./cleaneddata/mturk.csv")
head(dmt)
```
```{r}
dmt$Outcome <- ifelse(dmt$Q7=="I do not trust what the speaker was saying.", "No Trust", ifelse(dmt$Q7=="I trust what the speaker was saying.", "Trust", "NA"))
summary(dmt)
```
```{r}
ggthemr('earth', type="outer", layout='scientific', spacing=2)
p1 <- ggplot(data=dmt %>% filter(Outcome != "NA"), aes(x=FL_3_DO)) +
geom_bar(aes(y=FL_3_DO, fill=Outcome), stat = "identity") +
labs(title="Counts", y="Total Counts", x = "Tmt vs. Control") +
ylab("") +
theme_classic() #+ theme(legend.position = "none")
p1
```
```{r}
# Adding columns for control, treatment and clusters (M, F)
t = c("FTreatment", "MTreatment")
s = c("FTreatment", "FControl")
dmt$treat = ifelse(dmt$FL_3_DO %in% t, 1, 0 )
dmt$female = ifelse(dmt$FL_3_DO %in% s, 1, 0)
dmt$trust = ifelse(dmt$Q7 == "I trust what the speaker was saying.", 1,
ifelse (dmt$Q6 != "Pritikin" | dmt$Q7 == "", NA, 0))
```
```{r}
head(dmt, 100)
```
```{r}
summary(dmt)
```
```{r}
# Distribution of treatment vs control
hist(dmt$treat, main='Distribution of treatment, 1 is treatment', xlab='Treatment')
```
```{r}
# Distribution of male vs female
hist(dmt$female, main='Gender Distribution', xlab='Gender, 1 is female')
```
**These distributions are fine**
```{r}
# Regressing trust on treatment
lrmt = lm(trust ~ treat, data = dmt)
summary(lrmt)
```
```{r}
ate = lrmt$coefficients['treat']
paste0('The ATE of the dissonace video is: ', ate)
```
```{r}
# Adding Female as the covariate
lrmt1 = lm(trust ~ treat + female * treat, data = dmt)
summary(lrmt1)
```
```{r}
# Randomization inference
# Creating new variables for randomizing run and run_lag
dmt$treat_rnd = sample(dmt$treat)
head(dmt)
summary(dmt)
```
```{r}
# Repeating the regression with the randomized treatment variable
# Regressing trust on treatment
lrmt2 = lm(trust ~ treat_rnd, data = dmt, na.action = na.omit)
summary(lrmt2)
```
```{r}
names(summary(lrmt2))
summary(lrmt2)$coefficients
summary(lrmt2)$coefficients[,2]['treat_rnd']
```
```{r}
# Generic function to randomly(pseudo) pick the treatment assignments
coeff_treat_rnd <- function(d) {
d$treat_rnd = sample(d$treat)
lmr = lm(trust ~ treat_rnd, data=d, na.action = na.omit)
return (lmr$coefficients['treat_rnd'])
}
# Trying another randomization
coeff_treat_rnd(dmt)
```
```{r}
# 100K Randomizations
h.distribution.under.sharp.null <- replicate(100000, coeff_treat_rnd(dmt))
h.ate.mean <- mean(h.distribution.under.sharp.null)
paste0("Mean fromm 1k randomizations: ", h.ate.mean)
```
```{r}
# Graph for the estimates
plot(density(h.distribution.under.sharp.null),
main = "Density under Sharp Null")
# Adding our original ATE to the plot
abline(v = ate, col = "blue")
# Adding the mean to the plot
abline(v = mean(h.distribution.under.sharp.null), col = 'orange')
```
```{r}
# p value
pv = mean(abs(h.distribution.under.sharp.null) >= abs(ate))
paste0('p value: ', pv)
```
```{r}
# num of assignments that generate an estimated ATE at least as large as the actual
n <- sum(abs(h.distribution.under.sharp.null) >= abs(ate))
paste0("Number of assignments that generate an ATE at least as large as what we got from the experiment: ", n)
```
```{r}
# Standard error
se = sd(h.distribution.under.sharp.null)/sqrt(length(h.distribution.under.sharp.null))
paste0('Standard error: ', se)
```