1
$\begingroup$

I have a multilevel model based on data from weekly observations nested in persons. The cluster ist id. We measured fatigue several times. I expect that there may be a trend over time, i.e. persons should differ in their slopes. Hence, I specified a random intercept random slope model:

model <- lmer(fatigue ~ 1 + time + (1 + time|id),data = dataframe, na.action = na.omit)
summary(model)

The coef-command does provide the estimated/calculated intercepts and slopes for each person/cluster/id. The output looks like this. Obviously the number in the first column refers to the id

> coef(model)$id
    (Intercept)          time
2      2.730255 -0.0030445718
3      3.109098  0.0029101983
4      1.526722 -0.0219620556
5      2.662696 -0.0041064863
11     2.640430 -0.0044564646
12     3.560910  0.0100119139
13     2.698792 -0.0035391108

View(coef(model)$id[,"(Intercept)"])
    View(coef(model)$id[,"time"])

Extracting either intercepts or slopes seperately as vectors works fine.

I would now like to use the individual (id-specific) intercepts and slopes from the output as Level-2-variables (e.g. as covariates or cross-level moderators) of growth in just another criterion/dependent variable. That is, I need to have a data.frame containing: 1. id 2. Intercept 3. Slope

Whenever I try to extract the contents of the output, I do not succeed capturing the information from the first column (obviously the id). Working with the Intercept and Slope vector is fine. However R doesn't treat the first column as a vector/variable. So it is not possible to match intercepts and slopes to a Level-2-dataset containing other covariates like tenure or age.

Is someone aware of ways how to manage an export of the full output including the id column? I'd appreciate any hint. Thanks in advance.

Best Oliver

$\endgroup$

closed as off-topic by Jake Westfall, Michael Chernick, mdewey, Juho Kokkala, John Apr 24 '17 at 17:17

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – Jake Westfall, Michael Chernick, mdewey, Juho Kokkala, John
If this question can be reworded to fit the rules in the help center, please edit the question.

1
$\begingroup$

I suspect this should be migrated to StackOverflow as a more programming- rather than statistics-related question, but I'll take a guess that you want rownames():

library("lme4")
fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
dd <- ranef(fm1)[["Subject"]]
dd <- cbind(Subject=rownames(dd),dd)
rownames(dd) <- NULL
head(dd)
##   Subject (Intercept)       Days
## 1     308    2.258565  9.1989719
## 2     309  -40.398577 -8.6197032
## 3     310  -38.960246 -5.4488799
## 4     330   23.690498 -4.8143313
## 5     331   22.260203 -3.0698946
## 6     332    9.039526 -0.2721707

dplyr has a utility function that does this, although you'd still have to adjust the column name afterwards:

dplyr::add_rownames(coef(fm1)[["Subject"]])
$\endgroup$
  • $\begingroup$ Thank you very much. This procedure works really fine. For some reason I have trouble using the add_rownames-command (unrecognized command) although dplyr is installed and activated. I pragmatically used the names-command which works well, too: names(dd)[c(2)] <- c("intercept01") names(dd)[c(3)] <- c("slope01") $\endgroup$ – Oliver Weigelt Jul 29 '15 at 6:03
  • $\begingroup$ you can collapse that to names(dd)[2:3] <- c("intercept01","slope01") $\endgroup$ – Ben Bolker Jul 29 '15 at 10:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.