Professional Documents
Culture Documents
As noted by Wickham and Grolemund (2018), it’s surprising that the diamond price
would decline with an increase of diamond clarity. As it turns out, if we account for
the carat of the diamond, then see that better diamond clarity does indeed lead to
a higher diamond price, as shown in Figure 2.12. Seeing such a strong pattern in
the residuals of simple linear model of carat vs price indicates that our model
could be greatly improved by adding clarity as a predictor of price.
m <- lm(log(price) ~ log(carat), data = diamonds)
diamonds <- modelr::add_residuals(diamonds, m)
p <- ggplot(diamonds, aes(x = clarity, y = resid, color = clarity)) +
ggforce::geom_sina(alpha = 0.1) +
stat_summary(fun.data = "mean_cl_boot", color = "black") +
facet_wrap(~cut)
toWebGL(ggplotly(p))
FIGURE 2.12: A sina plot of diamond price by clarity and cut, after accounting for
carat.