[R] remove fixed effect in predict.merMod
Alexander Shenkin
ashenkin at ufl.edu
Thu May 19 17:30:53 CEST 2016
Hello all,
I've run a model, and now would like to extract residuals. However, I
have set sum-to-zero contrasts for the categorical fixed effect
(contr.sum). Because I am interested in looking at the variation in the
residuals associated with that fixed effect (along with other levels), I
need to calculate residuals setting that fixed effect to zero. Any
thoughts on how to do this?
thanks,
allie
contr.sum.keepnames <- function(...) {
# make deviation contrasts that don't lose the names of the factors
in the model results
# from
https://stackoverflow.com/questions/10808853/why-does-changing-contrast-type-change-row-labels-in-r-lm-summary
conS <- contr.sum(...)
colnames(conS) = rownames(conS)[-length(rownames(conS))]
conS
}
test_df = data.frame(site = rep(LETTERS[1:10], 10), resp = runif(100),
pred = runif(100))
contrasts(test_df$site) = contr.sum.keepnames(levels(test_df$site))
mod = lmer(resp ~ (1 + pred|site) + pred, data = test_df)
residuals = test_df$resp - predict(mod, REform=NA) # I would like the
site effect here to be set to zero
More information about the R-help
mailing list