Dear list users How is it possible to visualise both a linear trend line and a quadratic trend line on a plot of two variables? Here my almost working exsample. data(Duncan) attach(Duncan) plot(prestige ~ income) abline(lm(prestige ~ income), col=2, lwd=2) Now I would like to add yet another trend line, but this time a quadratic one. So I have two trend lines. One linear trend line and a quadratic trend line. A trend line as if I had taken I(income^2) into the equation. I know I can make two models and compare them using anova, but for pedagogical resons I wold like to visualise it. I know the trick from my past as an SPSS user, but I would like to do this in R as well. Se it in SPSS http://www.childrens-mercy.org/stats/weblog2006/QuadraticRegression.asp Thanks in adcance Eric
linear trend line and a quadratic trend line.
3 messages · Eric Fail, Peter Dalgaard, ONKELINX, Thierry
Eric Fail wrote:
Dear list users How is it possible to visualise both a linear trend line and a quadratic trend line on a plot of two variables? Here my almost working exsample. data(Duncan) attach(Duncan) plot(prestige ~ income) abline(lm(prestige ~ income), col=2, lwd=2) Now I would like to add yet another trend line, but this time a quadratic one. So I have two trend lines. One linear trend line and a quadratic trend line. A trend line as if I had taken I(income^2) into the equation. I know I can make two models and compare them using anova, but for pedagogical resons I wold like to visualise it. I know the trick from my past as an SPSS user, but I would like to do this in R as well. Se it in SPSS http://www.childrens-mercy.org/stats/weblog2006/QuadraticRegression.asp
There's no precooked function that I am aware of, but the generic way is like rg <- range(income) N <- 200 x <- seq(rg[1], rg[2],, N) pred <- predict(lm(prestige~ income+I(income^2)), newdata=data.frame(income=x)) lines(x, pred) as usual, "like" means that if you can't be bothered with making your example reproducible, I can't be bothered with testing the code! Well, actually, I found the Duncan data in library(car), so I did in fact test...
O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
1 day later
Here is a solution using ggplot2 n <- 50 Duncan <- data.frame(income = runif(n, 0, 50000)) Duncan$prestige <- with(Duncan, 0.01 * income - .0000001 * income ^2 + rnorm(n, sd = 10)) library(ggplot2) ggplot(Duncan, aes(y = prestige, x = income)) + #define the data geom_point() + #add the data points geom_smooth(method = "lm", colour = "red") + #add a linear trend geom_smooth(method = "lm", formula = y ~ poly(x, 2), color = "green") #add the quadratic trend HTH, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx at inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens Eric Fail Verzonden: zondag 8 november 2009 18:21 Aan: r-help at r-project.org Onderwerp: [R] linear trend line and a quadratic trend line. Dear list users How is it possible to visualise both a linear trend line and a quadratic trend line on a plot of two variables? Here my almost working exsample. data(Duncan) attach(Duncan) plot(prestige ~ income) abline(lm(prestige ~ income), col=2, lwd=2) Now I would like to add yet another trend line, but this time a quadratic one. So I have two trend lines. One linear trend line and a quadratic trend line. A trend line as if I had taken I(income^2) into the equation. I know I can make two models and compare them using anova, but for pedagogical resons I wold like to visualise it. I know the trick from my past as an SPSS user, but I would like to do this in R as well. Se it in SPSS http://www.childrens-mercy.org/stats/weblog2006/QuadraticRegression.asp Thanks in adcance Eric ______________________________________________ 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. Druk dit bericht a.u.b. niet onnodig af. Please do not print this message unnecessarily. Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document. The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.