-1

I am following this tutorial I found online on a difference in difference example in R. (https://www.princeton.edu/~otorres/DID101R.pdf)

I was wondering how I would go about plotting this data in R and adding a line that reflects before and after the treatment year.

hojiko
  • 11
  • 1
  • 1
  • 2
    please add a reproducible example https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – TobiO Jul 17 '19 at 17:59

1 Answers1

0

Here is a basic solution that should offer a starting point for you. The points represent the means for treated==0 and treated==1, and group=1 connects the points in ggplot.

library(haven)
library(ggplot2)
library(tidyverse)

df <- read_dta("http://dss.princeton.edu/training/Panel101.dta")

df$treated <- as.factor(ifelse(df$year > 1993, 1, 0))

t.test(df$y~df$treated)

df %>% group_by(treated) %>% summarise(t=mean(y,na.rm=1)) %>% 
  ggplot(aes(treated,t,group=1)) + geom_point() + geom_line()

enter image description here

kstew
  • 1,104
  • 6
  • 21