The following variables have the following significant relationships (x is the explanatory variable): linear, cubic, exponential, logistic. The linear relationship plots without any trouble. Cubic is the 'best' model, but it is not plotting as a smooth curve using the following code: cubic.lm<- lm(y~poly(x,3)) lines(x,predict(cubic.lm),lwd=2) How do I plot the data and the estimated curves for all of these regression models in the same plot? x <- c(62.5,68.5,0,52,0,52,0,52,23.5,86,0,0,0,0,0,0,0,0,0,0) y <- c(0.054,0.055,0.017,0.021,0.020,0.028,0.032,0.073,0.076,0.087,0.042,0.042,0.041,0.045,0.021,0.018,0.017,0.018,0.028,0.022) Thanks in advance. Rhonda Reidy [[alternative HTML version deleted]]
Rhonda Reidy wrote:> > The following variables have the following significant relationships (x is > the explanatory variable): linear, cubic, exponential, logistic. The > linear relationship plots without any trouble. > > Cubic is the 'best' model, but it is not plotting as a smooth curve using > the following code: > > cubic.lm<- lm(y~poly(x,3)) > lines(x,predict(cubic.lm),lwd=2) > > How do I plot the data and the estimated curves for all of these > regression models in the same plot? > > x <- c(62.5,68.5,0,52,0,52,0,52,23.5,86,0,0,0,0,0,0,0,0,0,0) > > y <- > c(0.054,0.055,0.017,0.021,0.020,0.028,0.032,0.073,0.076,0.087,0.042,0.042,0.041,0.045,0.021,0.018,0.017,0.018,0.028,0.022) >Hi Rhonda, The problem is that the default behavior of predict() only produces output corresponding to the original values of x used in the model. To get a "smooth" curve, you will want to predict at a large number of x values that fall within the range of the data. This can be done using the pretty() function, or using a composition of the seq() and range() functions if you desire more control: # Create a new vector of X values at which to generate predictions: predX <- pretty( x, n = 100 ) plot( y ~ x ) # Add the regression model, but generate predictions using the # points in predX-- the default is to predict using points in x. lines( predX, predict( cubic.lm, newdata = list( x = predX )), lwd = 2 ) The above should produce the output you want. However, I suggest Hadley Wickham's excellent ggplot2 package-- I feel the following is more expressive than using base graphics functions: require( ggplot2 ) Plot <- qplot( x, y ) + stat_smooth( method = 'lm', formula = 'y ~ poly(x,3)' ) print( Plot ) The amazing power of ggplot2 is that when you want to alter or extend your plot, you simply "add" more ggplot2 commands to your plot object: # Add a linear regression model and change the color theme used in the output Plot <- Plot + stat_smooth( method = 'lm', color = 'red' ) + theme_bw() print( Plot ) Hope this helps! -Charlie -- View this message in context: http://n4.nabble.com/Plot-different-regression-models-on-one-graph-tp1554606p1554622.html Sent from the R help mailing list archive at Nabble.com.
On Feb 13, 2010, at 1:35 PM, Rhonda Reidy wrote:> The following variables have the following significant relationships > (x is the explanatory variable): linear, cubic, exponential, > logistic. The linear relationship plots without any trouble. > > Cubic is the 'best' model, but it is not plotting as a smooth curve > using the following code: > > cubic.lm<- lm(y~poly(x,3))Try: lines(0:80, predict(cubic.lm, data.frame(x=0:80)),lwd=2) But I really must say the superiority of that relationship over a linear one is far from convincing to my eyes. Seems to be caused by one data point. I hope the quotes around "best" mean tha tyou have the same qualms.> lines(x,predict(cubic.lm),lwd=2) > > How do I plot the data and the estimated curves for all of these > regression models in the same plot? > > x <- c(62.5,68.5,0,52,0,52,0,52,23.5,86,0,0,0,0,0,0,0,0,0,0) > > y <- > c > (0.054,0.055,0.017,0.021,0.020,0.028,0.032,0.073,0.076,0.087,0.042,0.042,0.041,0.045,0.021,0.018,0.017,0.018,0.028,0.022 > ) > > Thanks in advance. > > Rhonda Reidy >-- David Winsemius, MD Heritage Laboratories West Hartford, CT
Dear Rhonda, Consider curve(). KeithC. -----Original Message----- From: Rhonda Reidy [mailto:rreidy at gmail.com] Sent: Saturday, February 13, 2010 11:36 AM To: r-help at r-project.org Subject: [R] Plot different regression models on one graph The following variables have the following significant relationships (x is the explanatory variable): linear, cubic, exponential, logistic. The linear relationship plots without any trouble. Cubic is the 'best' model, but it is not plotting as a smooth curve using the following code: cubic.lm<- lm(y~poly(x,3)) lines(x,predict(cubic.lm),lwd=2) How do I plot the data and the estimated curves for all of these regression models in the same plot? x <- c(62.5,68.5,0,52,0,52,0,52,23.5,86,0,0,0,0,0,0,0,0,0,0) y <- c(0.054,0.055,0.017,0.021,0.020,0.028,0.032,0.073,0.076,0.087,0.042,0.042,0. 041,0.045,0.021,0.018,0.017,0.018,0.028,0.022) Thanks in advance. Rhonda Reidy [[alternative HTML version deleted]]
Reasonably Related Threads
- Bug#500017: ignore.d.server/ssh: outdated 'reverse mapping checking ... failed' rule
- Bug#489172: logcheck: please add Auto-Submitted header field to mailouts
- Bug#503000: logcheck fails due to a perl warning
- How to plot linear, cubic and quadratic fitting curve in a figure?
- cubic spline