[R] graphing repeated curves

Richard Sherman r@@@pdx @ending from gm@il@com
Thu Aug 23 01:07:02 CEST 2018


Hi all,

I have a simple graphing question that is not really a graphing question, but a question about repeating a task.

I’m fiddling with some of McElreath’s Statistical Rethinking, and there’s a graph illustrating extreme overfitting (a number of polynomial terms in x equal to the number of observations), a subject I know well having taught it to grad students for many years.

The plot I want to reproduce has, in effect:

m1 <- lm( y ~ x)
m2 <- lm( y ~ x + x^2)

…etc., through lm( y ~ x + x^2 + x^3 + x^4 + x^5 + x^6 ), followed by some plot() or lines() or ggplot2() call to render the data and fitted curves.

Obviously I don’t want to run such regressions for any real purpose, but I think it might be useful to learn how to do such a thing in R without writing down each lm() call individually. It’s not obvious where I’d want to apply this, but I like learning how to repeat things in a compact way. 

So, something like:

data( mtcars )
d <- mtcars
v <- c( 1 , 2 , 3 , 4 , 5 , 6  )
m1 <- lm( mpg ~ hp  , data = d )

and then somehow use for() with an index or some flavor of apply() with the vector v to repeat this process yielding

m2 <- lm( mpg ~ hp + I( hp ^2 ) , data=d)
m3 <- lm( mpg ~ hp + I( hp^2 ) + I(hp^3) , data=d )

… and the rest through m6 <- lm( mpg ~ hp + I(hp^2) + I(hp^3) + I(hp^4) + I(hp^5) + I(hp^6) , data=d )

But finding a way to index these values including not just each value but each value+1 , then value+1 and value+2, and so on escapes me. Obviously I don’t want to include index values below zero. 

===
Richard Sherman
rss.pdx using gmail.com



More information about the R-help mailing list