3

我有以下代码比较仅在多项式项上不同的模型的均方根值。

library(tidyverse)
data(mtcars)
cv_mtcars = mtcars %>%
  crossv_kfold(k = 10)
cv_mtcars %>%
  mutate(model1 = map(train, ~lm(disp ~ wt, data = .)),
         model2 = map(train, ~lm(disp ~I(wt^2), data = .)),
         model3 = map(train, ~lm(disp ~I(wt^3), data = .)),         
         model4 = map(train, ~lm(disp ~I(wt^4), data = .)),
         model5 = map(train, ~lm(disp ~I(wt^5), data = .)),
         model6 = map(train, ~lm(disp ~I(wt^6), data = .)),
        order1 = map2_dbl(model1, test, ~rmse(.x, .y)),
        order2 = map2_dbl(model2, test, ~rmse(.x, .y)),
        order3 = map2_dbl(model3, test, ~rmse(.x, .y)),
        order4 = map2_dbl(model4, test, ~rmse(.x, .y)),
        order5 = map2_dbl(model5, test, ~rmse(.x, .y)),
        order6 = map2_dbl(model6, test, ~rmse(.x, .y))) %>% 
  select(order1,order2,order3,order4,order5,order6) %>% gather(1:6,key=model,value=value) %>% 
  ggplot()+
  geom_point(aes(x=factor(model),y=value))+
  labs(y="rmse",x="polynomial",title="Model Assesment",subtitle="disp~I(wt^x)")

有没有更有效的方法来遍历我的模型?我觉得我写的代码比我需要的多。

4

1 回答 1

3

您可以通过外部调用map迭代多项式顺序和内部调用map迭代 10 折来迭代模型。在下面的代码中,我使用了 ,poly(wt, i)而不是I(wt^i),因为I(wt^i)生成了一个只有最高阶项poly(wt, i)的多项式,而生成了一个包含所有最高阶项的多项式。我已经保存了对象rmse中的每个折叠model_cv,但是您当然可以将其直接通过管道传输到 ggplot 中。

set.seed(50)
model_cv = setNames(1:6, 1:6) %>% 
  map_df(function(i) { 
    map2_dbl(cv_mtcars[["train"]], cv_mtcars[["test"]], function(train, test) {
      model = lm(disp ~ poly(wt,i), data=train)
      rmse(model, test)
    })
  }) %>% 
  gather(`Polynomial Order`, rmse)

ggplot(model_cv, aes(`Polynomial Order`, rmse)) +
  geom_point() +
  stat_summary(fun.y=mean, geom="point", pch="_", colour="red", size=7) +
  labs(title="Model Assesment",subtitle="disp ~ poly(wt, order)")

在此处输入图像描述

于 2017-12-24T19:42:53.493 回答