59

Is there a difference between the functions fitted() and predict()? I've noticed that mixed models from lme4 work with fitted() but not predict().

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
N Brouwer
  • 4,778
  • 7
  • 30
  • 35
  • One difference that may affect a processing routine is that for vglm (but not lm), the result of 'predict' has 2 columns, one for the predicted mu and one for predicted sd. 'Fitted' for both vglm and lm returns only the predicted mu's. – InColorado Sep 19 '21 at 16:46

2 Answers2

85

Yes, there is. If there is a link function relating the linear predictor to the expected value of the response (such as log for Poisson regression or logit for logistic regression), predict returns the fitted values before the inverse of the link function is applied (to return the data to the same scale as the response variable), and fitted shows it after it is applied.

For example:

x = rnorm(10)
y = rpois(10, exp(x))
m = glm(y ~ x, family="poisson")

print(fitted(m))
#         1         2         3         4         5         6         7         8 
# 0.3668989 0.6083009 0.4677463 0.8685777 0.8047078 0.6116263 0.5688551 0.4909217 
#         9        10 
# 0.5583372 0.6540281 
print(predict(m))
#          1          2          3          4          5          6          7 
# -1.0026690 -0.4970857 -0.7598292 -0.1408982 -0.2172761 -0.4916338 -0.5641295 
#          8          9         10 
# -0.7114706 -0.5827923 -0.4246050 
print(all.equal(log(fitted(m)), predict(m)))
# [1] TRUE

This does mean that for models created by linear regression (lm), there is no difference between fitted and predict.

In practical terms, this means that if you want to compare the fit to the original data, you should use fitted.

David Robinson
  • 77,383
  • 16
  • 167
  • 187
  • 28
    good answer as far as it goes, but you can use `predict(m,type="response")` to get predictions on the original (response) scale, and as @GregSnow points out below, `predict` has additional options (depending on the case). The development version of `lme4`, on r-forge, *does* have a `predict()` method. – Ben Bolker Aug 30 '12 at 17:33
  • @BenBolker I believe you meant `Predict(m, type="response")` from the `car` package. The original post is about `predict` from the `stats` package. – Rachel Zhang Feb 17 '23 at 19:04
  • ?? why do you think I meant `Predict(m, type = "response")` from `car` ? (A quick glance at `car::Predict` suggests it isn't relevant here ...) – Ben Bolker Feb 17 '23 at 19:21
35

The fitted function returns the y-hat values associated with the data used to fit the model. The predict function returns predictions for a new set of predictor variables. If you don't specify a new set of predictor variables then it will use the original data by default giving the same results as fitted for some models, but if you want to predict for a new set of values then you need predict. The predict function often also has options for which type of prediction to return, the linear predictor, the prediction transformed to the response scale, the most likely category, the contribution of each term in the model, etc.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110