使用h2o.coxph构建Cox比例风险模型时遇特征数量报错求助
解决H2O CoxPH模型报错:"Training data must have at least 2 features (incl. response)"
针对你遇到的问题,可尝试以下几种解决方案:
- 修正x参数的传入格式:H2O的
h2o.coxph函数中,x参数需要接收字符向量类型的预测变量列表。即使只有一个预测变量,也需用向量形式传入,而非单个字符串。修改代码如下:
modelo_coxph <- h2o.coxph(x = c("edad"), training_frame = datos_h2o, event_column = "event", start_column= "inicio", stop_column = "final" )
验证数据集列的有效性:使用
h2o.describe(datos_h2o)查看数据集的列信息,确认:edad、inicio、final、event四列均存在且无缺失值edad为数值型(连续变量),event为0/1数值型或布尔型,inicio和final为数值型时间列
排除缺失值影响:如果
edad列存在大量缺失,H2O会自动剔除该列,导致无可用预测特征。可通过h2o.na_omit(datos_h2o)移除含缺失值的行,再重新训练模型:
datos_h2o_clean <- h2o.na_omit(datos_h2o) modelo_coxph <- h2o.coxph(x = c("edad"), training_frame = datos_h2o_clean, event_column = "event", start_column= "inicio", stop_column = "final" )
- 显式指定列类型:强制转换相关列的类型,确保符合模型要求:
# 将event列转为因子型 datos_h2o$event <- h2o.asfactor(datos_h2o$event) # 将start和stop列转为数值型 datos_h2o$inicio <- h2o.asnumeric(datos_h2o$inicio) datos_h2o$final <- h2o.asnumeric(datos_h2o$final) # 重新训练模型 modelo_coxph <- h2o.coxph(x = c("edad"), training_frame = datos_h2o, event_column = "event", start_column= "inicio", stop_column = "final" )
内容的提问来源于stack exchange,提问作者ilpadrino
相关产品推荐
相关产品推荐

