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
Either use the predict function to create the new predictions, or use the raw argument to poly. ________________________________ From: r-help-bounces@r-project.org on behalf of Jarek Jasiewicz Sent: Sat 1/12/2008 9:50 AM To: R-help@r-project.org Subject: [R] glm expand model to more values 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 ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]
How many parameters are you trying to estimate? How many observations do you have? What is wrong is that half of your parameter estimates are statistically meaningless: dd <- data.frame(a=c(1, 2, 3, 4, 5, 6), b=c(3, 5, 6, 7, 9, 10)) overparameterized.model <- glm(b~poly(a,3),data=dd) summary(overparameterized.model) Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 6.6667 0.1725 38.644 0.000669 *** poly(a, 3)1 5.7371 0.4226 13.576 0.005382 ** poly(a, 3)2 -0.1091 0.4226 -0.258 0.820395 poly(a, 3)3 0.2236 0.4226 0.529 0.649562 Charles Annis, P.E. Charles.Annis at StatisticalEngineering.com phone: 561-352-9699 eFax: 614-455-3265 http://www.StatisticalEngineering.com -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Jarek Jasiewicz Sent: Saturday, January 12, 2008 11:50 AM To: R-help at r-project.org Subject: [R] glm expand model to more values 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 ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.