Consider the following (small subset) of my entire dataset.
data<-data.frame(c("R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12"),
c(2005,2005,2005,2006,2006,2006,2005,2005,2005,2006,2006,2006), c(0,0,0,0,0,0,1,1,1,1,1,1),
c(1,0,1,1,0,1,1,1,1,0,0,1))
colnames(data)<-c("id", "time", "group", "med.use")
data$group[data$group==1]<-"treated"
data$group[data$group==0]<-"control"
data$time[data$time==2005]<-"prior_reform"
data$time[data$time==2006]<-"after_reform"
The goal of this exercise is to provide a simple difference in difference table. To do so, we have a treatment group and a control group which we observe before and after treatment. The outcome I am interested in is medication use. In order to get the means per group, we can simply use the tapply function. So we get
mean.meduse<-tapply(data$med.use, INDEX=data[,c("group", "time")], function(x) {mean(x, na.rm=TRUE)})
mean.meduse
#declare it a dataframe
mean.meduse<-as.data.frame(mean.meduse)
#calculate differences
mean.meduse$dif<-mean.meduse$after_reform-mean.meduse$prior_reform
mean.meduse[3,]<-mean.meduse[1,]-mean.meduse[2,]
rownames(mean.meduse)[3]<-"difference"
In the steps thereafter, I declare it a dataframe such that we can calculate also the difference between both groups before and after treatment. Now the question is how I could do the same thing for the standard errors. In other words, how would I get the standard errors for all 9 cells displayed in the table "mean.meduse" I created?
I know how I can get standard errors for the per group setting. I would simply use:
summary <- data %>% group_by(time, group) %>%
summarise(mean_before=mean(med.use,na.rm=T),
var=var(med.use,na.rm=T),
count=as.numeric(sum(!is.na(med.use)))),
summary$se<-sqrt(summary$var/summary$count)
The drawback of this approach is that I do not have all the standard errors that I am looking for in one table. As a consequence, I need to make another table to find the standard error of the mean.meduse of the difference prior and after the reform. The same holds as well for the standard error for the difference in difference mean (the 0.6666 at the right bottom of the table mean.meduse). So I am wondering whether there is a way to change the above line of code such that I can get all the datapoints (summarised in mean med.use) and the corresponding standard error in one table.
I appreciate any help.