繁体   English   中英

如何将相应的统计数据从线性回归模型导出到R中的表格?

[英]How can I export correspondent statistics from linear regression model to a table in R?

我拟合一个多元线性回归模型,其中包含1个因变量和45个独立变量。 我想把对应的效果,95%CI和p值放到表格中。

我的数据集如下:

a  b  x1 x2 x3 x4 .... x45
23 15 1 34 4  45       8
10 45 2 15 2  55       18

是否有更方便的方法来编写回归语法而不是写lm(y ~ x1 + x2 + x3 + x4 ... +x45, data=df)因为我有45个不同名称的变量?

我试过了

mod1 <- lm(a ~ x1 + x2 + ....+x45, data=df)
mod2 <- lm(b ~ x1 + x2 + .... +x45, data=df)

但我不知道如何将结果列入表格如下:

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

如果有人能提供帮助,那就太棒了。 非常感谢!

当我使用lmer函数进行回归时,此代码可以提取结果表。 请参考代码!

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

假设您的数据集仅包含因变量和相关预测变量,您可以像这样指定回归:

lm(a ~ ., data = df)

在您的情况下,您可以使用dplyr::select删除ba

library(dplyr
mod1 <- df %>%
  select(-b) %>%
  lm(a ~ ., data = .)

要总结表中的结果,请查看R包stargazersjPlot扫帚 我喜欢sjPlot::tab_model因为它在RStudio或RMarkdown文档中生成干净的HTML表。

library(sjPlot)
iris %>%
  lm(Petal.Length ~ ., data = .) %>%
  tab_model()

在此输入图像描述

或使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM