mlr3中surv.penalized模型系数访问错误咨询及解法指导
问题
在使用mlr3框架开展生存分析时,已完成数据集预处理、生存任务构建、缺失值插补、数据集划分,以及带crankcompositor的surv.penalized模型训练。但尝试访问模型系数时,执行learner::coef(learner$model)报错:Error in loadNamespace(x): there is no package called 'learner';调用learner$selected_features()报错:Error in eval(expr, envir, enclos): attempt to apply non-function。需指导如何正确获取该模型的系数及选中特征。
相关代码如下:
library(haven) library(tidyverse) library(survival) library(penalized) #> Welcome to penalized. For extended examples, see vignette("penalized").
library(mlr3) library(distr6) #> #> Attaching package: 'distr6' #> The following object is masked from 'package:stats': #> #> qqplot #> The following object is masked from 'package:base': #> #> truncate
library(mlr3verse) library(mlr3proba) library(mlr3learners) library(mlr3pipelines) library(mlr3extralearners) data <- read.csv("C:/Users/Click_32235414/Documents/liver30.csv") #convert data to tibble tb <- as_tibble(data) # delete variable with a lot of missing tb <- subset(tb, select = -c(alcohol,smoking,micro_vesicular_steatosis_donor_liver_biopsy, macro_vesicular_steatosis_donor_liver_biopsy,hdl_pre,hdl_post,phos_pre, mg_pre,tg_pre,tg_post,ldh_pre,ldh_post, ldl_pre,ldl_post,gamma_gt_pre,gamma_gt_post,t_chol_pre,t_chol_post, micro_vesicular_steatosis_number_donor_liver_biopsy ,reperfusion_syndrome,fibrosis_donor_liver_biopsy, macro_vesicular_steatosis_number_donor_liver_biopsy)) tb <- subset(tb, select = -c(bun_pre,bun_post,pt_pre,pt_post)) tb<- tb[!(is.na(tb$time_to_death)), ] attach(tb) ## Task Definition tsk_s <- as_task_surv(tb, time = "time_to_death", event = "status", type = "right") # imputes values based on histogram imputer_hist = po("imputehist", affect_columns = selector_name(c("meld_peld_score", "alk_pre", "alk_post","alb_pre", "alb_post" ,"creatinine_pre","creatinine_post","na_pre","na_post" ,"direct_billi_pre","direct_billi_post","total_billi_pre","total_billi_post" ,"inr_pre","inr_post","phos_post","mg_post","ppt_pre","ptt_post","bmi" ,"DeRitis_ratio_pre","DeRitis_ratio_post","cold_time","warm_time" ,"graft_weight","max_Tacrolimus"))) # imputes values using the mod imputer_mode = po("imputemode", affect_columns = selector_name(c("biliary_anastomosis", "vasopressors","acute_kidney_disease_pm_history" ,"diabetes_mellitus_pm_history","hypertension_pm_history","infections_pm_history" ,"sbp_pm_history","cardiovascular_pm_history","previous_hospitalization_pm_history" ,"Sirolimus"))) imputer_hist$train(list(tsk_s))[[1]]$missings() #> time_to_death status #> 0 0 #> Sirolimus acute_kidney_disease_pm_history #> 19 107 #> age biliary_anastomosis #> 0 101 #> cardiovascular_pm_history diabetes_mellitus_pm_history #> 107 107 #> gender hypertension_pm_history #> 0 107 #> infections_pm_history previous_hospitalization_pm_history #> 107 475 #> sbp_pm_history vasopressors #> 107 37 #> DeRitis_ratio_post DeRitis_ratio_pre #> 0 0 #> alb_post alb_pre #> 0 0 #> alk_post alk_pre #> 0 0 #> bmi cold_time #> 0 0 #> creatinine_post creatinine_pre #> 0 0 #> direct_billi_post direct_billi_pre #> 0 0 #> graft_weight inr_post #> 0 0 #> inr_pre max_Tacrolimus #> 0 0 #> meld_peld_score mg_post #> 0 0 #> na_post na_pre #> 0 0 #> phos_post ppt_pre #> 0 0 #> ptt_post total_billi_post #> 0 0 #> total_billi_pre warm_time #> 0 0
imputer_mode$train(list(tsk_s))[[1]]$missings() #> time_to_death status #> 0 0 #> DeRitis_ratio_post DeRitis_ratio_pre #> 156 234 #> age alb_post #> 0 52 #> alb_pre alk_post #> 258 64 #> alk_pre bmi #> 155 137 #> cold_time creatinine_post #> 129 32 #> creatinine_pre direct_billi_post #> 201 67 #> direct_billi_pre gender #> 185 0 #> graft_weight inr_post #> 118 79 #> inr_pre max_Tacrolimus #> 210 481 #> meld_peld_score mg_post #> 135 136 #> na_post na_pre #> 33 206 #> phos_post ppt_pre #> 91 463 #> ptt_post total_billi_post #> 116 66 #> total_billi_pre warm_time #> 163 128 #> Sirolimus acute_kidney_disease_pm_history #> 0 0 #> biliary_anastomosis cardiovascular_pm_history #> 0 0 #> diabetes_mellitus_pm_history hypertension_pm_history #> 0 0 #> infections_pm_history previous_hospitalization_pm_history #> 0 0 #> sbp_pm_history vasopressors #> 0 0
impute_graph = imputer_hist %>% imputer_mode surv_task = impute_graph$train(tsk_s)[[1]] set.seed(42) part = partition(surv_task, ratio = 0.8) learner_penalized=lrn("surv.penalized", lambda1=25.8) learner = ppl("crankcompositor", learner = learner_penalized, response = TRUE, method = "median", overwrite = FALSE) %>% as_learner() learner$train(surv_task, part$train) #> # nonzero coefficients: 38# nonzero coefficients: 25 # nonzero coefficients: 27 # nonzero coefficients: 28 # nonzero coefficients: 27 # nonzero coefficients: 27 # nonzero coefficients: 26 # nonzero coefficients: 25 # nonzero coefficients: 25 # nonzero coefficients: 25 # nonzero coefficients: 25 # nonzero coefficients: 24 # nonzero coefficients: 24 # nonzero coefficients: 24 # nonzero coefficients: 24 # nonzero coefficients: 24 # nonzero coefficients: 25 # nonzero coefficients: 25 # nonzero coefficients: 24 # nonzero coefficients: 25 # nonzero coefficients: 25 # nonzero coefficients: 24 # nonzero coefficients: 24 # nonzero coefficients: 24 # nonzero coefficients: 24 # nonzero coefficients: 24 # nonzero coefficients: 24 # nonzero coefficients: 24
learner::coef(learner$model) #> Error in loadNamespace(x): there is no package called 'learner'
learner$selected_features() #> Error in eval(expr, envir, enclos): attempt to apply non-function
解决方法
获取模型系数
之前的错误是误用了learner::coef语法(::用于调用包的函数,而learner是对象不是包),且由于learner是被crankcompositor包装后的复合学习器,需先提取内部的原始surv.penalized学习器,再访问其模型系数:
# 提取内部的surv.penalized学习器 inner_learner <- learner$graph$pipeops$learner$learner # 获取模型系数 model_coef <- coef(inner_learner$model) print(model_coef)
获取选中特征
外层的复合学习器没有selected_features方法,需调用内部原始学习器的该方法:
# 提取内部学习器后调用selected_features selected_features <- inner_learner$selected_features() print(selected_features)
简化操作方案
你代码中已经定义了原始学习器learner_penalized,可以直接训练它并获取结果,无需通过复合学习器中转,操作更简单:
# 直接训练原始surv.penalized学习器 learner_penalized$train(surv_task, part$train) # 获取模型系数 coef(learner_penalized$model) # 获取选中特征 learner_penalized$selected_features()
内容的提问来源于stack exchange,提问作者Faiza
相关产品推荐
相关产品推荐

