[R] glm expand model to more values

Jarek Jasiewicz jarekj at amu.edu.pl
Sat Jan 12 17:50:09 CET 2008


Hi

I have the problem with fitting curve to data with lm and glm. When I 
use polynominal dependiency, fitted values from model are OK, but I 
cannot  recive proper values when I use coefficents to caltulate this.  
Let me present simple example:

I have simple data.frame: (dd)
 a: 1 2 3 4 5 6
 b:  3  5  6  7  9 10

I try to fit it to model:

model=glm(b~poly(a,3),data=dd)
 I have following data fitted to model (as I expected)
 > fitted(model)
        1         2         3         4         5         6
 3.095238  4.738095  6.095238  7.333333  8.619048 10.119048

and coef(model)
(Intercept) poly(a, 3)1 poly(a, 3)2 poly(a, 3)3
  6.6666667   5.7370973  -0.1091089   0.2236068

so when I try to expand the model to other data (simple extrapolation), 
let say: s=seq(1:10,by=1)

I do:
extra=sapply(s,function(x) coef(model) %*% x^(0:3))
and here is result:
[1]  12.51826  19.49328  28.93336  42.18015  60.57528  85.46040 118.17714
 [8] 160.06715 212.47207 276.73354

the data form expanding coefs are completly differnd from fitted

What's going wrong?

Jarek




More information about the R-help mailing list