Re: [R] Predict

From: Christoph Buser <>
Date: Wed 20 Jul 2005 - 02:35:37 EST

Dear Matthias

Can you provide an example to demonstrate what you did? Two remarks to your email. Maybe that answers already your question.

  1. Using predict() you will get the estimated value for each observation or for new data. You can reproduce this value by using the coefficients from your estimated model (see the example below). For the interval you can get a confidence interval for the expected value under fixed conditions of the explanatory variables or you can obtain a prediction interval for a single new observation. The latter is of course wider, since you try to catch a single observation and not the expected value.
  2. Using confint() you will get the estimated parameters (which are random variables, too) and their confidence interval. You can use the estimated values to calculate the predicted values.

But you can NOT use the upper values from confint to estimate the upper values from predict by just putting them into your regression model. Thats not the way how confidence intervals are constructed.
(I am not sure if this was your intention. Maybe if you show a reproducible example you can correct me if you meant something different)

## R Code
## Creation of a dataframe
x1 <- runif(40)
f1 <- rep(c("a", "b", "c","d"), each = 10) y <- 2*x1 + rep(c(0.5, 0.1, -0.6, 1.5), each = 10) + rnorm(40, 0, 2) dat <- data.frame(y = y, f1 = f1, x1 = x1) ## regression model
reg <- lm(y~ x1 + f1, data = dat)

predict(reg, type=c("response"), interval = "confidence")

## caluclation of predicted values using the estimated ## coefficients

## estimated coefficients
co <- summary(reg)$coefficients[,"Estimate"] ## Using the regression model with that coefficients ## for observation 11
co["(Intercept)"] + dat[11,"x1"]*co["x1"] + co["f1b"] ## prediction of observation 11
predict(reg, type=c("response"))[11]



Christoph Buser <> Seminar fuer Statistik, LEO C13
ETH (Federal Inst. Technology)	8092 Zurich	 SWITZERLAND
phone: x-41-44-632-4673		fax: 632-1228

Matthias Eggenberger writes:
> When I callculate a linear model, then I can compute via confint the
> confidencial intervals. the interval level can be chosen. as result, I get
> the parameter of the model according to the interval level.
> On the other hand, I can compute the prediction-values for my model as well
> with predict(object, type=c("response") etc.). Here I have also the
> possibility to chose a level for the confidential intervals. the output are
> the calculatet values for the fit, the lower and upper level.
> the problem now is, that when I calculate the values through the linear
> model function with the parameter values I get from confint() an I compare
> them with the values I get from predict() these values differ extremely. Why
> is that so? Does the command predict() calculate the values through an other
> routine? That means the command predict() doesn't use the same parameters to
> calculate the prediction-values than the ones given by confint()?
> Greetings Matthias
> --
> GMX DSL = Maximale Leistung zum minimalen Preis!
> ______________________________________________
> mailing list
> PLEASE do read the posting guide! mailing list PLEASE do read the posting guide! Received on Wed Jul 20 02:42:36 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:48 EST