Hi Tal,

That is correct. In the code, I actually call the same exact data.frame that was used to create the object. I understand I can call predict() without a data.frame to get this result, but I would like to predict other datasets as well.



Thanks,

Harold

On Dec 5, 2010, at 7:18 AM, Tal Galili wrote:

> Hi Harold,
> I think the error stems from the data.frame you are entering into the predict function.
> Your data.frame must have the EXACT column name as the one used for the creation of the model object.
> Is that the case in your code?
> 
> 
> Best,
> Tal
> 
> ----------------Contact Details:-------------------------------------------------------
> Contact me: Tal.Galili@gmail.com |  972-52-7275845
> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English)
> ----------------------------------------------------------------------------------------------
> 
> 
> 
> 
> On Sat, Dec 4, 2010 at 9:30 AM, Harold Pimentel <haroldpimentel@gmail.com> wrote:
> Hi all,
> 
> I recently wrote some code to store a number of polynomial models in a list and return this list. The model is returned fine, but when I make a subsequent call to predict(), I have an error. The code for polyModel selection is listed at the end of the email. Here is an example of the error:
> 
> > poly.fit <- polyModelSelection(x,y,10,F)
> > for (d in 1:4) {
> +     lm.model <- poly.fit$models[[d]]
> +     curve(predict(lm.model,data.frame(x)),add=T,lty=d+1)
> + }
> Error: variable 'poly(x, d, raw = T)' was fitted with type "nmatrix.1" but type "nmatrix.10" was supplied
> 
> Does anyone have any ideas?
> 
> 
> 
> Thanks,
> 
> Harold
> 
> 
> ############################################################################
> 
> polyModelSelection <- function(x,y,maxd,add.dim=F) {
>    dim.mult <- 0
>    if (add.dim) {
>        dim.mult = 1
>    }
>    bestD <- 1
>    bestError <- 1
>    loss <- 1
>    lm.models <- vector("list",maxd)
>    for (d in 1:maxd) {
>        lm.mod <- lm(y ~ 0 + poly(x,d,raw=T))
>        lm.models[[d]] <- lm.mod
>        loss[d] <- modelError(lm.mod,data.frame(x),y)+d*dim.mult
>        if (d == 1){
>            bestError <- loss[d]
>        }
>        if (loss[d] < bestError) {
>            bestError <- loss[d]
>            bestD <- d
>        }
>        # print(loss[d])
>    }
>    list(loss=loss,models=lm.models,best=c(bestD))
> }
> 
> modelError <- function(model,x,y) {
>    sum((y-predict(model,x))^2)
> }
> ______________________________________________
> 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]]

