R中的提取回归P值

q3qa4bjr  于 2023-01-18  发布在  其他
关注(0)|答案(5)|浏览(236)

我正在对一个查询文件中的不同列执行多重回归,我的任务是从R中的回归函数lm中提取某些结果。
到目前为止,

> 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

为了提取系数、r平方和F统计量,我使用以下公式:

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

我还想提取p值0.277。
有没有一段代码可以做到这一点?
谢谢

yeotifhr

yeotifhr1#

我建议使用“扫帚”包作为处理这些情况(可能需要从模型拟合输出创建数据框)的良好实践。
下面是一个简单的例子:

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
blpfk2vs

blpfk2vs2#

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

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

summary(Model)$coefficients[,4]

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

mlnl4t2r

mlnl4t2r3#

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

dy2hfwbg

dy2hfwbg4#

您还可以用途:

pf(summary(reg)$fstatistic[1],
summary(reg)$fstatistic[2],
summary(reg)$fstatistic[3],
lower.tail=FALSE)
wwodge7n

wwodge7n5#

这是非常有帮助的!我唯一可以补充的是,用户也可以像这样访问系数值,例如,如果用户想向具有拟合值的现有数据集添加一列:所有变量$预测=所有变量$x* 汇总(拟合)$系数[2,1] +汇总(拟合)$系数[1,1]
很明显,残差是:所有变量$残差=所有变量$y -(所有变量$x* 汇总(拟合)$系数[2,1]
再次感谢ADR

相关问题