# This works in any R installation without any packages
data(mtcars)
# Run regression
model <- lm(mpg ~ wt + hp, data = mtcars)
# Generate predictions and make a new column for predictions in
dataset
mtcars$predicted <- predict(model)
# Write the predictions and data to csv file
[Link](mtcars, "mtcars_with_predictions.csv", [Link] = FALSE)
# Basic output
summary(model)
Residuals:
Min 1Q Median 3Q Max
-3.941 -1.600 -0.182 1.050 5.854
Residuals are the differences between the actual observed values of
mpg and the values predicted by our model
Min/Max: The smallest and largest residuals.
1Q & 3Q (Quartiles): The 1st and 3rd quartiles. This gives you an idea
of the distribution.
What to look for: You want the median (the middle value) to be close
to zero, and the 1Q and 3Q to have roughly similar magnitudes. This
indicates a symmetric distribution of errors. In our case, the median
is -0.182, which is good.
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 37.22727 1.59879 23.285 < 2e-16 ***
wt -3.87783 0.63273 -6.129 1.12e-06 ***
hp -0.03177 0.00903 -3.519 0.00145 **
Std. Error: The standard error tells you how much the estimated
coefficient would bounce around if you repeated the study many
times. A smaller standard error relative to the estimate generally
indicates a more precise estimate. For wt, this estimate could
reasonably be off by about ±0.63 in either direction.
t value: The test statistic for the hypothesis test that the coefficient is
different from zero (i.e., it has a significant effect). It's calculated as t
= Estimate / Std. Error.
Pr(>|t|): The p-value associated with the t-statistic.
Null Hypothesis (H₀): The coefficient is zero (the predictor has no
effect).
A small p-value (typically < 0.05) provides evidence against the null
hypothesis. We conclude that the predictor is statistically significant.
In our output, both wt and hp have very small p-values (***),
meaning they are significant predictors of mpg.
Residual Standard Error (RSE): Think of it as the average prediction
error. Lower is better. Here, the average error is about 2.6 mpg.
Degrees of Freedom (29): Calculated as n-p-1 (number of
observations - number of predictors - 1). Here: 32 cars - 2 predictors -
1 = 29.
Multiple R-squared (R²): The proportion of variance in the dependent
variable (mpg) that can be explained by the independent variables
(wt and hp). It ranges from 0 to 1.
Adjusted R-squared: Adjusts the R² for the number of predictors in
the model. This is important because adding more predictors will
always increase the R², even if they are useless. The adjusted R²
penalizes this. It's a more reliable measure for multiple regression.
Our adjusted R² is 81.48%.
F-statistic: Tests the overall significance of the model. The null
hypothesis is that all of the regression coefficients are equal to zero
(i.e., the model has no predictive power).
p-value: 9.109e-12: This is extremely small. We reject the null
hypothesis and conclude that our model (with wt and hp) is
significantly better at predicting mpg than a model with no
predictors.
Metric Formula Purpose
R-square SSres Variance explained
1−
SS tot
Adj R-square ( 1−R 2 ) ( n−1 ) Penalized variance
1−
n−p−1 explained
F-stat MS reg Overall model
MS res significance
t-stat bj Individual coefficient
SE ( b j ) significance
RSE √ MS res Average prediction
error
SSres =∑ ( y i−^y i )
2
SStot =∑ ( y i− y )
2
yi= Actual values, ^y = Predicted values, and y is mean of
i
y (actual value)
n = number of observations
p = number of predictor variables
n−p−1 = degrees of freedom for residuals
SS reg
MS reg =
p
SSreg =SStot −SS res
MS res =SS res /(n− p−1)
bj = coefficient estimate
SE ( b j ) = standard error of the coefficient
SS_residual <- deviance(model)
SS_total <- sum((mtcars$mpg - mean(mtcars$mpg))^2)
R-sq <- 1-(SS_residual/SS_total)
n <- nobs(model)
p <- length(coef(model)) - 1