Barry: I reproduced your error on Windows.
However, as you know, in your code below, the first two components of your list are NULL. This is a bit clumsy, so I modified your lmn function by changing it from ... for(i in n) ... to ... for( i in 1:n) ... With that change, there are no errors. Other than that, no clue, but maybe it helps. -- Bert Bert Gunter Genentech Nonclinical Statistics -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Barry Rowlingson Sent: Monday, January 18, 2010 3:31 PM To: r-help@r-project.org Subject: [R] Predict polynomial problem I have a function that fits polynomial models for the orders in n: lmn <- function(d,n){ models=list() for(i in n){ models[[i]]=lm(y~poly(x,i),data=d) } return(models) } My data is: > d=data.frame(x=1:10,y=runif(10)) So first just do it for a cubic: > mmn = lmn(d,3) > predict(mmn[[3]]) 1 2 3 4 5 6 7 8 0.6228353 0.5752811 0.5319524 0.4957381 0.4695269 0.4562077 0.4586691 0.4798001 9 10 0.5224893 0.5896255 and lets extrapolate a bit: > predict(mmn[[3]],newdata=data.frame(x=c(9,10,11))) 1 2 3 0.5224893 0.5896255 0.6840976 now let's to it for cubic to quintic: > mmn = lmn(d,3:5) check the cubic: > predict(mmn[[3]]) 1 2 3 4 5 6 7 8 0.6228353 0.5752811 0.5319524 0.4957381 0.4695269 0.4562077 0.4586691 0.4798001 9 10 0.5224893 0.5896255 - thats the same as last time. Extrapolate? > predict(mmn[[3]],newdata=data.frame(x=c(9,10,11))) Error: variable 'poly(x, i)' was fitted with type "nmatrix.3" but type "nmatrix.5" was supplied In addition: Warning message: In Z/rep(sqrt(norm2[-1L]), each = length(x)) : longer object length is not a multiple of shorter object length it falls over. I can't see the difference between the objects, summary() looks the same. Is something wrapped up in an environment somewhere, or some lazy evaluation thing, or have I just done something stupid? Here's a complete example you can paste in - R --vanilla < this.R gives the error above - R 2.10.1 on Ubuntu, and also on R 2.8.1 I had lying around on a Windows box: d = data.frame(x=1:10,y=runif(10)) lmn <- function(d,n){ models=list() for(i in n){ models[[i]]=lm(y~poly(x,i),data=d) } return(models) } mmn = lmn(d,3) predict(mmn[[3]]) predict(mmn[[3]],newdata=data.frame(x=c(9,10,11))) mmn2 = lmn(d,3:5) predict(mmn2[[3]]) predict(mmn2[[3]],newdata=data.frame(x=c(9,10,11))) Barry ______________________________________________ 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. ______________________________________________ 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.