Thanks a ton Rolf, I was using the coefficients as given from summary(fit), which have been rounded. When I used coef(fit) as you've done, the poly function is the same now in Octave and returned the R predicted values as expected. Your time is appreciated,
C > CC: r-help@r-project.org > From: r.tur...@auckland.ac.nz > Subject: Re: [R] Polynomial Fitting > Date: Wed, 30 Sep 2009 08:30:15 +1300 > To: w_chris_carle...@hotmail.com > > > On 30/09/2009, at 5:34 AM, chris carleton wrote: > > > > > Thanks for the response. I'm sorry I didn't provide the code or > > data example earlier. I was using the polynomial fitting technique > > of this form; > > > > test <- lm(x[,34] ~ I(x[,1]) + I(x[,1]^2) + I(x[,1]^3)) > > > > for the original fitting operation. I also tried to use; > > > > lm(y ~ poly(x,3,raw=TRUE)) > > > > with the same results for the polynomial coefficients in both > > cases. If my understanding is correct, both of the methods above > > produce the coefficients of a polynomial based on the data in 'y' > > as that data varies over 'x'. Therefore, I would assume that the > > function of the polynomial should always produce the same results > > as the predict() function in R produces. However, here are the raw > > data for anyone that has the time to help me out. > > > > y: > > [1] 9097 9074 9043 8978 8912 8847 8814 8786 8752 8722 8686 8657 > > 8610 8604 8554 > > [16] 8546 8496 8482 8479 8462 8460 8438 8428 8418 8384 > > > > x: > > [1] 17.50 NA 20.59 21.43 17.78 21.89 NA 22.86 NA 6.10 > > NA 5.37 > > [13] 3.80 NA 6.80 NA NA NA 5.80 NA NA NA > > NA NA > > [25] NA > > > > I think that R lm() just ignores the NA values, but I've also tried > > this by first eliminating NAs and the corresponding x values from > > the data before fitting the poly and the result was the same > > coefficients. Thanks very much to anyone who is willing to provide > > information. > > What's the problem? > > fit <- lm(y~x+I(x^2)+I(x^3)) > ccc <- coef(fit) > X <- cbind(1,x,x^2,x^3) > print(fitted(fit)) > chk <- X%*%ccc > print(chk[!is.na(chk)]) > print(range(fitted(fit)-chk[!is.na(chk)])) > [1] 0.000000e+00 5.456968e-12 > > The answers are the same. > > cheers, > > Rolf Turner > > ###################################################################### > Attention: > This e-mail message is privileged and confidential. If you are not the > intended recipient please delete the message and notify the sender. > Any views or opinions presented are solely those of the author. > > This e-mail has been scanned and cleared by MailMarshal > www.marshalsoftware.com > ###################################################################### _________________________________________________________________ [[alternative HTML version deleted]] ______________________________________________ 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.