Skip to content
Prev 8945 / 20628 Next

Two Questions re: Piecewise Mixed Effects Models

Hi Eric,

The first part of this code is reproducible and I suggest you run it
to see the graph and the output as I hope it will help you understand.

## simulate some data
set.seed(10)
y <- rnorm(1000, mean = (time <- sample(-2:2, 1000, TRUE)) * ((time >
0) + .5), sd = 1)

# plot time and the outcome y, you can see the 'break' sort of
plot(jitter(time), y)

# fit a non broek and broken time model
# also get the predicted values for times -2, -1, 0, 1, and 2
yhat1 <- predict(m1 <- lm(y ~ time), data.frame(time = -2:2))
yhat2 <- predict(m2 <- lm(y ~ time + time:I(time > 0)), data.frame(time = -2:2))

# add lines of predicted values from the different models
lines(-2:2, yhat1, col = "black", lwd = 3)
lines(-2:2, yhat2, col = "blue", lwd = 3)

# summaries of the models so you can see the coefficients
summary(m1)
summary(m2)

# what you have is an intercept and slope for time <= 0
# these are called intercept and time
# then for time > 0, you get an adjustment to the time slope (the
interaction term)
# the final design matrix would look something like:
cbind(Int = 1, Time = -2:2, Time2Adj = (-2:2) * (-2:2 > 0))


This part of the code is not reproducible, but I think this is more
like the model you probably want in glmer()

model <- glmer(rate ~ policy + time.before + time.after + Z +
  (1+ time.before + time.after | Site) + offset(log(people)),
  data=data, family="poisson")

# this gives you fixed effects for policy and Z
# and random effects for the intercept, and two time slopes
# that are allowed to vary across levels in Site
# all random effects have freely estimates covariances, so that is a 3
x 3 matrix
# (intercept, time.before, time.after) with all elements estimated

This is roughly (exactly?) equivalenet to the SAS model using proc glimmix:

proc glimmix data=data method=LAPLACE;
  class Site;
  model rate = policy time.before time.after Z / dist=poisson offset=people;
  random intercept time.before time.after / subject=Site type = chol;
/* you may be more familiar with type = un for unstructured */
/*  random intercept time.before time.after / subject=Site type = un; */
run;

Hope this helps,

Josh
On Sun, Sep 30, 2012 at 2:20 AM, Lofgren, Eric <elofgren at email.unc.edu> wrote: