Introduction
Linear regression models the relationship between a dependent variable and one or more independent variables.
Simple Linear Regression
# Fit model
model <- lm(y ~ x, data = df)
# Model summary
summary(model)
# Coefficients
coef(model)
# Predictions
predict(model, newdata = data.frame(x = 10))
# Confidence interval
predict(model, newdata = data.frame(x = 10),
interval = "confidence")
Multiple Linear Regression
# Multiple predictors
model <- lm(y ~ x1 + x2 + x3, data = df)
# Model summary
summary(model)
# All coefficients
coefficients(model)
# R-squared
summary(model)$r.squared
adj.r.squared
Model Diagnostics
# Residuals
residuals(model)
# Fitted values
fitted(model)
# Diagnostic plots
plot(model)
# Multiple plots
par(mfrow = c(2, 2))
plot(model)
Summary
Linear regression is fundamental for modeling relationships. Check assumptions for valid results.