0
0
R-programmingHow-ToBeginner · 3 min read

How to Perform Linear Regression r in R: Simple Guide

To perform linear regression r in R, use the lm() function with a formula like response ~ predictor. Fit the model by calling lm(response ~ predictor, data = your_data) and then use summary() to see results.
📐

Syntax

The basic syntax for linear regression in R uses the lm() function:

  • lm(formula, data): Fits a linear model.
  • formula: Describes the relationship, e.g., y ~ x means y depends on x.
  • data: The data frame containing variables.

Use summary() on the model to get detailed results.

r
model <- lm(y ~ x, data = dataset)
summary(model)
💻

Example

This example shows how to fit a linear regression model predicting mpg (miles per gallon) from wt (weight) in the built-in mtcars dataset.

r
model <- lm(mpg ~ wt, data = mtcars)
summary(model)
Output
Call: lm(formula = mpg ~ wt, data = mtcars) Residuals: Min 1Q Median 3Q Max -4.5432 -2.3651 -0.1252 1.4103 6.8727 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 37.2851 1.8776 19.859 < 2e-16 *** wt -5.3445 0.5591 -9.559 1.29e-10 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 3.046 on 30 degrees of freedom Multiple R-squared: 0.7528, Adjusted R-squared: 0.7446 F-statistic: 91.38 on 1 and 30 DF, p-value: 1.294e-10
⚠️

Common Pitfalls

Common mistakes when performing linear regression in R include:

  • Not specifying the data argument, causing errors if variables are not in the global environment.
  • Using incorrect formula syntax, like missing the tilde ~.
  • Ignoring assumptions like linearity and normality of residuals.

Always check your model summary and diagnostics.

r
## Wrong: missing data argument
# model <- lm(mpg ~ wt)

## Correct:
model <- lm(mpg ~ wt, data = mtcars)
summary(model)
📊

Quick Reference

StepDescriptionExample
1Fit model with lm()model <- lm(y ~ x, data = df)
2View summarysummary(model)
3Check coefficientssummary(model)$coefficients
4Predict new valuespredict(model, newdata)
5Plot diagnosticsplot(model)

Key Takeaways

Use lm() with a formula and data to fit linear regression in R.
Always include the data argument to avoid variable not found errors.
Check model summary to understand coefficients and significance.
Watch out for formula syntax errors like missing ~.
Use diagnostic plots to validate model assumptions.