How to extract p-value and R-squared from a linear regression in R?


We can use regression model object name with $r.squared to find the R-squared and a user defined function to extract the p-value.

Example

Extracting R-Squared

> 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

Extracting p-value

> 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

Updated on: 06-Jul-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements