Data
This is my dput:
structure(list(Mins_Work = c(435L, 350L, 145L, 135L, 15L, 60L,
60L, 390L, 395L, 395L, 315L, 80L, 580L, 175L, 545L, 230L, 435L,
370L, 255L, 515L, 330L, 65L, 115L, 550L, 420L, 45L, 266L, 196L,
198L, 220L, 17L, 382L, 0L, 180L, 343L, 207L, 263L, 332L, 0L,
0L, 259L, 417L, 282L, 685L, 517L, 111L, 64L, 466L, 499L, 460L,
269L, 300L, 427L, 301L, 436L, 342L, 229L, 379L, 102L, 146L, NA,
94L, 345L, 73L, 204L, 512L, 113L, 135L, 458L, 493L, 552L, 108L,
335L, 395L, 508L, 546L, 396L, 159L, 325L, 747L, 650L, 377L, 461L,
669L, 186L, 220L, 410L, 708L, 409L, 515L, 413L, 166L, 451L, 660L,
177L, 192L, 191L, 461L, 637L, 297L), Coffee_Cups = c(3L, 0L,
2L, 6L, 4L, 5L, 3L, 3L, 2L, 2L, 3L, 1L, 1L, 3L, 2L, 2L, 0L, 1L,
1L, 4L, 4L, 3L, 0L, 1L, 3L, 0L, 0L, 0L, 0L, 2L, 0L, 1L, 2L, 3L,
2L, 2L, 4L, 3L, 6L, 6L, 3L, 4L, 6L, 8L, 3L, 5L, 0L, 2L, 2L, 8L,
6L, 4L, 6L, 4L, 4L, 2L, 6L, 6L, 5L, 1L, 3L, 1L, 5L, 4L, 6L, 5L,
0L, 6L, 6L, 4L, 4L, 2L, 2L, 6L, 6L, 7L, 3L, 3L, 0L, 5L, 7L, 6L,
3L, 5L, 3L, 3L, 1L, 9L, 9L, 3L, 3L, 6L, 6L, 6L, 3L, 0L, 7L, 6L,
6L, 3L), Work_Environment = c("Office", "Office", "Office", "Home",
"Home", "Office", "Office", "Office", "Office", "Office", "Home",
"Home", "Office", "Office", "Office", "Home", "Office", "Home",
"Home", "Office", "Office", "Home", "Office", "Home", "Home",
"Home", "Office", "Office", "Office", "Office", "Home", "Home",
"Home", "Office", "Office", "Office", "Office", "Office", "Home",
"Home", "Office", "Office", "Home", "Home", "Office", "Home",
"Home", "Office", "Office", "Home", "Home", "Office", "Home",
"Home", "Office", "Office", "Home", "Office", "Home", "Home",
"Home", "Home", "Office", "Home", "Office", "Office", "Home",
"Home", "Office", "Office", "Home", "Home", "Office", "Office",
"Home", "Office", "Office", "Home", "Office", "Office", "Home",
"Home", "Office", "Office", "Home", "Home", "Office", "Home",
"Home", "Office", "Office", "Home", "Office", "Office", "Home",
"Home", "Office", "Home", "Home", "Home")), class = "data.frame", row.names = c(NA,
-100L))
Problem
I have built this model and have written a printout for the fixed effect coefficients below:
lmer.work <- lmer(Mins_Work
~ Coffee_Cups
(1|Work_Environment),
data = work)
sum.work <- summary(lmer.work)
sum.work$coefficients
Which gives me a pretty standard read of the coefficients:
Estimate Std. Error df t value Pr(>|t|)
(Intercept) 210.17185 71.55028 1.306848 2.937401 1.594028e-01
Coffee_Cups 29.93377 7.28184 96.286964 4.110743 8.297325e-05
However, I would like to convert this into a data frame with the coefficient terms on the right with their own column name. However, converting this into a matrix or a data frame doesn't seem to work in assigning a default column name to the terms column:
as.matrix(sum.work$coefficients)
as.data.frame(sum.work$coefficients)
Which gives the same result:
Estimate Std. Error df t value Pr(>|t|)
(Intercept) 210.17185 71.55028 1.306848 2.937401 1.594028e-01
Coffee_Cups 29.93377 7.28184 96.286964 4.110743 8.297325e-05
I've also tried to just force column names in with this code, but it doesn't seem to work:
colnames(sum.work$coefficients) <- c("Term",
"Estimate",
"Standard.Error",
"DF",
"T Value",
"P Value")
The class
function seems to indicate it is both a matrix and an array. I'm not entirely sure how to change the column names in then if this is the case, but anybody with a solution would be helpful. I'm trying to later include this into a flextable
for a presentation.
CodePudding user response:
I'm not sure, if I understand you correctly. However, this might be a solution:
df <- as.data.frame(sum.work$coefficients)
df <- df %>%
rownames_to_column(var="Term")
CodePudding user response:
You can use the broom.mixed package.
library("broom.mixed")
tidy(lmer.work, "fixed")
#> # A tibble: 2 × 5
#> effect term estimate std.error statistic
#> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 fixed (Intercept) 210. 71.6 2.94
#> 2 fixed Coffee_Cups 29.9 7.28 4.11
It's easy to extract fixed and random effects, so it's a quite flexible solution.
tidy(lmer.work)
#> # A tibble: 4 × 6
#> effect group term estimate std.error statistic
#> <chr> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 fixed <NA> (Intercept) 210. 71.6 2.94
#> 2 fixed <NA> Coffee_Cups 29.9 7.28 4.11
#> 3 ran_pars Work_Environment sd__(Intercept) 91.6 NA NA
#> 4 ran_pars Residual sd__Observation 163. NA NA