我一直在try 使用一些tidyModels工具进行10倍交叉验证, 虽然一切看起来都很正常,但我在使用增强功能扩展模型时遇到了麻烦.该错误似乎与按rSample::analysis()拆分数据集有关,并且该扩展不适用于类:Function的对象.
我可以看到预测是存在的,例如,cv_Models[[3]][[1]]$fit,并且折叠也存在,例如,Split_Folds$Splits[[1]]%>;%analysis(), 但增强失败了.
如有任何线索,我们将不胜感激!
library(tidymodels); library(nlme); library(broom.mixed)
#> Warning: package 'scales' was built under R version 4.3.1
#> Warning: package 'dplyr' was built under R version 4.3.1
#> Warning: package 'ggplot2' was built under R version 4.3.1
#> Warning: package 'modeldata' was built under R version 4.3.1
#> Warning: package 'recipes' was built under R version 4.3.1
#> Warning: package 'yardstick' was built under R version 4.3.1
#>
#> Attaching package: 'nlme'
#> The following object is masked from 'package:dplyr':
#>
#> collapse
data <-
tibble(
co2_mean_tot = rlnorm(n = 100, meanlog = 0, sdlog = 1),
WALA = rlnorm(n = 100, meanlog = 0, sdlog = 1),
precip_av = rnorm(n = 100, mean = 400, sd = co2_mean_tot),
soil_c_kgC = rnorm(n = 100, mean = 40, sd = 5),
area = rlnorm(n = 100, meanlog = 0, sdlog = 1),
long = seq(100,180, length.out = 100))
split_folds <- vfold_cv(data, strata = long, v = 10)
fit_gls_cv <-
function(split) {
gls(log10(co2_mean_tot) ~ scale(log10(WALA))*scale(precip_av)*scale(soil_c_kgC) + scale(log10(area)),
weights = varExp(form = ~ precip_av),
data = rsample::analysis(split), method = "REML")
}
cv_models <-
split_folds %>%
mutate(model = map(splits, fit_gls_cv),
coef_info = map(model, tidy),
gl_info = map(model, glance),
pred = map(model, predict))
cv_models %>%
mutate(aug = map(model, augment))
#> Error in `mutate()`:
#> ℹ In argument: `aug = map(model, augment)`.
#> Caused by error in `map()`:
#> ℹ In index: 1.
#> Caused by error in `rsample::analysis()`:
#> ! No method for objects of class: function
#> Backtrace:
#> ▆
#> 1. ├─cv_models %>% mutate(aug = map(model, augment))
#> 2. ├─dplyr::mutate(., aug = map(model, augment))
#> 3. ├─dplyr:::mutate.data.frame(., aug = map(model, augment))
#> 4. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
#> 5. │ ├─base::withCallingHandlers(...)
#> 6. │ └─dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
#> 7. │ └─mask$eval_all_mutate(quo)
#> 8. │ └─dplyr (local) eval()
#> 9. └─purrr::map(model, augment)
#> 10. └─purrr:::map_("list", .x, .f, ..., .progress = .progress)
#> 11. ├─purrr:::with_indexed_errors(...)
#> 12. │ └─base::withCallingHandlers(...)
#> 13. ├─purrr:::call_with_cleanup(...)
#> 14. ├─generics (local) .f(.x[[i]], ...)
#> 15. └─broom.mixed:::augment.gls(.x[[i]], ...)
#> 16. ├─broom::augment_columns(x, data, newdata, se.fit = NULL)
#> 17. ├─nlme::getData(x)
#> 18. └─nlme:::getData.gls(x)
#> 19. ├─base::eval(if ("data" %in% names(object)) object$data else mCall$data)
#> 20. │ └─base::eval(if ("data" %in% names(object)) object$data else mCall$data)
#> 21. ├─rsample::analysis(split)
#> 22. └─rsample:::analysis.default(split)
#> 23. └─cli::cli_abort("No method for objects of class{?es}: {cls}")
#> 24. └─rlang::abort(...)
Created on 2024-01-28 with reprex v2.0.2