gpt4 book ai didi

r - 在 purrr 中迭代略有不同的模型

转载 作者:行者123 更新时间:2023-12-02 09:04:22 25 4
gpt4 key购买 nike

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

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)")

是否有更有效的方法来迭代我的模型?我觉得我编写的代码超出了我的需要。

最佳答案

您可以通过对 map 的外部调用来迭代多项式阶数以及对 map 的内部调用来迭代 10 次折叠来迭代模型。在下面的代码中,我使用了 poly(wt, i) 而不是 I(wt^i),因为 I(wt^i) code> 生成仅包含最高阶项的多项式,而 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)")

enter image description here

关于r - 在 purrr 中迭代略有不同的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47962215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com