在R中提取回归P值

时间:2021-04-12 23:01:57

I am performing multiple regressions on different columns in a query file. I've been tasked with extracting certain results from the regression function lm in R.

我在查询文件中的不同列上执行多次回归。我的任务是从R中的回归函数lm中提取某些结果。

So far I have,

到目前为止,我有,

> reg <- lm(query$y1 ~ query$x1 + query$x2)
> summary(reg)

Call:
lm(formula = query$y1 ~ query$x1 + query$x2)

Residuals:
    1     2     3     4 
  7.68 -4.48 -7.04  3.84 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept)  1287.26     685.75   1.877    0.312
query$x1      -29.30      20.92  -1.400    0.395
query$x2     -116.90      45.79  -2.553    0.238

Residual standard error: 11.97 on 1 degrees of freedom
Multiple R-squared:  0.9233,    Adjusted R-squared:  0.7699 
F-statistic: 6.019 on 2 and 1 DF,  p-value: 0.277

To extract the coefficients, r-squared and F statistics I use the following:

要提取系数,r平方和F统计数据,我使用以下内容:

reg$coefficients
summary(reg)$r.squared
summary(reg)$fstatistic

I would like to also extract the p-value of 0.277.

我想提取0.2值的p值。

Is there a piece of code that could do this?

是否有一段代码可以做到这一点?

Thanks

3 个解决方案

#1


7  

I would recommend using the "broom" package as a good practice to go forward with those cases (where you might need to create a data frame from a model fit output).

我建议使用“扫帚”软件包作为一种良好的做法,继续处理这些情况(您可能需要从模型拟合输出创建数据框)。

Check this as a simple example:

检查这个简单的例子:

library(broom)

dt = data.frame(mtcars) # example dataset

model = lm(mpg ~ disp + wt, data = dt) # fit a model

summary(model) # usual summary of a model fit

tidy(model) # get coefficient table as a data frame

glance(model) # get rest of stats as a data frame

glance(model)$p.value # get p value

#2


5  

You could by using anova(reg)$'Pr(>F)'

你可以使用anova(reg)$'Pr(> F)'

#3


0  

The two easiest ways I've found for extracting the p-value are:

我发现提取p值的两种最简单的方法是:

summary(Model)$coefficients[,"Pr(>|t|)"][2]

summary(Model)$coefficients[2,4]

Just replace "Model" with the name of your model

只需将“Model”替换为您的模型名称即可

#1


7  

I would recommend using the "broom" package as a good practice to go forward with those cases (where you might need to create a data frame from a model fit output).

我建议使用“扫帚”软件包作为一种良好的做法,继续处理这些情况(您可能需要从模型拟合输出创建数据框)。

Check this as a simple example:

检查这个简单的例子:

library(broom)

dt = data.frame(mtcars) # example dataset

model = lm(mpg ~ disp + wt, data = dt) # fit a model

summary(model) # usual summary of a model fit

tidy(model) # get coefficient table as a data frame

glance(model) # get rest of stats as a data frame

glance(model)$p.value # get p value

#2


5  

You could by using anova(reg)$'Pr(>F)'

你可以使用anova(reg)$'Pr(> F)'

#3


0  

The two easiest ways I've found for extracting the p-value are:

我发现提取p值的两种最简单的方法是:

summary(Model)$coefficients[,"Pr(>|t|)"][2]

summary(Model)$coefficients[2,4]

Just replace "Model" with the name of your model

只需将“Model”替换为您的模型名称即可