How to extract p-value and r-squared from a linear regression model in R? Explain
We can see the p-value and r-squared value using summary function
Let us explain with an example
x_data = cumsum(c(0, runif(100, -1, +1)))
y_data = cumsum(c(0, runif(100, -1, +1)))
fit = lm(y_data ~ x_data)
summary(fit)
Now summary function will show the following values
Standard error,estimates, t-values, p-values.
R-squared is calculated based on the following function
summary(fit)$r.squared
We can also use cor.test function to see the p-value and r-squared value
x_data <- c(44.4, 45.9, 41.9, 53.3, 44.7, 44.1, 50.7, 45.2, 60.1)
y_data<- c( 2.6, 3.1, 2.5, 5.0, 3.6, 4.0, 5.2, 2.8, 3.8)
mycor = cor.test(x_data,y_data)
model = lm(x_data~y_data)
# r and rsquared:
cor.test(x_data,y_data)$estimate ** 2
cor
summary(lm(x_data~y_data))$r.squared
# P.value
lmp(lm(x_data~y_data)) Chase's answer
cor.test(x_data,y_daa)$p.value