[R] predict.lm(...,type="terms") question

David Winsemius dwinsemius at comcast.net
Sat Sep 1 07:40:33 CEST 2012


On Aug 31, 2012, at 3:48 PM, jjthaden wrote:

> On Aug 30, 2012, at 4:35 PM,  David Windemius wrote:
>
>>> David said my newdata data frame 'new' must have a column named  
>>> 'area'.
>>> It did. Nonetheless predict.lm throws an error with type = "terms"  
>>> and
>>> newdata = new. I see nothing in the predict.lm  documentation that
>>> bars this usage. Is there a bug?
>>
>> After correcting the error in your definition of the 'area' vector I
>> get no error from predict.lm().
>
>> David.
>
> What error did you correct?

This was the code you offered:

#Ludbrook's data set S1 (except renaming
#his 'x' as 'concn' and his 'y' as 'area')
S1 <- data.frame(
     area = c(2.4,2.6,6.0,6.5,8.9,),
     concn = c(1.1,4,5,8.5,8.5))

There's an extra comma in the "area" vector.

>
> The newdata data frames in my examples have been pretty simple, and  
> have
> defined the 'area' vector simply, for instance,
> new <- data.frame(area = c(8172, 10220, 11570, 24150))
> new
> #        area
> #    1  8172
> #    2 10220
> #    3 11570
> #    4 24150
>
> Is something wrong with this?

I don't really know  ....  this appears to be a different problem than  
you posed earlier. If you would learn to post with context, we might  
not be in the position of trying to read your mind.

>  Were you able to make predict.lm work with newdata = new and type =  
> "terms"?

Yes. I have no trouble doing so.

 >  x <- rnorm(15); x2=rnorm(15)
 >  y <- x + x2 +rnorm(15)
 >  fit <- lm(y ~ x+x2)
 >  new <- data.frame(x2 = seq(-3.5, 3.5, 0.5)  )
 >  predict(fit, newdata=new,type="terms", terms="x2")
            x2
1  -4.9230035
2  -4.1850588
3  -3.4471141
4  -2.7091694
5  -1.9712248
6  -1.2332801
7  -0.4953354
8   0.2426093
9   0.9805539
10  1.7184986
11  2.4564433
12  3.1943880
13  3.9323326
14  4.6702773
15  5.4082220
attr(,"constant")
[1] 0.4501911


>
> -John
>

> Sent from the (NOT) R help mailing list  (and NOT) archive at  
> Nabble.com.

Hmmm ... Nabble ... definitely part of the problem.

-- 

David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list