如何在 R 中從線性迴歸中提取 p 值和 R 平方?


我們可以使用帶有 $r.squared 的迴歸模型物件名稱來查詢 R 平方,並使用使用者自定義函式來提取 p 值。

示例

提取 R 平方

> x<-c(32,37,68,87,32,43)
> y<-c(12,8,6,3,5,3)
> LinearRegression<-lm(y~x)
> summary(LinearRegression)$r.squared
[1] 0.2814271

提取 p 值

> Regressionp <- 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)
> Regressionp(LinearRegression)
[1] 0.2789025

更新時間:2020-07-06

1K+ 瀏覽量

開啟你的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.