I want to save my output regression of lmer() from lme4 R package. Is there any good way for this to get the out put below in a table e.g .csv or or .txt or .html, etc?
Fixed effects:
Estimate Std. Error df t value Pr(>|t|)
(Intercept) 103.989 5.617 139.000 18.52 < 2e‐16 ***
age ‐0.172 0.177 139.000 ‐1.03 0.304
bmi 0.597 0.229 139.000 2.56 0.012 *
gender 1.019 0.325 139.000 3.15 0.002 **
I tried, tab_model() from library sjplot in R, but it does not give the SE, df and t value. I would like to save the output above. I appreciate any advice.
CodePudding user response:
Make sure the class of your model object is lmerMod and it will work with stargazer, which exports beautiful formatted regression tables to plain text, html, latex, etc. and has all sort of options to customize those tables (see the docs).
# class(mod)<- "lmerMod"
mod <- lme4::lmer(Ozone ~ Temp (1|Month),
data = airquality)
stargazer::stargazer(mod)
stargazer::stargazer(mod, type = "html")
CodePudding user response:
Update:to write to textfile:
library(lme4)
m1 <- lmer(drat ~ wt (1 wt|cyl), data=mtcars)
library(broom.mixed)
library(dplyr)
df<- m1 %>%
tidy()
write.table(df,"filename.txt",sep="\t",row.names=FALSE)
OR
m1 %>%
tidy() %>%
write.table(.,"filename.txt",sep="\t",row.names=FALSE)
"effect" "group" "term" "estimate" "std.error" "statistic"
"fixed" NA "(Intercept)" 4.67281034450577 0.344833957358875 13.5508996280279
"fixed" NA "wt" -0.344238767944164 0.0911701519816392 -3.77578363600283
"ran_pars" "cyl" "sd__(Intercept)" 0.374914148920673 NA NA
"ran_pars" "cyl" "cor__(Intercept).wt" -1 NA NA
"ran_pars" "cyl" "sd__wt" 0.0839046849277359 NA NA
"ran_pars" "Residual" "sd__Observation" 0.370192153038516 NA NA
One way could be using broom.mixed package as suggested by @
user63230 in the comments section:
Here is an example:
library(lme4)
m1 <- lmer(drat ~ wt (1 wt|cyl), data=mtcars)
library(broom.mixed)
library(dplyr)
m1 %>%
tidy()
effect group term estimate std.error statistic
<chr> <chr> <chr> <dbl> <dbl> <dbl>
1 fixed NA (Intercept) 4.67 0.345 13.6
2 fixed NA wt -0.344 0.0912 -3.78
3 ran_pars cyl sd__(Intercept) 0.375 NA NA
4 ran_pars cyl cor__(Intercept).wt -1 NA NA
5 ran_pars cyl sd__wt 0.0839 NA NA
6 ran_pars Residual sd__Observation 0.370 NA NA
