Extracting
Model Outputs in R
Learn how to pull summary statistics, coefficients, and fitted values from linear models using R's built-in extraction functions.
What Is a Model Object?
When you run a regression in R, the result is stored as a model object: a special list that contains everything about the fit: coefficients, residuals, fitted values, and much more.
Why Extract Model Outputs?
Simply printing a model object only shows a brief summary. To perform deeper analysis, create plots, or report results, you need to programmatically extract specific pieces.
The Big Three Extraction Functions
R provides three essential functions to pull information from a model object. Master these and you can handle most analysis tasks.
Inside summary()
Extracting Coefficients with coef()
The coef() function returns a clean named vector of the estimated parameters. For the model \( \hat{y} = \hat{\beta}_0 + \hat{\beta}_1 x \), it gives you the intercept and slope directly.
Fitted Values with fitted()
The fitted() function returns \( \hat{y}_i = \hat{\beta}_0 + \hat{\beta}_1 x_i \) for every observation. These are the points on the regression line corresponding to your data.
summary() vs coef() vs fitted()
| summary() | coef() | fitted() | |
|---|---|---|---|
| Returns | Full report object | Named numeric vector | Numeric vector |
| Contains | \( R^2 \), SE, p-values, \( F \) | \( \hat{\beta}_0, \hat{\beta}_1, \ldots \) | \( \hat{y}_1, \hat{y}_2, \ldots, \hat{y}_n \) |
| Length | Complex list | \( p + 1 \) elements | \( n \) elements |
| Use Case | Full analysis report | Build equations | Plot/compare fits |
Drilling Into summary() with $
The summary() result is itself a list. Use the $ operator to access sub-components like the coefficient matrix, \( R^2 \), or residual standard error.
Bonus Helpers: resid() and confint()
How to Apply It: Step by Step
Example 1: Basic Extraction
Suppose we model the relationship between study hours and test score for 25 students.
Example 2: Extracting \( R^2 \) and p-values
Example 3: Fitted Values and Residuals
Common Mistakes to Avoid
Quick Reference Table
| I Want... | R Code | Returns |
|---|---|---|
| Full report | summary(fit) | List with all stats |
| Coefficients | coef(fit) | Named numeric vector |
| Fitted values | fitted(fit) | Numeric vector of \( \hat{y}_i \) |
| Residuals | resid(fit) | Numeric vector of \( e_i \) |
| \( R^2 \) value | summary(fit)$r.squared | Single number |
| Confidence intervals | confint(fit) | Matrix with bounds |
| p-value for slope | s$coefficients[2, 4] | Single number |
Question 1 of 5
After running fit <- lm(y ~ x, data = df), which function returns only the estimated coefficients as a named numeric vector?
Question 2 of 5
If s <- summary(fit), which expression gives you the p-value for the slope coefficient?
Question 3 of 5
What is always true about the relationship between fitted(fit), resid(fit), and the original response variable \( y \)?
Question 4 of 5
You run coef(fit) and get the output:
(Intercept) x
10.2 3.7
What is the predicted value \( \hat{y} \) when \( x = 5 \)?
Question 5 of 5
Which of the following correctly extracts the \( R^2 \) value from a linear model fit?