从线性回归中提取 p 值和 r 平方 [英] pull out p-values and r-squared from a linear regression

查看:168
本文介绍了从线性回归中提取 p 值和 r 平方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从简单的线性回归模型中提取 p 值(因为单个解释变量的系数不为零)和 R 平方值?例如...

How do you pull out the p-value (for the significance of the coefficient of the single explanatory variable being non-zero) and R-squared value from a simple linear regression model? For example...

x = cumsum(c(0, runif(100, -1, +1)))
y = cumsum(c(0, runif(100, -1, +1)))
fit = lm(y ~ x)
summary(fit)

我知道 summary(fit) displays p 值和 R 平方值,但我希望能够将它们粘贴到其他变量中.>

I know that summary(fit) displays the p-value and R-squared value, but I want to be able to stick these into other variables.

推荐答案

r-squared:可以直接从汇总对象 summary(fit)$r 返回 r-squared 值.平方.请参阅 names(summary(fit)) 以获取您可以直接提取的所有项目的列表.

r-squared: You can return the r-squared value directly from the summary object summary(fit)$r.squared. See names(summary(fit)) for a list of all the items you can extract directly.

Model p-value:如果要获得整体回归模型的p-value,这篇博文概述了返回 p 值的函数:

Model p-value: If you want to obtain the p-value of the overall regression model, this blog post outlines a function to return the p-value:

lmp <- function (modelobject) {
    if (class(modelobject) != "lm") stop("Not an object of class 'lm' ")
    f <- summary(modelobject)$fstatistic
    p <- pf(f[1],f[2],f[3],lower.tail=F)
    attributes(p) <- NULL
    return(p)
}

> lmp(fit)
[1] 1.622665e-05

在具有一个预测变量的简单回归的情况下,模型 p 值和系数的 p 值将相同.

In the case of a simple regression with one predictor, the model p-value and the p-value for the coefficient will be the same.

系数 p 值:如果您有多个预测变量,则以上将返回模型 p 值,并且可以使用以下方法提取系数的 p 值:

Coefficient p-values: If you have more than one predictor, then the above will return the model p-value, and the p-value for coefficients can be extracted using:

summary(fit)$coefficients[,4]  

或者,您可以以与上述摘要对象类似的方式从 anova(fit) 对象中获取系数的 p 值.

Alternatively, you can grab the p-value of coefficients from the anova(fit) object in a similar fashion to the summary object above.

这篇关于从线性回归中提取 p 值和 r 平方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆