R for Data Science总结之——modelr(2)

R for Data Science总结之——modelr(2)

本章针对真实数据集进行建模实践:

library(tidyverse)
library(modelr)
options(na.action = na.warn)

library(nycflights13)
library(lubridate)

为什么低质量的钻石更加昂贵?

首先查看diamonds数据集:

ggplot(diamonds, aes(cut, price)) + geom_boxplot()
ggplot(diamonds, aes(color, price)) + geom_boxplot()
ggplot(diamonds, aes(clarity, price)) + geom_boxplot()

R for Data Science总结之——modelr(2)
R for Data Science总结之——modelr(2)
R for Data Science总结之——modelr(2)
再查看连续变量克拉与价格的关系:

ggplot(diamonds, aes(carat, price)) + 
  geom_hex(bins = 50)

R for Data Science总结之——modelr(2)
再针对小于2.5克拉的钻石,抛弃极值点:

diamonds2 <- diamonds %>% 
  filter(carat <= 2.5) %>% 
  mutate(lprice = log2(price), lcarat = log2(carat))
ggplot(diamonds2, aes(lcarat, lprice)) + 
  geom_hex(bins = 50)

R for Data Science总结之——modelr(2)
对对数化数据进行建模:

mod_diamond <- lm(lprice ~ lcarat, data = diamonds2)

grid <- diamonds2 %>% 
  data_grid(carat = seq_range(carat, 20)) %>% 
  mutate(lcarat = log2(carat)) %>% 
  add_predictions(mod_diamond, "lprice") %>% 
  mutate(price = 2 ^ lprice)

ggplot(diamonds2, aes(carat, price)) + 
  geom_hex(bins = 50) + 
  geom_line(data = grid, colour = "red", size = 1)

R for Data Science总结之——modelr(2)
再查看残差:

diamonds2 <- diamonds2 %>% 
  add_residuals(mod_diamond, "lresid")

ggplot(diamonds2, aes(lcarat, lresid)) + 
  geom_hex(bins = 50)

再查看之前的几个变量与残差的关系:

ggplot(diamonds2, aes(cut, lresid)) + geom_boxplot()
ggplot(diamonds2, aes(color, lresid)) + geom_boxplot()
ggplot(diamonds2, aes(clarity, lresid)) + geom_boxplot()

R for Data Science总结之——modelr(2)
R for Data Science总结之——modelr(2)
R for Data Science总结之——modelr(2)
可以看出这三个变量都与残差有关,因此建立复杂模型:

mod_diamond2 <- lm(lprice ~ lcarat + color + cut + clarity, data = diamonds2)

然后使用data_grid()的.model参数修改过程,这里由于R版本问题有时会报错,若报错请查看博客底部GITHUB的详细代码:

grid <- diamonds2 %>% 
  data_grid(cut, .model = mod_diamond2) %>% 
  add_predictions(mod_diamond2)
grid
#> # A tibble: 5 x 5
#>   cut       lcarat color clarity  pred
#>   <ord>      <dbl> <chr> <chr>   <dbl>
#> 1 Fair      -0.515 G     VS2      11.2
#> 2 Good      -0.515 G     VS2      11.3
#> 3 Very Good -0.515 G     VS2      11.4
#> 4 Premium   -0.515 G     VS2      11.4
#> 5 Ideal     -0.515 G     VS2      11.4

ggplot(grid, aes(cut, pred)) + 
  geom_point()

R for Data Science总结之——modelr(2)
再查看残差:

diamonds2 <- diamonds2 %>% 
  add_residuals(mod_diamond2, "lresid2")

ggplot(diamonds2, aes(lcarat, lresid2)) + 
  geom_hex(bins = 50)

R for Data Science总结之——modelr(2)
我们再单独查看残差极大的值:

diamonds2 %>% 
  filter(abs(lresid2) > 1) %>% 
  add_predictions(mod_diamond2) %>% 
  mutate(pred = round(2 ^ pred)) %>% 
  select(price, pred, carat:table, x:z) %>% 
  arrange(price)
#> # A tibble: 16 x 11
#>   price  pred carat cut     color clarity depth table     x     y     z
#>   <int> <dbl> <dbl> <ord>   <ord> <ord>   <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1  1013   264  0.25 Fair    F     SI2      54.4    64  4.3   4.23  2.32
#> 2  1186   284  0.25 Premium G     SI2      59      60  5.33  5.28  3.12
#> 3  1186   284  0.25 Premium G     SI2      58.8    60  5.33  5.28  3.12
#> 4  1262  2644  1.03 Fair    E     I1       78.2    54  5.72  5.59  4.42
#> 5  1415   639  0.35 Fair    G     VS2      65.9    54  5.57  5.53  3.66
#> 6  1415   639  0.35 Fair    G     VS2      65.9    54  5.57  5.53  3.66
#> # ... with 10 more rows

什么影响每天的航班数量?

daily <- flights %>% 
  mutate(date = make_date(year, month, day)) %>% 
  group_by(date) %>% 
  summarise(n = n())
daily
#> # A tibble: 365 x 2
#>   date           n
#>   <date>     <int>
#> 1 2013-01-01   842
#> 2 2013-01-02   943
#> 3 2013-01-03   914
#> 4 2013-01-04   915
#> 5 2013-01-05   720
#> 6 2013-01-06   832
#> # ... with 359 more rows

ggplot(daily, aes(date, n)) + 
  geom_line()

R for Data Science总结之——modelr(2)
我们再看每一周的航班数量分布:

daily <- daily %>% 
  mutate(wday = wday(date, label = TRUE))
ggplot(daily, aes(wday, n)) + 
  geom_boxplot()

R for Data Science总结之——modelr(2)
可以看出周六航班极为稀少,下一步建模:

mod <- lm(n ~ wday, data = daily)

grid <- daily %>% 
  data_grid(wday) %>% 
  add_predictions(mod, "n")

ggplot(daily, aes(wday, n)) + 
  geom_boxplot() +
  geom_point(data = grid, colour = "red", size = 4)

R for Data Science总结之——modelr(2)
计算残差:

daily <- daily %>% 
  add_residuals(mod)
daily %>% 
  ggplot(aes(date, resid)) + 
  geom_ref_line(h = 0) + 
  geom_line()

R for Data Science总结之——modelr(2)
可以看出有一些拟合极度糟糕的情况,接下来按每周进行分类:

ggplot(daily, aes(date, resid, colour = wday)) + 
  geom_ref_line(h = 0) + 
  geom_line()

R for Data Science总结之——modelr(2)
可以看出周六的预测完全失败了,我们挑出极值点:

daily %>% 
  filter(resid < -100)
#> # A tibble: 11 x 4
#>   date           n wday  resid
#>   <date>     <int> <ord> <dbl>
#> 1 2013-01-01   842 Tue   -109.
#> 2 2013-01-20   786 Sun   -105.
#> 3 2013-05-26   729 Sun   -162.
#> 4 2013-07-04   737 Thu   -229.
#> 5 2013-07-05   822 Fri   -145.
#> 6 2013-09-01   718 Sun   -173.
#> # ... with 5 more rows

可以看出这里包括一些美国节日,这是导致航班数量与模型不吻合的原因。

daily %>% 
  ggplot(aes(date, resid)) + 
  geom_ref_line(h = 0) + 
  geom_line(colour = "grey50") + 
  geom_smooth(se = FALSE, span = 0.20)
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

R for Data Science总结之——modelr(2)
我们再观察一下季节性与星期间的关系:

daily %>% 
  filter(wday == "Sat") %>% 
  ggplot(aes(date, n)) + 
    geom_point() + 
    geom_line() +
    scale_x_date(NULL, date_breaks = "1 month", date_labels = "%b")

R for Data Science总结之——modelr(2)
这里我们创建term()函数来包含三个学期:

term <- function(date) {
  cut(date, 
    breaks = ymd(20130101, 20130605, 20130825, 20140101),
    labels = c("spring", "summer", "fall") 
  )
}

daily <- daily %>% 
  mutate(term = term(date)) 

daily %>% 
  filter(wday == "Sat") %>% 
  ggplot(aes(date, n, colour = term)) +
  geom_point(alpha = 1/3) + 
  geom_line() +
  scale_x_date(NULL, date_breaks = "1 month", date_labels = "%b")

R for Data Science总结之——modelr(2)
再看下星期间的差别:

daily %>% 
  ggplot(aes(wday, n, colour = term)) +
    geom_boxplot()

R for Data Science总结之——modelr(2)
这里看出学期中的区别时模型拟合不准确的一大因素,因此创建模型:

mod1 <- lm(n ~ wday, data = daily)
mod2 <- lm(n ~ wday * term, data = daily)

daily %>% 
  gather_residuals(without_term = mod1, with_term = mod2) %>% 
  ggplot(aes(date, resid, colour = model)) +
    geom_line(alpha = 0.75)

R for Data Science总结之——modelr(2)
可以看出残差明显缩小了,再观察拟合结果:

grid <- daily %>% 
  data_grid(wday, term) %>% 
  add_predictions(mod2, "n")

ggplot(daily, aes(wday, n)) +
  geom_boxplot() + 
  geom_point(data = grid, colour = "red") + 
  facet_wrap(~ term)

R for Data Science总结之——modelr(2)
我们可以发现拟合结果较好,但仍有极值点的存在,因此可使用RGM也就是MASS::rlm()消除极值点影响:

mod3 <- MASS::rlm(n ~ wday * term, data = daily)

daily %>% 
  add_residuals(mod3, "resid") %>% 
  ggplot(aes(date, resid)) + 
  geom_hline(yintercept = 0, size = 2, colour = "white") + 
  geom_line()

R for Data Science总结之——modelr(2)
可以看出除特殊日期外的残差明显减小了。
这里需要注意,如果项进行多次可视化,则需要计算新的变量,推荐使用函数进行计算:

compute_vars <- function(data) {
  data %>% 
    mutate(
      term = term(date), 
      wday = wday(date, label = TRUE)
    )
}

或者直接在模型中进行计算:

wday2 <- function(x) wday(x, label = TRUE)
mod3 <- lm(n ~ wday2(date) * term(date), data = daily)

也可以用多项式拟合:

library(splines)
mod <- MASS::rlm(n ~ wday * ns(date, 5), data = daily)

daily %>% 
  data_grid(wday, date = seq_range(date, n = 13)) %>% 
  add_predictions(mod) %>% 
  ggplot(aes(date, pred, colour = wday)) + 
    geom_line() +
    geom_point()

R for Data Science总结之——modelr(2)
这里也推荐学习caret包进行高级建模The caret Package
全文代码已上传GITHUB点此进入