Hey all, I'm performing polynomial regression. I'm simulating x values using runif() and y values using a deterministic function of x and rnorm(). When I perform polynomial regression like this: fit_poly <- lm(y ~ poly(x,11,raw = TRUE)) I get some NA coefficients. I think this is due to the high correlation between say x and x^2 if x is distributed uniformly on the unit interval (as is the case in my example). However, I'm still able to plot a polynomial fit like this: points(x, predict(fit_poly), type="l", col="green", lwd=2) What I'm interested in finding out is, how R handles the NA values I get for some coefficients (and how that affects the polynomial I see plotted). Thanks!
Inline. -- Bert On Sat, Apr 27, 2013 at 8:48 AM, Lucas Holland <hollandlucas at gmail.com> wrote:> Hey all, > > I'm performing polynomial regression. I'm simulating x values using runif() and y values using a deterministic function of x and rnorm(). > > When I perform polynomial regression like this: > > fit_poly <- lm(y ~ poly(x,11,raw = TRUE)) > > I get some NA coefficients. I think this is due to the high correlation between say x and x^2 if x is distributed uniformly on the unit interval (as is the case in my example). However, I'm still able to plot a polynomial fit like this: > > points(x, predict(fit_poly), type="l", col="green", lwd=2) > > What I'm interested in finding out is, how R handles the NA values I get for some coefficients (and how that affects the polynomial I see plotted).It ignores them, i.e. treats them as 0. You are overfitting. See the singular.ok argument. Incidentally, using high order polynomials as data smoothers is nowadays usually frowned on. Consider using splines or other effectively local smoothers instead. R has many alternatives. -- Bert> > Thanks! > > ______________________________________________ > R-help at 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
Hi Lucas, You may find some of these examples useful (towards the end): http://elkhartgroup.com/rmodels.php For example in your case you could be using b splines instead of an 11th order polynomial, or use thin plate regression splines from the mgcv package. I will also humbly suggest that ggplot2 overlaying observed values with predicted lines is a more elegant way to visualize the data and the results. Cheers, Josh On Sat, Apr 27, 2013 at 8:48 AM, Lucas Holland <hollandlucas@gmail.com>wrote:> Hey all, > > I'm performing polynomial regression. I'm simulating x values using > runif() and y values using a deterministic function of x and rnorm(). > > When I perform polynomial regression like this: > > fit_poly <- lm(y ~ poly(x,11,raw = TRUE)) > > I get some NA coefficients. I think this is due to the high correlation > between say x and x^2 if x is distributed uniformly on the unit interval > (as is the case in my example). However, I'm still able to plot a > polynomial fit like this: > > points(x, predict(fit_poly), type="l", col="green", lwd=2) > > What I'm interested in finding out is, how R handles the NA values I get > for some coefficients (and how that affects the polynomial I see plotted). > > Thanks! > > ______________________________________________ > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://joshuawiley.com/ Senior Analyst - Elkhart Group Ltd. http://elkhartgroup.com [[alternative HTML version deleted]]
Reasonably Related Threads
- Coefficients of a factorized polynomial
- unequally spaced factor levels orthogonal polynomial contrasts coefficients trend analysis
- ordinary polynomial coefficients from orthogonal polynomials?
- coefficients of each local polynomial from locfit
- Coefficients in a polynomial glm with family poisson/binomial