I'm trying to plot some linear and polynomical regressions with ggplot
. This is very straightforward when estimating the regression coefficients inside the geom_smoot
function:
ggplot (mtcars, aes(x=wt, y=mpg, fill=factor(cyl), colour=factor(cyl))) + geom_smooth(method='lm', formula = y ~ poly(x,2)) + geom_point()
However, here I want to plot just a prediction (or more, as in the example above) based on previous knowledge about the regression parameters.
So here with my regression estimates can by printed by:
dlply(mtcars,.(cyl), lm, formula=mpg ~ poly(wt,2)) %>%
llply(summary) %>%
ldply(coefficients)
Now I want to build the plot in a reverse way, from the estimates to the plot. Or even better, to build a prediction from other values for these estimates (ej. Intercept=20
, poly(wt,2)1=-15
and poly(wt,2)2=4
for cyl=4
), and then obtain a plot as the one above.
But here is where I dont know how to proceed. I guess I need to use a different geom_smooth
, geom_line
or similar for each level of cyl
, including in each of these the corresponding values of the estimates, but cannot figure out how.
I usually do this by generating a dataframe of predictions.
mod <- lm(mpg ~ poly(wt,2), mtcars)
pred <- data.frame(wt = seq(0,6,0.01))
pred$mpg <- predict(mod, pred)
ggplot() +
geom_line(data = pred, aes(x=wt, y=mpg)) +
geom_point(data = mtcars, aes(x=wt, y=mpg, colour=factor(cyl)))
You can, of course, change the parameters to whatever you like.
pred$mpg <- 57 - pred$wt * 21 + pred$wt^2 * 3.3
Alternatively, you can use stat_function
:
ggplot(pred, aes(x=wt)) +
stat_function(fun = function(x) 57 - 21*x + 3.3*x^2) +
geom_point(data = mtcars, aes(y=mpg, colour=factor(cyl)))
A final point: you can't interpret the coefficients of a poly() fit the way you think you would .
I thought this might be a good exercise to look at the broom
package. I wasn't quite sure which way you wanted to go, so here's some examples of what I found:
Plotting regressions:
I don't know how you plot your polynomial function, so that's an exercise for you, but here's some code to get a polynomial regression into a data frame:
library(dplyr)
library(broom)
library(tidyr)
mtcars %>% group_by(cyl) %>% do(tidy(lm(mpg ~ poly(wt, 2), data=.))) %>%
select(cyl, term, estimate) %>%
spread(term, estimate)
# Source: local data frame [3 x 4]
# Groups: cyl [3]
#
# cyl `(Intercept)` `poly(wt, 2)1` `poly(wt, 2)2`
# * <dbl> <dbl> <dbl> <dbl>
# 1 4 26.66364 -10.170962 3.003872
# 2 6 19.74286 -2.426656 -1.589859
# 3 8 15.10000 -6.003055 -1.933630
But here's one for a linear regression:
fit <- mtcars %>% group_by(cyl) %>% do(tidy(lm(mpg ~ wt, data=.))) %>%
select(cyl, term, estimate) %>%
spread(term, estimate)
ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point() +
geom_abline(data=fit, aes(slope=wt, intercept=`(Intercept)`, colour=cyl))
You can't just plot the fit, as you'll need to provide x- and y-values, so perhaps some predicted values:
wt <- c(2:5)
mtcars %>% group_by(cyl) %>% do(augment(lm(mpg ~ poly(wt, 2), data=.), newdata=data.frame(wt=wt))) %>%
ggplot(aes(x=wt, y=.fitted, group=cyl, colour=cyl)) + geom_line()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.