[英]how to save the output of regression in a table in R
I want to save my output regression of lmer()
from lme4
R package.我想从
lme4
R 包中保存lmer()
的输出回归。 Is there any good way for this to get the output below in a table eg .csv
or .txt
or .html
etc?有什么好的方法可以在表格中获得下面的输出,例如
.csv
或.txt
或.html
等?
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
values.我尝试了 R 中库
sjplot
中的tab_model()
,但它没有给出 SE、 df
和t
值。 I would like to save the output above.我想保存上面的输出。 I appreciate any advice.
我很感激任何建议。
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).确保您的模型对象的类是
lmerMod
并且它将与stargazer
一起使用,它将漂亮的格式化回归表导出为纯文本、html、latex 等,并具有自定义这些表的各种选项(请参阅文档)。
# class(mod)<- "lmerMod"
mod <- lme4::lmer(Ozone ~ Temp + (1|Month),
data = airquality)
stargazer::stargazer(mod)
stargazer::stargazer(mod, type = "html")
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:一种方法是使用评论部分中@ user63230 建议的
broom.mixed
包:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.