Hi everybody! I'm looking some way to do in R a polynomial fit, say like polyfit function of Octave/MATLAB.
For who don't know, c = polyfit(x,y,m) finds the coefficients of a polynomial p(x) of degree m that fits the data, p(x[i]) to y[i], in a least squares sense. The result c is a vector of length m+1 containing the polynomial coefficients in descending powers: p(x) = c[1]*x^n + c[2]*x^(n-1) + ... + c[n]*x + c[n+1] For prediction, one can then use function polyval like the following: y0 = polyval( polyfit( x, y, degree ), x0 ) y0 are the prediction values at points x0 using the given polynomial. In R, we know there is lm for 1-degree polynomial: lm( y ~ x ) == polyfit( x, y, 1 ) and for prediction I can just create a function like: lsqfit <- function( model, xx ) return( xx * coefficients(model)[2] + coefficients(model)[1] ); and then: y0 <- lsqfit(x0) (I've tried with predict.lm( model, newdata=x0 ) but obtain a bad result) For a degree greater than 1, say m, what can I use.?? I've tried with lm( y ~ poly(x, degree=m) ) I've also looked at glm, nlm, approx, ... but with these I can't specify the polynomial degree. Thank you so much! Sincerely, -- Marco ______________________________________________ R-help@stat.math.ethz.ch 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.