-
Notifications
You must be signed in to change notification settings - Fork 0
/
genderexperiment.Rmd
225 lines (155 loc) · 4.94 KB
/
genderexperiment.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
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
---
title: "Gender Experiment - w241 Final Project"
author: "Daniel Alvarez"
date: "3/13/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r,include=FALSE, results='hide'}
# clear out environment variables
rm(list=ls())
# set seed for randomization
set.seed(1)
```
Load packages
```{r, include=FALSE}
# load packages
library(foreign)
library(data.table)
library(knitr)
library(dplyr)
library(foreign)
library(zoo)
library(lmtest) # robust standard errors
library(sandwich) # robust standard errors
library(stargazer) # reporting formatted results
library(magrittr) # printing document
library(pwr) # estimating power
```
## Gender experiment
Here we load the pre-treatment survey data. Then block on gender and age, by peaking at the distribution. Then, assign subjects randomly to the pilot study and treatment, placebo and control groups.
```{r read data}
d <- fread('./pretreatmentresponses1.csv')
d
```
Peak at the dataset
```{r}
str(d)
summary(d)
dim(d)
```
Set an `id` variable (ordinal) useful for randomization assignment
```{r}
d[,'id'] <- c(1:nrow(d))
```
Rename long variable names
```{r}
colnames(d)[1:5] <-c("timestamp","gender_string","age","email","consent")
str(d)
```
Check for any "No" consents. These would be excluded from the study.
```{r}
table(d$consent)
```
### Modify dataset
Look at gender distribution.
```{r}
table(d$gender_string)
```
Look at age distribution
```{r}
hist(d$age,main="Age distribution", xlab="Counts for age",ylab = "Frequency", breaks=50)
```
```{r}
# summary statistics on age
summary(d$age)
```
Codify gender as a binary variable. Female is 0, Male is 1.
```{r}
d[, gender:=ifelse(gender_string=='Female',0,1)]
# check through a cross-tab
# table(d$gender_string,d$gender)
```
### Randomly assign subjects to pilot study
Assume 10 people assigned to pilot.
```{r pressure, echo=FALSE}
# Assume 10 people assigned to the pilot
n_pilot <- 10
x_prob <- n_pilot/nrow(d)
x_prob <- round(x_prob,digits=2)
x_prob
d[,'is_pilot':= sample(c(0:1),size=.N, replace=TRUE, prob=c(1-x_prob,x_prob))]
# check number of people assigned to pilot is ~10
table(d$is_pilot)
```
### Blocking
Block on gender.
```{r}
# Assign variables
N = nrow(d)
n_female <- length(d$age[d$gender==0])
n_female
n_male = length(d$age[d$gender==1])
n_male
#randomly assign subjects to either treatment or control groups blocking by state
randomize_blockbygender <- function(){
ifelse(d$gender==1,sample(c(rep(0,n_female/2),rep(1,n_female/2))),sample(c(rep(0,n_male/2),rep(1,n_male/2))) )
}
d[,'treatment'] <- randomize_blockbygender()
```
Check cross-tab of treatment and gender to observe if more or less equal proportions of gender in each block.
```{r}
table(d$gender,d$treatment)
```
### Introduce two treatment groups: female voice `TF` and male voice `TM`
Split treatment group further into two treatment groups
```{r}
n_treatment = length(d$age[d$treatment==1])
n_treatment
#randomly assign subjects to either two treatment groups (TF and TM) for those with treatment==1
randomize_treatment <- function(){
ifelse(d$treatment==1,sample(c(rep(0,n_treatment/2),rep(1,n_treatment/2))),0)
}
d[,'is_treatedmalevoice'] <- randomize_treatment()
```
Check cross-tab of treatment and is_treatedmalevoice to observe if more or less equal proportions of treatment of male and female voice when treatment ==1.
```{r}
table(d$is_treatedmalevoice,d$treatment)
```
For dataset readability purposes, include a string variable for each subjects assignment: control `C`, treatment with male voice `TM`, and treatment with female voice `TF`.
```{r}
# create a string assignment variable for readability purposes
d[treatment==0, assignment:='C']
d[treatment==1 & is_treatedmalevoice==1, assignment:='TM']
d[treatment==1 & is_treatedmalevoice==0, assignment:='TF']
# check cross-tab
table(d$assignment)
```
Peak at resulting dataset.
```{r}
head(d)
```
```{r}
d
```
##Examine covariate balance across assignment groups
Due to blocking, the distribution of experimental subject's gender is roughly balanced across assignment groups.
```{r}
# check cross-tab of assignment and subject gender
table(d$assignment, d$gender_string)
assignment.labs <- c("Control", "Treatment-Female Voice", "Treatment-Male Voice")
names(assignment.labs) <- c("C", "TF", "TM")
ggplot(d,aes(x=gender_string))+stat_count(width = 0.5)+
facet_grid(~assignment, labeller = labeller(assignment = assignment.labs)) + labs(title = 'Histograms of gender distribution by assignment group')+theme_bw()
```
```{r}
# check cross-tab of assignment and age
table(d$assignment, d$age)
# examine age distributions across assignment groups
assignment.labs <- c("Control", "Treatment-Female Voice", "Treatment-Male Voice")
names(assignment.labs) <- c("C", "TF", "TM")
ggplot(d,aes(x=age))+geom_histogram(bins=30)+
facet_grid(~assignment, labeller = labeller(assignment = assignment.labs)) + labs(title = 'Histograms of age distribution by assignment group')+theme_bw()
```