-
ggplot2 패키지에서 제공하는 diamonds 데이터의 cut과 price, cut과 carat, carat과 price 강한 관계가 있다. 이러한 강력한 관계를 제거하기 위하여 다음과 같이 model를 사용할 수 있다.
install.packages("tidyverse")
install.packages("modelr")
library(tidyverse)
library(modelr)# 모형
diamonds.model <- lm(log(price) ~ log(carat), data = diamonds)# 산점도
diamonds %>%
modelr::add_residuals(diamonds.model) %>%
dplyr::mutate(resid = exp(resid)) %>%
ggplot2::ggplot(mapping = aes(x = carat, y = resid)) +
geom_point()# 상자그림
diamonds %>%
modelr::add_residuals(diamonds.model) %>%
dplyr::mutate(resid = exp(resid)) %>%
ggplot2::ggplot(mapping = aes(x = cut, y = resid)) +
geom_boxplot()[출처] R for Data Science, Hadley Wickham & Garrett Grolemund, O'REILLY, p106~107