How to Find Linear Regression in R
This guide shows you how to perform linear regression in R, from fitting a model with lm() to extracting coefficients, predictions, and diagnostics. You’ll enter your data, run a few commands, and get the regression equation, R-squared, and p-values that tell you how well your predictor explains the outcome.
What You’ll Need
- R installed (version 4.0 or newer recommended) and preferably RStudio.
- Your data in a format R can read: a CSV file, Excel file, or a built-in dataset.
- Basic familiarity with R syntax (assigning variables, using functions).
- No extra packages are required for simple linear regression; base R has everything.
Step-by-Step
- Load your data into R. If you have a CSV file, use
data <- read.csv("yourfile.csv"). For built-in datasets likemtcars, just typedata(mtcars). Make sure your columns are numeric (usestr(data)to check). - Check the relationship visually (optional but recommended). Run
plot(y ~ x, data = data)whereyis your outcome andxis your predictor. This helps spot non-linearity or outliers before fitting. - Fit the linear model. Use the
lm()function:model <- lm(y ~ x, data = data). Replaceyandxwith your actual column names. For multiple predictors, add them with+:lm(y ~ x1 + x2, data = data). - View the model summary. Type
summary(model)and press Enter. This gives you the coefficients (intercept and slopes), standard errors, t-values, p-values, and R-squared. The coefficient forxis the slope of the regression line. - Extract the coefficients directly. Use
coef(model)to get just the intercept and slopes as a named vector. For example,coef(model)["x"]gives the slope. - Make predictions. For new data, create a data frame with the same predictor names:
newdata <- data.frame(x = c(10, 20, 30)), thenpredict(model, newdata)to get predicted y-values. - Check model fit and assumptions. Run
plot(model)to see residual plots. Look for a roughly horizontal line in the residuals vs. fitted plot and a normal Q-Q plot that follows the diagonal. These help validate that linear regression is appropriate. - Get confidence intervals for coefficients. Use
confint(model)to see the 95% confidence intervals for the intercept and slopes. This tells you the range where the true coefficient likely lies. - Test if the overall model is significant. The F-statistic at the bottom of the summary output tests whether at least one predictor is useful. A p-value less than 0.05 (or your chosen threshold) indicates a significant model.
- Export or report results. To save the coefficients, use
write.csv(coef(model), "coefficients.csv"). For a quick report, you can copy the summary output into your document.
Common Mistakes
- Using the wrong variable order in the formula. The syntax is
y ~ x, notx ~ y. The outcome (dependent variable) goes on the left. - Forgetting to specify the
dataargument. If you omitdata =, R looks for variables in the global environment, which may cause errors or use the wrong columns. - Interpreting the intercept incorrectly. The intercept is the predicted y when all x are 0. If 0 is not meaningful for your predictor, the intercept may not be practically useful.
- Ignoring non-linear patterns. If your scatterplot shows a curve, a straight line may not fit well. Consider polynomial terms or transformations.
- Using summary() but not checking the residual standard error. The residual standard error (RSE) tells you the typical distance of points from the regression line; a large RSE relative to the mean of y indicates poor fit.
Tips
- Always start with a plot. It takes seconds and can reveal issues that numbers alone will hide.
- Use
names(model)to see all the components you can extract, likefitted.valuesandresiduals. - Standardize your predictors if they have very different scales, especially for multiple regression, to make coefficient comparisons easier.
- Check for multicollinearity in multiple regression using
car::vif(model)(requires thecarpackage). - Save your model with
saveRDS(model, "model.rds")so you can load it later withreadRDS()without refitting.
Troubleshooting
- Error: “object ‘y’ not found” – This usually means you didn’t specify
data =or the column name is misspelled. Check withnames(data). - Error: “NAs are not allowed” – Your data has missing values. Remove them with
na.omit(data)or usena.action = na.excludeinlm(). - Warning: “essentially perfect fit” – This happens when your predictor perfectly predicts the outcome (e.g., you accidentally included the outcome as a predictor). Double-check your formula.
- Summary shows NA for a coefficient – This often indicates perfect multicollinearity (one predictor is a linear combination of others). Remove redundant predictors.
- Predictions are wildly off – Check that your new data has the same column names and types as the training data. Use
str()to compare.
Frequently Asked Questions
How do I get the equation of the regression line?
Use coef(model) to get the intercept and slope. The equation is y = intercept + slope * x. For example, if intercept is 2 and slope is 3, the line is y = 2 + 3x.
What does R-squared mean?
R-squared is the proportion of variance in the outcome explained by the predictors. A value of 0.8 means 80% of the variability in y is explained by the model. It ranges from 0 to 1, but a high value doesn’t guarantee the model is appropriate.
How do I add a regression line to a scatterplot?
After plotting, use abline(model) to add the fitted line. For a smoother curve, use lines(fitted(model) ~ x, data).
Can I do linear regression with multiple predictors?
Yes, just add them in the formula: lm(y ~ x1 + x2 + x3, data = data). The summary will show a coefficient for each predictor, and the F-test tests the overall model.
What if my data is not linear?
Try transformations like log(y) or sqrt(x), or add polynomial terms like lm(y ~ x + I(x^2)). Check the residual plots to see if the fit improves.
How do I export the regression results?
Use sink("output.txt") then summary(model) then sink() to save the output to a file. For a tidy table, consider the broom package’s tidy(model).
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.