01 / 20
> summary(fit) Coefficients: 2.50 0.87

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.

R Programming

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.

# Fit a simple linear model fit <- lm(y ~ x, data = my_data)

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.

Reporting
Extract \( \hat{\beta} \) values and p-values for tables and papers.
Diagnostics
Pull residuals and fitted values to check model assumptions.
Prediction
Use coefficients to build prediction equations and forecasts.

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.

1
summary(): returns a comprehensive report including \( R^2 \), \( F \)-statistic, and coefficient table with standard errors and p-values.
2
coef(): extracts just the estimated coefficients \( \hat{\beta}_0, \hat{\beta}_1, \ldots \) as a named numeric vector.
3
fitted(): returns the predicted values \( \hat{y}_i \) for each observation in the original data.

Inside summary()

summary(fit) Residuals: Min: -2.31 1Q: -0.58 Median: 0.12 3Q: 0.71 Max: 1.96 Coefficients: Estimate Std.Error t value Pr(>|t|) (Intercept) 2.500 0.312 8.01 1.2e-08 *** x 0.870 0.045 19.33 < 2e-16 *** Model Fit: R-squared: 0.9412 Adjusted R-squared: 0.9387 F: 373.6 on 1 and 23 DF residuals coefficients goodness

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.

coef(fit) # (Intercept) x # 2.5000000 0.8700000

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.

x y Observed Fitted Residual

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.

s <- summary(fit) # Coefficient matrix (with SE, t, p) s$coefficients # R-squared value s$r.squared # [1] 0.9412 # Residual standard error s$sigma # [1] 0.8327

Bonus Helpers: resid() and confint()

resid(fit)
Returns the residuals \( e_i = y_i - \hat{y}_i \) for each observation. Essential for diagnostic plots.
confint(fit)
Returns 95% confidence intervals for each \( \hat{\beta} \). Change level with level = 0.99.
# Residuals resid(fit) # [1] -0.23 0.41 -0.58 0.12 ... # 95% Confidence intervals confint(fit) # 2.5 % 97.5 % # (Intercept) 1.858 3.142 # x 0.778 0.962

How to Apply It: Step by Step

1
Fit your model. Use lm(y ~ x, data = df) and store the result in a variable.
2
Check the summary. Run summary(fit) to see the full report: \( R^2 \), coefficients, and p-values.
3
Extract what you need. Use coef(fit) for coefficients, fitted(fit) for predictions, resid(fit) for residuals.
4
Drill deeper with $. Store summary(fit) in a variable, then access $r.squared, $coefficients, or $sigma.
5
Use outputs downstream. Plot residuals, build prediction equations, or create formatted tables for reports.

Example 1: Basic Extraction

Suppose we model the relationship between study hours and test score for 25 students.

# Fit the model fit <- lm(score ~ hours, data = students) # Get coefficients coef(fit) # (Intercept) hours # 52.30 4.85

Example 2: Extracting \( R^2 \) and p-values

s <- summary(fit) # R-squared s$r.squared # [1] 0.8731 # p-value for slope (hours) s$coefficients[2, 4] # [1] 3.41e-10 # Standard error for slope s$coefficients[2, 2] # [1] 0.382

Example 3: Fitted Values and Residuals

# Get fitted values head(fitted(fit)) # 1 2 3 4 5 6 # 57.15 62.00 66.85 71.70 76.55 81.40 # Get residuals head(resid(fit)) # 1 2 3 4 5 6 # 2.85 -1.00 3.15 -0.70 1.45 -2.40 # Verify: observed = fitted + residual head(fitted(fit) + resid(fit)) # [1] 60 61 70 71 78 79

Common Mistakes to Avoid

✕ Confusing summary() with coef()
Using summary(fit)$coefficients returns a matrix, not a vector. Use coef(fit) for a clean numeric vector of estimates.
✕ Forgetting to store summary()
Running summary(fit)$r.squared works but is slow. Store it: s <- summary(fit), then use s$r.squared repeatedly.
✕ Wrong matrix index
The coefficient matrix is [row, col]. Row 1 is the intercept, not the slope. Slope is always row 2 (or later for multiple regression).

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?