CS计算机代考程序代写 # BS1033 Lecture 3 Analysis
# BS1033 Lecture 3 Analysis
# Author: Chris Hansman
# Email: chansman@imperial.ac.uk
# Date : 21/01/21
# Installing Packages
#install.packages(“tidyverse”)
install.packages(“ggthemes”) # Install
install.packages(“stargazer”)
# Loading Libraries
library(tidyverse)
library(lubridate)
library(ggthemes) # Load
library(stargazer)
#Loading Today’s Data
#Reading Data
sandp<-read_csv("sandp_5com.csv")
panel<-read_csv("panel_example.csv")
d_in_d<-read_csv("diff_in_diff.csv")
#--------------------------------------------------#
# Analyzing S&P Data
#--------------------------------------------------#
# Plotting a comparison of prices
ggplot(data=sandp, aes(x=t, y=price)) +
geom_point(aes(color=comnam))
sandp <- sandp %>%
mutate(date2=dmy(date))
# Plotting a comparison of prices
ggplot(data=sandp, aes(x=date2, y=price)) +
geom_point(aes(color=comnam))
#Simple Fixed Effects Regression */
com_ols <-lm(price ~ as.factor(comnam), data=sandp)
summary(com_ols)
#Predict Residuals
sandp <- sandp %>%
mutate(residuals = residuals(com_ols))
# Plotting a comparison of residualized prices
ggplot(data=sandp, aes(x=t, y=residuals)) +
geom_point(aes(color=comnam)) +
geom_line(aes(color=comnam)) +
theme_classic()+
ylim(-4,4)
# Plotting a comparison of residualized prices
#Other Themes: theme_dark, theme_bw
#ggthemes: theme_tufte, theme_economist
ggplot(data=sandp, aes(x=date2, y=residuals)) +
geom_point(aes(color=comnam)) +
geom_line(aes(color=comnam)) +
theme_minimal()+
ylim(-4,4) +
labs(x=””, y= “Residualized Stock Price”)+
ggsave(“sandp_resid.pdf”)
# Two way Fixed Effects
#Simple Fixed Effects Regression */
com_ols2 <-lm(price ~ as.factor(comnam)+ as.factor(date2), data=sandp)
summary(com_ols2)
#Predict Residuals
sandp <- sandp %>%
mutate(residuals2 = residuals(com_ols2))
# Plotting a comparison of residualized prices
#Other Themes: theme_dark, theme_bw
#ggthemes: theme_tufte, theme_economist
ggplot(data=sandp, aes(x=date2, y=residuals2)) +
geom_point(aes(color=comnam)) +
geom_line(aes(color=comnam)) +
theme_classic()+
ylim(-4,4) +
labs(x=””, y= “Residualized Stock Price”)+
ggsave(“sandp_resid2.pdf”, width = 10, height = 4)
#————————————————–#
# Panel Example
#————————————————–#
#x and a are correlated
ggplot(data=panel, aes(x=x_it, y=a_i)) +
geom_point()+
geom_smooth(method=’lm’,formula=y~x)
# Suppose we Observed a_i
a_ols <- lm(y_it ~ x_it+a_i, data=panel)
summary(a_ols)
#Pooled Regression without a_i
pooled_ols <- lm(y_it ~ x_it, data=panel)
summary(pooled_ols)
stargazer(a_ols, pooled_ols, type="text")
# Creating First Differenced Data
panel_diff <- panel %>%
group_by(i) %>%
mutate(Dy_it = y_it – lag(y_it)) %>%
mutate(Dx_it = x_it – lag(x_it), Da_i = a_i – lag(a_i)) %>%
filter(!is.na(Dy_it))
# First Difference Regression
firstdiff_ols <-lm(Dy_it ~ Dx_it, data=panel_diff)
summary(firstdiff_ols)
stargazer(a_ols, pooled_ols, firstdiff_ols, type="text")
# Fixed Effects Regression
fixedeffects_ols <-lm(y_it ~ x_it+as.factor(i), data=panel)
summary(fixedeffects_ols)
stargazer(a_ols, pooled_ols, fixedeffects_ols, type="text", keep=c("x_it"))
#--------------------------------------------------#
# Difference in Difference Example
#--------------------------------------------------#
#Difference in Difference
d_in_d <- d_in_d %>%
mutate(D_i=state==”Delaware”) %>%
mutate(A_t=year==1992) %>%
mutate(DA=(state==”Delaware”&year==1992))
# Difference in Difference
ols_dd<- lm(leverage~DA+A_t+D_i, data=d_in_d)
summary(ols_dd)
# Full Model
ols_ddfull<- lm(leverage~DA+as.factor(company_id)+as.factor(year), data=d_in_d)
summary(ols_ddfull)
stargazer(ols_dd, ols_ddfull, type="text", keep=c("DA"))