You need to enable JavaScript to run this app. Tidymodels装袋树10折交叉验证报错:Input must be a vector not NULL
优惠活动
大模型
产品
解决方案
定价
更多

Tidymodels装袋树10折交叉验证报错:Input must be a vector not NULL

概述

我使用tidymodels包基于数据框FID构建了4类模型:

  • 广义线性模型
  • 装袋树(Bagged Tree)
  • 随机森林
  • 提升树(Boosted Trees)

该数据框包含3个预测变量:

  • Year(数值型)
  • Month(因子型)
  • Days(数值型)

因变量为Frequency(数值型)

问题描述

我在使用bag_tree()构建装袋树回归模型、结合fit_resamples()开展10折交叉验证拟合时,出现所有交叉验证折报错Input must be a vector, not NULL的问题,需要定位该报错的触发原因并给出解决方法。
目前公开可查的相关参考资料较少,已找到的同类报错均与逻辑回归场景相关,并非装袋树模型的使用场景。

报错输出内容:

x Fold01: model: Error: Input must be a vector, not NULL.
x Fold02: model: Error: Input must be a vector, not NULL.
x Fold03: model: Error: Input must be a vector, not NULL.
x Fold04: model: Error: Input must be a vector, not NULL.
x Fold05: model: Error: Input must be a vector, not NULL.
x Fold06: model: Error: Input must be a vector, not NULL.
x Fold07: model: Error: Input must be a vector, not NULL.
x Fold08: model: Error: Input must be a vector, not NULL.
x Fold09: model: Error: Input must be a vector, not NULL.
x Fold10: model: Error: Input must be a vector, not NULL.

Warning message:

All models failed in [fit_resamples()]. See the `.notes` column. 
复现问题的R代码
## 加载tidymodels相关包
library(tidymodels)
library(glmnet)
library(parsnip)
library(rpart.plot)
library(rpart)
library(tidyverse) # 数据操作
library(skimr) # 数据探索
library(baguette) # 装袋树模型
library(future) # 并行计算
library(xgboost) # 提升树模型
library(ranger)
library(yardstick)
library(purrr)
library(forcats)
library(rlang)
library(poissonreg)

# 拆分训练集和测试集
data_split <- initial_split(FID)
train_data <- training(data_split)
test_data  <- testing(data_split)

# 构建10折交叉验证重抽样对象
cv <- vfold_cv(train_data, v=10)

###########################################################
## 构建数据预处理配方
rec <- recipe(Frequency ~ ., data = FID) %>% 
          step_nzv(all_predictors(), freq_cut = 0, unique_cut = 0) %>% # 移除零方差变量
          step_novel(all_nominal()) %>% # 处理预测集中新出现的因子水平
          step_medianimpute(all_numeric(), -all_outcomes(), -has_role("id vars"))  %>% # 数值型缺失值用中位数填充
          step_dummy(all_nominal(), -has_role("id vars")) # 分类变量转哑变量编码

##### 定义装袋树模型
mod_bag <- bag_tree() %>%
                    set_mode("regression") %>%
                          set_engine("rpart", times = 10) # 10次自助抽样

## 更新模型的复杂度惩罚参数
Updated_bag<-update(mod_bag, cost_complexity=1)

## 构建建模工作流
wflow_bag <- workflow() %>% 
                   add_recipe(rec) %>%
                        add_model(Updated_bag)

## 在训练集上拟合并查看模型结果
bag_fit_model <- fit(wflow_bag, data = train_data)

bag_fit_model %>%
       pull_workflow_fit() 

## 在训练集上做预测
bag_predict<-predict(bag_fit_model, train_data)

## 开启并行计算开展交叉验证
plan(multisession)

fit_bag <- fit_resamples(
                      wflow_bag,
                      cv,
                      metrics = metric_set(rmse, rsq),
                      control = control_resamples(save_pred = TRUE,
                      extract = function(x) extract_model(x)))
所用数据框FID结构
structure(list(Year = c(2015, 2015, 2015, 2015, 2015, 2015, 2015, 
2015, 2015, 2015, 2015, 2015, 2016, 2016, 2016, 2016, 2016, 2016, 
2016, 2016, 2016, 2016, 2016, 2016, 2017, 2017, 2017, 2017, 2017, 
2017, 2017, 2017, 2017, 2017, 2017, 2017), Month = structure(c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L), .Label = c("January", "February", "March", 
"April", "May", "June", "July", "August", "September", "October", 
"November", "December"), class = "factor"), Frequency = c(36, 
28, 39, 46, 5, 0, 0, 22, 10, 15, 8, 33, 33, 29, 31, 23, 8, 9, 
7, 40, 41, 41, 30, 30, 44, 37, 41, 42, 20, 0, 7, 27, 35, 27, 
43, 38), Days = c(31, 28, 31, 30, 6, 0, 0, 29, 15, 
29, 29, 31, 31, 29, 30, 30, 7, 0, 7, 30, 30, 31, 30, 27, 31, 
28, 30, 30, 21, 0, 7, 26, 29, 27, 29, 29)), row.names = c(NA, 
-36L), class = "data.frame")
报错原因与修复方案

核心触发原因

  • 参数传递位置错误:times=10是bag_tree()定义自助抽样次数的专属函数参数,不属于rpart引擎的可识别参数,当前代码将该参数传入set_engine()时,引擎会将其解析为NULL值,直接触发“Input must be a vector, not NULL”的报错。
  • 超参数取值不合理:设置cost_complexity=1时,rpart的复杂度惩罚系数过大,会直接返回不做任何节点分裂的根节点空模型,无法开展后续的预测和指标计算环节。
  • 控制函数配置冗余:control_resamples()中传入的自定义extract函数,在模型拟合失败时会尝试从空的模型对象中提取内容,进一步放大报错表现。

可直接落地的修复步骤

  1. 调整times参数的传递位置:将times=10从引擎参数移到bag_tree()的函数定义中,引擎部分仅保留rpart支持的合法参数。
  2. 调整复杂度惩罚参数为合理值:将cost_complexity从1调整为接近0的数值(如0.01),保证决策树可以正常生长分裂,避免生成空模型。
  3. 简化交叉验证控制参数:移除control_resamples()中自定义的extract函数定义,保留核心的save_pred = TRUE配置即可,减少不必要的环节报错。

内容的提问来源于stack exchange,提问作者Alice Hobbs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.12 13:06:08