|
Hi R experts, Could you please help me to fit a linear, cubic and quadratic curve in a figure? I was trying to show all these three fitting curves with different colour in one figure. I spent substantial time to figure it out, but I could not. I have given here a example and what I did for linear, but no idea for cubic and quadratic fitting curve > dput(test) structure(list(sp = c(4L, 5L, 9L, 12L, 14L), env = c(12L, 18L, 20L, 17L, 15L)), .Names = c("sp", "env"), class = "data.frame", row.names = c(NA, -5L)) > plot(test$sp~test$env, main = "S vs. temp", xlim=c(0,20), ylim=c(0,14), ylab="S",xlab="env") > linear<-lm(test$sp~test$env) > quadratic<-lm(test$sp~test$env+I(test$env^2)) > #summary(quadratic) > cubic<-lm(test$sp~test$env+I(test$env^2)+I(test$env^3)) > #summary(cubic) > #fitting curve > abline(linear) > Here I did for linear, but now I don't how I can plot quadratic and cubic line in the figure with different colour. I further wanted to put r2 value on the top of the fitting line. ON linear fitting curve, I can see the line originated from left to right (cover whole x axis). I could not plot the line only within the data set. Would any one help me to figure it? I think it is not difficult but for me- it is really taking time. Thanks and waiting for your suggestions sincerely, Kristi Glover [[alternative HTML version deleted]] ______________________________________________ [hidden email] 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. |
|
Hi, Kristi-
Here is an option. ?curve...to add the polynomials to your graph ?text....for adding the R2 to you plot Ken On 06/13/12, Kristi Glover wrote: > > Hi R experts, > Could you please help me to fit a linear, cubic and quadratic curve in a figure? I was trying to show all these three fitting curves with different colour in one figure. > I spent substantial time to figure it out, but I could not. > > I have given here a example and what I did for linear, but no idea for cubic and quadratic fitting curve > > > > dput(test) > structure(list(sp = c(4L, 5L, 9L, 12L, 14L), env = c(12L, 18L, > 20L, 17L, 15L)), .Names = c("sp", "env"), class = "data.frame", row.names = c(NA, > -5L)) > > plot(test$sp~test$env, main = "S vs. temp", xlim=c(0,20), ylim=c(0,14), ylab="S",xlab="env") > > linear<-lm(test$sp~test$env) > > quadratic<-lm(test$sp~test$env+I(test$env^2)) > > #summary(quadratic) > > cubic<-lm(test$sp~test$env+I(test$env^2)+I(test$env^3)) > > #summary(cubic) > > #fitting curve > > abline(linear) > > > Here I did for linear, but now I don't how I can plot quadratic and cubic line in the figure with different colour. I further wanted to put r2 value on the top of the fitting line. > ON linear fitting curve, I can see the line originated from left to right (cover whole x axis). I could not plot the line only within the data set. > Would any one help me to figure it? I think it is not difficult but for me- it is really taking time. > > Thanks and waiting for your suggestions > > sincerely, > Kristi Glover > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
|
In reply to this post by Kristi Glover
> dput(test) structure(list(sp = c(4L, 5L, 9L, 12L, 14L), env = c(12L, 18L, 20L, 17L, 15L)), .Names = c("sp", "env"), class = "data.frame", row.names = c(NA, -5L)) > plot(test$sp~test$env, main = "S vs. temp", xlim=c(0,20), ylim=c(0,14), > ylab="S",xlab="env") > linear<-lm(test$sp~test$env) > quadratic<-lm(test$sp~test$env+I(test$env^2)) > #summary(quadratic) > cubic<-lm(test$sp~test$env+I(test$env^2)+I(test$env^3)) > #summary(cubic) > #fitting curve > abline(linear) > sincerely, Kristi Glover Try adding the following lines of code cq = coef(quadratic) cc = coef(cubic) newenv = seq(min(test$env), max(test$env), by = (max(test$env) - min(test$env))/500) sp.quad = cq[1] + cq[2]*newenv +cq[3]*newenv^2 lines(newenv,sp.quad, col='red') sp.cubic = cc[1] + cc[2]*newenv +cc[3]*newenv^2 +cc[4]*newenv^3 lines(newenv, sp.cubic, col='blue', lty=2) ------------------------------------------ Robert W. Baer, Ph.D. Professor of Physiology Kirksville College of Osteopathic Medicine A. T. Still University of Health Sciences 800 W. Jefferson St. Kirksville, MO 63501 660-626-2322 FAX 660-626-2965 ______________________________________________ [hidden email] 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. |
|
It is more direct to use predict() instead of reconstructing
by hand the prediction expression from the formula given to lm(). E.g., > x <- seq(1,6,by=1/4) > y <- sin(x) + rnorm(length(x), 0, 1/4) > plot(x, y) > fits <- lapply(1:3, function(degree)lm(y~poly(x, deg=degree))) > xpred <- pretty(x, n=50) > predictions <- lapply(fits, predict, newdata=list(x=xpred)) > invisible(lapply(seq_along(fits), function(i)lines(xpred, predictions[[i]], col=i))) If you change your fitting function, say to rq, or your formula, say to use a categorical variable or interaction, you don't have to change anything else, as the predict method for a model type takes care of the details. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -----Original Message----- > From: [hidden email] [mailto:[hidden email]] On Behalf > Of Robert Baer > Sent: Wednesday, June 13, 2012 8:40 AM > To: Kristi Glover; R-help > Subject: Re: [R] How to plot linear, cubic and quadratic fitting curve in a figure? > > > > dput(test) > structure(list(sp = c(4L, 5L, 9L, 12L, 14L), env = c(12L, 18L, > 20L, 17L, 15L)), .Names = c("sp", "env"), class = "data.frame", row.names = > c(NA, > -5L)) > > plot(test$sp~test$env, main = "S vs. temp", xlim=c(0,20), ylim=c(0,14), > > ylab="S",xlab="env") > > linear<-lm(test$sp~test$env) > > quadratic<-lm(test$sp~test$env+I(test$env^2)) > > #summary(quadratic) > > cubic<-lm(test$sp~test$env+I(test$env^2)+I(test$env^3)) > > #summary(cubic) > > #fitting curve > > abline(linear) > > > Thanks and waiting for your suggestions > > sincerely, > Kristi Glover > > Try adding the following lines of code > cq = coef(quadratic) > cc = coef(cubic) > newenv = seq(min(test$env), max(test$env), by = (max(test$env) - > min(test$env))/500) > sp.quad = cq[1] + cq[2]*newenv +cq[3]*newenv^2 > lines(newenv,sp.quad, col='red') > > sp.cubic = cc[1] + cc[2]*newenv +cc[3]*newenv^2 +cc[4]*newenv^3 > lines(newenv, sp.cubic, col='blue', lty=2) > > ------------------------------------------ > Robert W. Baer, Ph.D. > Professor of Physiology > Kirksville College of Osteopathic Medicine > A. T. Still University of Health Sciences > 800 W. Jefferson St. > Kirksville, MO 63501 > 660-626-2322 > FAX 660-626-2965 > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
|
See also the scripts in http://biostat.mc.vanderbilt.edu/BioMod
Frank
Frank Harrell
Department of Biostatistics, Vanderbilt University |
|
In reply to this post by Kristi Glover
On Jun 13, 2012, at 10:29 AM, Kristi Glover wrote: > > Hi R experts, > Could you please help me to fit a linear, cubic and quadratic curve > in a figure? I was trying to show all these three fitting curves > with different colour in one figure. > I spent substantial time to figure it out, but I could not. > > I have given here a example and what I did for linear, but no idea > for cubic and quadratic fitting curve > > >> dput(test) > structure(list(sp = c(4L, 5L, 9L, 12L, 14L), env = c(12L, 18L, > 20L, 17L, 15L)), .Names = c("sp", "env"), class = "data.frame", > row.names = c(NA, > -5L)) >> plot(test$sp~test$env, main = "S vs. temp", xlim=c(0,20), >> ylim=c(0,14), ylab="S",xlab="env") >> linear<-lm(test$sp~test$env) >> quadratic<-lm(test$sp~test$env+I(test$env^2)) >> #summary(quadratic) >> cubic<-lm(test$sp~test$env+I(test$env^2)+I(test$env^3)) >> #summary(cubic) >> #fitting curve >> abline(linear) >> > Here I did for linear, but now I don't how I can plot quadratic and > cubic line in the figure with different colour. I further wanted to > put r2 value on the top of the fitting line. > ON linear fitting curve, I can see the line originated from left to > right (cover whole x axis). I could not plot the line only within > the data set. > Would any one help me to figure it? I think it is not difficult but > for me- it is really taking time. > preserves the statistical propriety of polynomial fits. > quadratic<-lm( sp ~ poly(env, 2), data=test) > #summary(quadratic) > cubic<-lm( sp ~ poly(env, 3), data=test) > #summary(cubic) > #fitting curve > plot( sp~ env, data=test, main = "S vs. temp", xlim=c(0,20), ylim=c(0,14), ylab="S",xlab="env") > abline(linear) > lines( test$env[order(test$env)] , predict(quadratic)[order(test $env)] ) > lines( test$env[order(test$env)] , predict(cubic)[order(test $env)] , col="red") You can substitute: lines( x=seq( min(test$env) , max(test$env), len=100) , y= predict(model, data.frame(env=seq( min(test$env) , max(test$env), len=100) ) ) ) ... if you want smoother output. -- David Winsemius, MD West Hartford, CT ______________________________________________ [hidden email] 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. |
| Powered by Nabble | Edit this page |
