[英]How can I export correspondent statistics from linear regression model to a table in R?
I am fitting a multiple linear regression model with 1 dependent variable and 45 independent variables. 我拟合一个多元线性回归模型,其中包含1个因变量和45个独立变量。 I would like to put the correspondent effect, 95% CI and p-value to table.
我想把对应的效果,95%CI和p值放到表格中。
My data set is as below: 我的数据集如下:
a b x1 x2 x3 x4 .... x45
23 15 1 34 4 45 8
10 45 2 15 2 55 18
Is there any more convenient way to write the regression syntax rather than writing lm(y ~ x1 + x2 + x3 + x4
... +x45, data=df)
as I have 45 variables with different names? 是否有更方便的方法来编写回归语法而不是写
lm(y ~ x1 + x2 + x3 + x4
... +x45, data=df)
因为我有45个不同名称的变量?
I've tried 我试过了
mod1 <- lm(a ~ x1 + x2 + ....+x45, data=df)
mod2 <- lm(b ~ x1 + x2 + .... +x45, data=df)
But I don't know how to put the outcomes into table as following: 但我不知道如何将结果列入表格如下:
model variable effect 95%CI p
mod1 x1 177.93 79.16- 276.71 0.003
mod1 x2 -75.13 -116.46 - -33.8 0.003
...
mod1 x45 118.61 53.09-184.13 0.005
mod2 x1 79.53 36.94 - 122.13 0.004
mod2 x2 201.93 60.48 - 343.38 0.01
...
mod2 x45 61.56 20.87 - 102.25 0.005
It would be great if anyone can help. 如果有人能提供帮助,那就太棒了。 Many thanks!
非常感谢!
When I used lmer function for the regression, this code can extract the table of the results. 当我使用lmer函数进行回归时,此代码可以提取结果表。 Please refer the code!
请参考代码!
RESULT_model1 <- summary(model1<-lmer(LOGTHSMON_SELNG_AMT~ .-BLK_CD-LOGPERTHSMON_SELNG_AMT
- STDR_YM_CD - bub_dong-M201507-M201508-M201509
-STOR_CO
+(1|BLK_CD)
,data=dta_fin_WEST))
RESULT_model1_cof <- as.data.frame(tidy(RESULT_model1$coefficients))
RESULT_model1_cof$p.value <- 2 * (1 - pnorm(abs(RESULT_model1_cof$t.value)))
RESULT_model1_cof$VAR <- RESULT_model1_cof$.rownames
RESULT_model1_cof$STDERR <- RESULT_model1_cof$Std..Error
Assuming that your dataset contains only the dependent variable and the relevant predictors, you can specify the regression like this: 假设您的数据集仅包含因变量和相关预测变量,您可以像这样指定回归:
lm(a ~ ., data = df)
In your case you could use dplyr::select
to remove b
or a
: 在您的情况下,您可以使用
dplyr::select
删除b
或a
:
library(dplyr
mod1 <- df %>%
select(-b) %>%
lm(a ~ ., data = .)
To summarise outcomes in a table, look at the R packages stargazer or sjPlot or broom . 要总结表中的结果,请查看R包stargazer或sjPlot或扫帚 。 I like
sjPlot::tab_model
as it generates clean HTML tables in RStudio or in RMarkdown documents. 我喜欢
sjPlot::tab_model
因为它在RStudio或RMarkdown文档中生成干净的HTML表。
library(sjPlot)
iris %>%
lm(Petal.Length ~ ., data = .) %>%
tab_model()
Or using broom::tidy
: 或使用
broom::tidy
:
library(broom)
iris %>%
lm(Petal.Length ~ ., data = .) %>%
tidy()
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) -1.11 0.270 -4.12 6.45e- 5
2 Sepal.Length 0.608 0.0502 12.1 1.07e-23
3 Sepal.Width -0.181 0.0804 -2.25 2.62e- 2
4 Petal.Width 0.602 0.121 4.96 1.97e- 6
5 Speciesversicolor 1.46 0.173 8.44 3.14e-14
6 Speciesvirginica 1.97 0.245 8.06 2.60e-13
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.