extract r squared from lm in r

To extract R-squared from a linear regression model (lm) in R, you can follow these steps:

  1. Fit a linear regression model using the lm() function, for example:
model <- lm(y ~ x, data = dataset)

Replace y, x, and dataset with the appropriate variable names.

  1. Use the summary() function to obtain the summary of the linear regression model:
summary(model)
  1. Extract the R-squared value from the summary:
r_squared <- summary(model)$r.squared

By following these steps, you can fit a linear regression model, obtain the summary, and extract the R-squared value in R.