在R中从回归(lm)输出中提取最终p值

3

我有以下数据和代码:

> res = lm(vnum1~vnum2+vch1, data=rndf)
> sumres=summary(res)
> 
> sumres

Call:
lm(formula = vnum1 ~ vnum2 + vch1, data = rndf)

Residuals:
     Min       1Q   Median       3Q      Max 
-1.48523 -0.42050  0.05919  0.43710  1.93554 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)  
(Intercept)  -1.0265     1.0192  -1.007   0.3310  
vnum2         1.9538     0.9665   2.022   0.0628 .
vch1B        -0.7072     0.8386  -0.843   0.4132  
vch1C         0.5502     0.8546   0.644   0.5301  
vch1D        -0.6556     0.8412  -0.779   0.4488  
vch1E         0.1461     0.8418   0.174   0.8647  
---
Signif. codes:  0***0.001**0.01*0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.9181 on 14 degrees of freedom
Multiple R-squared:  0.2799,    Adjusted R-squared:  0.02275 
F-statistic: 1.088 on 5 and 14 DF,  p-value: 0.4088


> dput(rndf)
structure(list(vnum1 = c(-1.63272832611568, 0.225401613406123, 
-0.412759271404808, 0.0518634835165988, 0.130576187815585, 0.393254112514486, 
-0.22429939238377, -1.01640685392138, -0.5419194916071, 0.602275306119663, 
-0.378031662946265, -0.357452340621538, 0.178526276590386, -0.138016672074599, 
2.13719092448509, 1.03443214036885, 1.34821211116271, -0.718873325233001, 
1.80014304090489, -0.497878912730538), vnum2 = c(0.168299512239173, 
0.624244463164359, 0.0156862761359662, 0.450781079474837, 0.622718085534871, 
0.285390306729823, 0.911491815699264, 0.500363457249478, 0.566354847047478, 
0.942464957712218, 0.00690335803665221, 0.860874759964645, 0.786528263241053, 
0.337976476177573, 0.346998119959608, 0.549394505331293, 0.71448978385888, 
0.865091580431908, 0.967393533792347, 0.539990464225411), vch1 = structure(c(3L, 
5L, 5L, 3L, 3L, 3L, 1L, 5L, 4L, 2L, 3L, 4L, 4L, 3L, 3L, 3L, 1L, 
2L, 5L, 2L), .Label = c("A", "B", "C", "D", "E"), class = "factor")), .Names = c("vnum1", 
"vnum2", "vch1"), class = "data.frame", row.names = c(NA, -20L
))

我可以从sumres$r.squared和sumres$adj.r.squared获得R平方和调整后的R平方值。但是我无法从res或sumres中获取最终的p值0.4088。我该如何获得这个值?谢谢你的帮助。

1个回答

4
你可以通过输入以下代码查看打印摘要的代码:
class(sumres)
#> "summary.lm"

要获取该类,然后通过输入以下代码来获取打印方法的代码:

stats:::print.summary.lm

在控制台中输入以下内容:

 cat(...lots of stuff..., "p-value:", format.pval(pf(x$fstatistic[1L], 
            x$fstatistic[2L], x$fstatistic[3L], lower.tail = FALSE), 
            digits = digits)...morestuff...)

在这种情况下,你需要做的是:
pf(sumres$fstatistic[1L], sumres$fstatistic[2L], sumres$fstatistic[3L], lower.tail = FALSE)

抱歉,p值现在在答案中。 - Jthorpe
1
我得到了这个数据的正确值,但对于我的真实大数据,值是不同的:7.374763e-196,而实际值是<2.2e-16。我需要在你的方程中加入样本大小吗? - rnso
sumres$fstatistic[2L]sumres$fstatistic[3L] 分别是 F 统计量分子和分母的自由度。不同之处在于,在打印方法中,打印到控制台的数字是通过调用format.pval(7.374763e-196) 进行格式化的。 - Jthorpe
感谢您的专业知识。 - rnso

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接