## ----include = FALSE---------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 6, fig.height = 4 ) ## ----setup, message = FALSE--------------------------------------------------- library(Applr) library(ggplot2) ## ----------------------------------------------------------------------------- autoplot(lm(Petal.Length ~ Sepal.Length + I(Sepal.Length^2):Species, iris)) ## ----results = "hide"--------------------------------------------------------- model <- lm(hwy ~ displ + drv + cyl, data = mpg) autoplot(model, aes(color = factor(cyl))) + facet_wrap(~drv) autoplot(model, aes(x = cyl, color = factor(drv)), interval = "none") + labs(title = "The Same Model From a Different Angle") ## ----message = TRUE, fig.show = "hold", out.width = "49%", fig.width = 4, fig.height = 3.2---- set.seed(123) n <- 80 x <- runif(n, 0.1, 10) x2 <- runif(n, 0.1, 10) x3 <- sample(c(1, 20, 40), n, replace = TRUE) x4 <- sample(c(1, 2), n, replace = TRUE) y <- log(5 + x + x2 + x3 + 1/5 * x * x3 + 80 * x4) # no random noise dat <- data.frame(x, x2, x3, x4, y) model <- lm(exp(y) ~ x*x3 + x2 + x4, data = dat) # exact model fit ggplot(dat, aes(x, y)) + geom_point() + geom_slice(model) + labs(title = "A Perfectly Fit Model 'Slice'") # The same model against its full linear predictor: an exact fit ggplot(dat, aes(x = log(5 + x + x2 + x3 + 1/5 * x * x3 + 80 * x4), y)) + geom_point() + geom_slice(model) + labs(title = "Same Model, Different X-Axis") ## ----------------------------------------------------------------------------- ggplot(dat, aes(x, y, color = factor(x3))) + geom_point() + geom_slice(model, band = "x2", predict_vars = list(x2 = c(0, 10))) + facet_wrap(~x4) + geom_slice_text() + geom_slice_subtitle() ## ----------------------------------------------------------------------------- model <- lm(Sepal.Length ~ Sepal.Width + Petal.Length, iris) ggplot(iris, aes(Petal.Length, Sepal.Length)) + geom_point() + geom_slice( model, predict_vars = list(Sepal.Width = c(2, 3, 4)), interval = "confidence" ) + geom_slice_text() ## ----fig.show = "hold", out.width = "49%", fig.width = 4, fig.height = 3.2---- model <- lm(hwy ~ displ + I(displ^2), data = mpg) ggplot(mpg, aes(displ, hwy)) + geom_point() + geom_slice(model, interval = "confidence") + labs(title = "Confidence Interval") ggplot(mpg, aes(displ, hwy)) + geom_point() + geom_slice(model, interval = "prediction") + labs(title = "Prediction Interval") ## ----------------------------------------------------------------------------- model <- lm(hwy ~ displ * drv + cyl, data = mpg) # Inferred: cyl is the only predictor not shown, so it spans 4 to 8 ggplot(mpg, aes(displ, hwy, color = drv)) + geom_point() + geom_slice(model, band = TRUE) + geom_slice_text() # Specified: cyl from 5 to 7 ggplot(mpg, aes(displ, hwy, color = drv)) + geom_point() + geom_slice(model, band = "cyl", predict_vars = list(cyl = c(5, 7))) + geom_slice_text() ## ----fig.show = "hold", out.width = "33%", fig.width = 2.5, fig.height = 3.2---- model <- lm(log(mpg) ~ disp + hp, data = mtcars) ggplot(mtcars, aes(disp, mpg)) + geom_point() + geom_slice(model) + labs(title = "normal y axis") ggplot(mtcars, aes(disp, log(mpg))) + geom_point() + geom_slice(model) + labs(title = "log() y axis") ggplot(mtcars, aes(disp, mpg)) + geom_point() + geom_slice(model) + scale_y_log10() + labs(title = "scale_y_log10() axis") ## ----fig.show = "hold", out.width = "49%", fig.width = 4, fig.height = 3.2---- model <- lm(hwy ~ displ + I(displ^2) + displ:drv, data = mpg) ggplot(mpg, aes(displ, hwy, color = drv)) + geom_point() + geom_slice(model) ggplot(mpg, aes(displ, hwy, color = drv)) + geom_point() + geom_slice(model, full_range = TRUE) ## ----fig.width = 7.5---------------------------------------------------------- set.seed(123) n <- 80 x <- runif(n, -10, 10) x2 <- runif(n, 0, 4) g <- factor(sample(c("A", "B"), n, replace = TRUE)) y <- x * ifelse(g == "A", 0.5, 2) + ifelse(g == "A", 5, -5) + 4 * x2 + rnorm(n) dat <- data.frame(x, x2, g, y) model <- lm(y ~ x * g + x2, data = dat) ggplot(dat, aes(x, y)) + geom_point(alpha = 0.5) + geom_slice(model, predict_vars = list(x2 = c(0, 2, 4), g = c("A", "B"))) + geom_slice_text(style = "legend") ## ----------------------------------------------------------------------------- model <- lm(Petal.Length ~ Sepal.Length + I(Sepal.Length^2):Species + Sepal.Width, iris) ggplot(iris, aes(Sepal.Length, Petal.Length, color = Species)) + geom_point() + geom_slice(model, interval = "confidence") + geom_slice_subtitle()