In the R statistical package, is there a way to plot a graph of a second order polynomial regression with one continuous variable and one categorical variable?
To generate a linear regression graph with one categorical variable:
library(ggplot2)
library(ggthemes) ## theme_few()
set.seed(1)
df <- data.frame(minutes = runif(60, 5, 15), endtime=60, category="a")
df$category = df$category=letters[seq( from = 1, to = 2 )]
df$endtime = df$endtime df$minutes^3/180 df$minutes*runif(60, 1, 2)
ggplot(df, aes(y=endtime, x=minutes, col = category))
geom_point()
geom_smooth(method=lm)
theme_few()
To plot a polynomial graph with one one continuous variable:
ggplot(df, aes(x=minutes, y=endtime))
geom_point()
stat_smooth(method='lm', formula = y ~ poly(x,2), size = 1)
xlab('Minutes of warm up')
ylab('End time')
But I can’t figure out how to plot a polynomial graph with one continuous variable and one categorical variable.
CodePudding user response:
Just add a colour
or group
mapping. This will make ggplot fit and display separate polynomial regressions for each category. (1) It's not possible to display an additive mixed-polynomial regression (i.e. lm(y ~ poly(x,2) category)
); (2) what's shown here is not quite equivalent to the results of the interaction model lm(y ~ poly(x,2)*col)
, because the residual variances (and hence the widths of the confidence ribbons) are estimated separately for each group.
ggplot(df, aes(x=minutes, y=endtime, col = category))
geom_point()
stat_smooth(method='lm', formula = y ~ poly(x,2))
labs(x = 'Minutes of warm up', y = 'End time')
theme_few()