#################################################################
## POL269: POLITICAL DATA ANALYSIS
## LECTURE 8: EFFECT OF DEATH OF LEADER ON LEVEL OF DEMOCRACY
#################################################################

## STEP 1: Set the working directory to POL269 folder load packages
setwd("~/Desktop/POL269") # if Mac
setwd("C:/user/Desktop/POL269") # if Windows
library(tidyverse) # loads tidyverse package

## STEP 2: Load the dataset 
leaders <- read.csv("leaders.csv") # reads and stores data

## STEP 3: Understand the data
head(leaders) # shows first observations
## the treatment variable is ____
## the outcome variable is ____

## STEP 4: Compute difference-in-means estimator by fitting
## a simple linear regression model where X is the treatment
## variable and Y is the outcome variable of interest
lm(leaders$polityafter ~ leaders$died) # fits linear model
## interpretation of beta-hat? 

## STEP 5: Identify potential confounding variables
## Calculate the average politybefore for the two groups
leaders |> group_by(died) |> summarise(mean_politybefore = mean(politybefore))


## STEP 6: Estimate average causal effect while controlling
## for confounders by fitting a multiple linear regression model
lm(leaders$polityafter ~ leaders$died + leaders$politybefore)
## interpretation of beta-hat-sub1?