DiscoR
DiscoR

Reputation: 247

How to add baseline values of outcome at time = 0 as a fixed effect in mixed effect model?

My study was a 12 week long study involving two diets (diet). Outcome measurements such as weight, waist circumference etc. were taken at time (time) = 0,6,and 12 week intervals. (Code) stands for subjects.

I tried the following code to create a new variable called weight0 to only pick outcome values at time = 0.

weight0 <- dat2$weight[dat2$time==0] 

I created the following model.

p1 <- lme4::lmer(weight ~ diet * time + weight0 + weight0 * time + (time | code), REML = FALSE, data = dat2)

Error in model.frame.default(data = dat2, drop.unused.levels = TRUE, formula = weight ~  : 
variable lengths differ (found for 'weight0')

I think I understand what is going on. The values of weight 0 are not the same length as values of the outcome weight. Is this correct? How do I correct for this and use baseline as a fixed effect?

Thanks for taking the time to read this.

Upvotes: 0

Views: 60

Answers (1)

Dimitris Rizopoulos
Dimitris Rizopoulos

Reputation: 350

You can do the following to define weight0:

dat2 <- dat2[order(dat2$code, dat2$time), ]
dat2$weight0 <- with(dat2, ave(weight, code, FUN = function (x) x[1]))

and then fit the model.

Upvotes: 0

Related Questions