Skip to content

Instantly share code, notes, and snippets.

@BioSciEconomist
Last active September 11, 2020 22:17
Show Gist options
  • Save BioSciEconomist/e5c58b2997a52addee1f98cc1aff0333 to your computer and use it in GitHub Desktop.
Save BioSciEconomist/e5c58b2997a52addee1f98cc1aff0333 to your computer and use it in GitHub Desktop.
compare repeated measures model with random effects with DID
# *-----------------------------------------------------------------
# | PROGRAM NAME: ex DID vs repeated measures
# | DATE: 9/9/20
# | CREATED BY: MATT BOGARD
# | PROJECT FILE: https://gist.github.com/BioSciEconomist
# *----------------------------------------------------------------
# | PURPOSE: compare repeated measures model with random effects with DID
# *----------------------------------------------------------------
# see also: http://econometricsense.blogspot.com/2018/02/intuition-for-random-effects.html
library(dplyr)
library(reshape)
# ref: https://stats.stackexchange.com/questions/194888/difference-in-difference-vs-repeated-measures
cards <- "subj group t1 t2 diff
1 A 5 6 1
2 A 5 7 2
3 A 6 9 3
4 A 6 10 4
5 A 5 7 2
6 A 5 7 2
7 A 6 9 3
8 A 6 9 3
9 A 2 2 0
10 A 2 3 1
11 B 10 11 1
12 B 10 11 1
13 B 9 11 2
14 B 9 11 2
15 B 10 18 8
16 B 10 21 11
17 B 12 18 6
18 B 12 19 7
19 B 5 20 15
20 B 5 20 15"
df <- read.table(textConnection(cards), header = TRUE)
closeAllConnections()
df1 <- melt(data=df, id.vars =c('subj', 'group'), measure.vars = c('t1', 't2'))
names(df1) = c("subj","group","time","value")
# DID model
summary(glm(value ~ group + time + group*time, data=df1))
# repeated measures model with random effect for subject
library(nlme)
mix<-lme(value ~ group*time, random=(~1 | subj), data = df1)
summary(mix)
#---------------------------------
# additional examples repeated measures
#---------------------------------
cards <- "id group pulse time
1 1 35 1
1 1 25 2
1 1 16 3
2 1 32 1
2 1 23 2
2 1 12 3
3 1 36 1
3 1 22 2
3 1 14 3
4 1 34 1
4 1 21 2
4 1 13 3
5 2 57 1
5 2 43 2
5 2 22 3
6 2 54 1
6 2 46 2
6 2 26 3
7 2 55 1
7 2 46 2
7 2 23 3
8 2 60 1
8 2 47 2
8 2 25 3"
df <- read.table(textConnection(cards), header = TRUE)
closeAllConnections()
## Convert variables to factor
df2 <- within(df, {
group <- factor(group)
time <- factor(time)
id <- factor(id)
})
#
# visualize
#
with(df2, interaction.plot(time, group, pulse,
ylim = c(10, 60), lty = c(1, 12), lwd = 3,
ylab = "mean of pulse", xlab = "time", trace.label = "group"))
#
# explore
#
# group differences
df2%>%
group_by(group) %>%
summarize(pulse = mean(pulse))
# differences across time
df2%>%
group_by(time) %>%
summarize(pulse = mean(pulse))
# group and time interactions
df2%>%
group_by(group,time) %>%
summarize(pulse = round(mean(pulse),2))
#
# standard regression
#
summary(glm(pulse ~ group + time + group*time, data = df2))
#
# repeated measures anova
#
summary(aov(pulse ~ group * time + Error(id), data = df2))
#
# linear mixed model with random effects
#
library(nlme)
summary(lme(pulse ~ group*time, random = (~1 | id), data = df2))
library(lme4)
summary(lmer(pulse ~ group*time + (1 | id), data = df2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment