Skip to main content
v2026.11,858 entries · CC-BY 4.0

fitted() in R: Extract Fitted Values from lm and glm

How fitted() in R extracts fitted values from lm and glm, how it differs from predict(), and how missing values and the link scale change the result.

Written and maintained by CASRAI Editorial Board

Last updated

fitted() in R returns the values a fitted model predicts for the observations it was built from. Give it the object returned by lm() or glm() and you get one fitted value per observation used in the fit. fitted.values() is an alias for the same function. The sections below show the call, how it differs from predict(), what happens to missing values, and which scale a glm reports on.

What fitted() is used for in research

Fitted values are the model’s own predictions for the data it was estimated on. Researchers extract them to plot observed against predicted outcomes, to compute residuals, and to check a regression’s assumptions (see assumptions of simple regression and how to read a residual plot). The R documentation describes fitted as a generic function that extracts fitted values from objects returned by modeling functions, and says every class of object returned by a model-fitting function should provide a fitted method.

In statistics notation the fitted value for each observation is usually written as y-hat. For the difference between observed and fitted values, see how to calculate residuals.

Install or access

fitted belongs to the stats package that ships with R, so no extra package is needed. R itself is distributed from the Comprehensive R Archive Network (CRAN); the binary installers for Linux, macOS and Windows are listed at cran.r-project.org/bin/. The help page for the function is at stat.ethz.ch/R-manual/R-devel/library/stats/html/fitted.values.html, and inside R you can open it with ?fitted.

Step-by-step: get fitted values from a model

The usage on the help page is fitted(object, ...) and fitted.values(object, ...). The steps below apply it to a linear model.

  1. Fit a model.
    fit = lm(y ~ x, data = mydata)

    Replace y, x and mydata with your own response, predictor and data frame.

  2. Extract the fitted values.
    fitted(fit)

    The result holds the fitted values extracted from the object you passed.

  3. Use the alias if you prefer it.
    fitted.values(fit)

    The help page states that fitted.values is an alias for fitted. Note that the generic is fitted, not fitted.values.

  4. Read them from the model object.
    fit$fitted.values

    The lm help page lists fitted.values among the components of the returned object (described as the fitted mean values) and names fitted.values among the generic accessor functions for lm results.

  5. Add them to your data frame. Only do this when no rows were dropped for missing values (see the troubleshooting section):
    mydata$fit = fitted(fit)
  6. Get the matching residuals.
    residuals(fit)

    The lm help page defines the residuals as the response minus the fitted values. resid is an alias for residuals.

Output and how to read it

Fitted values versus predict()

For a model fitted with lm, predict(fit) without newdata returns predictions based on the data used for the fit, which is the same thing fitted(fit) reports. The documentation for predict.lm states: “If newdata is omitted the predictions are based on the data used for the fit.” Choose predict() when you need more than the in-sample values. Its usage includes newdata, se.fit, interval (none, confidence or prediction), level (default 0.95) and type. With an interval requested, the result is a matrix with columns fit, lwr and upr. With se.fit = TRUE it is a list containing fit, se.fit, residual.scale and df.

The same help page separates the two interval types: a confidence interval is for the mean response, a prediction interval is for a single future observation.

Fitted values from a glm: link scale or response scale

For a generalized linear model the choice of scale matters. predict.glm has a type argument whose default is link, which gives predictions on the scale of the linear predictors; for a binomial model with the usual logit link that means log-odds. Setting type = 'response' returns values on the scale of the response variable, such as probabilities. type = 'terms' returns a matrix of fitted values for each term, on the linear-predictor scale. To avoid ambiguity in a report, state the scale explicitly:

predict(fit_glm, type = "response")

For background on this model family, see what is a generalized linear model.

Missing values

The fitted help page says that methods can use napredict to compensate for the omission of missing values, and that the default and nls methods do. The predict.lm page spells out the consequence when newdata is omitted: the na.action used in the fit controls missing values. With na.omit the omitted cases are absent from the result; with na.exclude they appear as NA. If you want fitted values and residuals that line up row for row with your original data, fit the model with na.action = na.exclude; the lm help page notes that na.exclude can be useful.

Troubleshooting

  • Assigning the fitted values to a column fails because the lengths differ. The model dropped incomplete cases (the lm default na.action is na.omit), so the fitted vector is shorter than the data frame. Refit with na.action = na.exclude or filter the data to complete cases first.
  • Values look like log-odds, not probabilities. You are on the link scale, the default for predict.glm. Request type = 'response'.
  • Predictions for new observations come back with the original length. predict() only uses new data when you pass newdata. Without it, predictions are based on the data used for the fit.

Free alternatives and equivalent calls in R

Within R there are several ways to obtain the same in-sample values or to go further. Each row below is taken from the R documentation pages listed in the sources.

Call What it returns Documented on
fitted(fit) Fitted values extracted from the model object fitted help page
fitted.values(fit) Alias for fitted fitted help page
fit$fitted.values The fitted mean values stored in an lm object lm help page
predict(fit) Predictions based on the data used for the fit when newdata is omitted predict.lm help page
predict(fit, newdata = d, interval = 'prediction') Matrix with fit, lwr, upr for new observations predict.lm help page
predict(fit_glm, type = 'response') Predictions on the scale of the response variable predict.glm help page
residuals(fit) Response minus fitted values for an lm lm help page

FAQ

What does fitted() do in R?

It extracts fitted values from the object returned by a model-fitting function such as lm or glm. fitted.values is an alias.

Is fitted() the same as predict()?

For an lm, predict(fit) without newdata is based on the data used for the fit, so it carries the same meaning as fitted(fit). predict() adds newdata, intervals and standard errors.

How do I add fitted values to a data frame in R?

Assign them to a new column, for example mydata$fit = fitted(fit), after checking that the model did not drop rows. If it did, refit with na.action = na.exclude or use only complete cases.

How do I get fitted values on the probability scale for a logistic regression?

Use predict(fit_glm, type = 'response'). The default type = 'link' returns log-odds for a binomial model.

What is the difference between fitted values and residuals?

The lm help page describes residuals as the response minus the fitted values. Fitted values are the model’s predictions; residuals are what the model leaves unexplained. For worked residual diagnostics see the Breusch-Pagan test in R.

Sources (official documentation)

Date checked: 2026-10-29. All links go to the official documentation.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about fitted() in R: Extract Fitted Values from lm and glm

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Ask CASRAI · Regulatory Radar

Research-admin question? Get an answer that links its sources.

An AI assistant specialized in research administration. Every answer links its sources to check before you act. 2 questions free, no account. $29/month after.

  • Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.
  • Every answer numbers its sources and links each one, so you can check the source yourself.