[R] linear trend line and a quadratic trend line.
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Sun Nov 8 18:44:54 CET 2009
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
More information about the R-help
mailing list