[R] non-linear regression for 3D data

Duncan Murdoch murdoch.duncan at gmail.com
Thu Aug 12 18:39:51 CEST 2010


On 12/08/2010 10:35 AM, szisziszilvi wrote:
> I've tried lm, but something is wrong.
> I've made a test dataset of 599 data points, my original equation is
>
> zz = 1 +0.5*xx -3.2*xx*xx -1*yy +4.2*yy*yy
>
> but the R gives this result:
> -------------------------------------------------------
> > mp <- read.csv(file="sample.csv",sep=";",header=TRUE)
> > lm(zz ~ poly(xx,2) + poly(yy,2), data=mp)
>
> Call:
> lm(formula = zz ~ poly(xx, 2) + poly(yy, 2), data = mp)
>
> Coefficients:
>  (Intercept)  poly(xx, 2)1  poly(xx, 2)2  poly(yy, 2)1  poly(yy, 2)2  
>        25.86      -2239.86       -595.01       2875.54        776.84
> -----------------------------------------------------------
> which is definitely not the original. :(


I don't think you are interpreting the coefficients properly.  The basis 
functions are orthogonal polynomials, not xx and xx^2, so the 
coefficients won't match the ones you used in your definition.  You 
should compare the predictions of the model, e.g. by looking at

 range(predict(lm(zz ~ poly(xx,2) + poly(yy,2), data=mp)) - zz)

If you insist on the power basis, just fit the model as

 lm(zz ~ xx + I(xx^2) + yy + I(yy^2), data=mp)

but you might get less accurate predictions due to increased collinearity.

Duncan Murdoch



More information about the R-help mailing list