# [R] predict.lm

Bill Szkotnicki bszk at uoguelph.ca
Tue May 2 22:14:54 CEST 2006

```I did mean to use x1,x2,x3,x4 in the new data frame.

And I think the theory would be something like

yhat = 1' K' bhat
and so the variance should be  1' K'CK 1  where C=(X'X)-1
and 1 is a 1 vector.

The question is do I need to form these matrices and grind through it or is
there an easier way?

Bill

-----Original Message-----
From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk]
Sent: Tuesday, May 02, 2006 2:54 PM
To: Christos Hatzis
Cc: 'Bill Szkotnicki'; 'R-Help help'
Subject: Re: [R] predict.lm

On Tue, 2 May 2006, Christos Hatzis wrote:

> I think you got it right.
>
> The mean of the (weighted) sum of a set of random variables is the
> (weighted) sum of the means and its variance is the (weighted) sum of the
> individual variances (using squared weights).  Here you don't have to
worry
>
> So what you proposed does exactly this.

Yes, but the theory has assumptions which are not met here: the random
variables are correlated (in almost all case).

> -Christos
>
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Bill Szkotnicki
> Sent: Tuesday, May 02, 2006 2:59 PM
> To: 'R-Help help'
> Subject: [R] predict.lm
>
> I have a model with a few correlated explanatory variables.
> i.e.
>> m1=lm(y~x1+x2+x3+x4,protdata)
> and I have used predict as follows:
>
>> x=data.frame(x=1:36)
>> yp=predict(m1,x,se.fit=T)

How can this work?  You fitted the model to x1...x4 and supplied x.

>> tprot=sum(yp\$fit) # add up the predictions tprot
>
> tprot is the sum of the 36 predicted values and I would like the se of
that
> prediction.
> I think
>> sqrt(sum(yp\$se.fit^2))
> is not correct.
>
> Would anyone know the correct approach?
> i.e. How to get the se of a function of predicted values (in this case
sum)

You need to go back to the theory: it is easy to do for a linear function,
otherwise you will need to linearize.

--
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

```