# BS1033 Lecture 2 Analysis
# Author: Chris Hansman
# Email: chansman@imperial.ac.uk
# Date : 18/01/21
# Installing Packages
#install.packages(“tidyverse”)
# Loading Libraries
library(tidyverse)
#Loading Today’s Data
#Reading Data
modes<-read_csv("modes_experiment.csv")
votes<-read_csv("votes_income.csv")
#--------------------------------------------------#
# Analyzing Candy Experiment
#--------------------------------------------------#
# Plotting a comparison of means (ugly)
ggplot(data=modes, aes(x=D_i, y=y_i)) +
geom_bar(stat="summary", fun="mean")
# Plotting a comparison of means (a bit prettier)
ggplot(data=modes, aes(x=as.factor(D_i), y=y_i)) +
geom_bar(stat="summary", fun.y="mean", width=0.5)+
labs(x = "Treatment: Candy or Not", y= "Modes Score")+
ylim(0,5) +
ggsave("modes_means.pdf")
#Conditional Means:
modes %>%
group_by(D_i) %>%
summarize(mean(y_i))
# A T-test
ttest <- t.test(y_i~D_i, data=modes, var.equal=TRUE)
ttest
#Regressions To Recover Means:
ols_modes<-lm(y_i~D_i, data= modes)
summary(ols_modes)
#--------------------------------------------------#
# Analyzing Voting Data
#--------------------------------------------------#
#Plotting All Data
ggplot(data = votes ) +
geom_point(aes(x = income, y = repvotes)) +
geom_smooth(aes(x = income, y = repvotes), method='lm',formula=y~x) +
ggsave("votes_all.pdf")
#Plotting Data by Group
ggplot(data = votes ) +
geom_point(aes(x = income, y = repvotes, color=as.factor(south))) +
geom_smooth(aes(x = income, y = repvotes, group=as.factor(south)), method='lm',formula=y~x) +
ggsave("votes_bygroup.pdf")
#Regressions Overall:
ols_votes_all<-lm(repvotes~income, data= votes)
summary(ols_votes_all)
#Regressions For South:
ols_votes_south<-lm(repvotes~income, data=subset(votes,south==1))
summary(ols_votes_south)
#Regressions For north:
ols_votes_north<-lm(repvotes~income, data=subset(votes,south==0))
summary(ols_votes_north)
# The average of the two...
(0.2968+0.3835 )/2
#Regressions With Control:
ols_votes_control<-lm(repvotes~income+south, data= votes)
summary(ols_votes_control)