0

我的目标是使用该tidymodels软件包安装 Poisson glmnet。为此,我使用recipes包来预处理数据、parsnip拟合模型、workflows将模型与预处理器捆绑在一起,并poissonreg能够将泊松回归与parsnip. 如果我的训练数据集只包含数字预测变量,它工作得非常好,但是当有一些因子(或分类)预测变量时,我无法拟合模型。在下面的代码中,您可能会认为 usingtidymodels是矫枉过正。是的,这是针对这个最小的示例,但最终,我会想要调整我的超参数,验证我的模型等,然后,tidymodels这将是有用的。

首先,让我们加载我们需要的包。

library(tibble)
library(recipes)
library(poissonreg)
library(parsnip)
library(workflows)
library(glmnet)

让我们还模拟我们的数据集,该数据集有 1000 行、1 个结果 ( y)、1 个具有 2 个级别 ( x_fac) 的分类预测变量和 3 个数字预测变量 (x_num_01x_num_02) x_num_03

n <- 1000

dat <- tibble::tibble(
  y = rpois(n, lambda = 0.15),
  x_fac = factor(sample(c("M", "F"), size = n, replace = T)),
  x_num_01 = rnorm(n),
  x_num_02 = rnorm(n),
  x_num_03 = rnorm(n)
)

然后,我们定义并准备配方。预处理非常简单:如果有的话,所有的分类预测器都被转换为虚拟预测器。

rec <- 
  recipes::recipe(y ~ ., data = dat) %>% 
  recipes::step_dummy(all_nominal()) %>% 
  recipes::prep()

然后我们定义我们的模型,

glmnet_mod <- 
  poissonreg::poisson_reg(penalty = 0.01, mixture = 1) %>% 
  parsnip::set_engine("glmnet")

将模型和预处理器与workflows包捆绑在一起

glmnet_wf <- 
  workflows::workflow() %>%
  workflows::add_recipe(rec) %>% 
  workflows::add_model(glmnet_mod)

最后,我们训练模型parsnip

glmnet_fit <- 
  glmnet_wf %>% 
  parsnip::fit(data = dat)

parsnip::fit函数抛出错误

Error in fishnet(x, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  NA/NaN/Inf in foreign function call (arg 4)
In addition: Warning message:
In fishnet(x, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  :
  NAs introduced by coercion
Timing stopped at: 0.005 0 0.006

我完全不知道为什么!如果您x_fac从模拟数据集中删除预测器dat,它工作正常。如果我在使用包运行 glmnet 之前自己预处理数据,它也可以工作glmnet

x <- dat %>% dplyr::mutate(x_fac_M = x_fac == "M") %>% dplyr::select(contains("x"), -x_fac) %>% as.matrix()
y <- dat$y

glmnet::glmnet(x = x, y = y, family = "poisson", lambda = 0.01, alpha = 1)

谢谢你的帮助!

会话信息:

R version 4.0.0 (2020-04-24)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.4

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] workflows_0.1.1  poissonreg_0.0.1 parsnip_0.1.0    recipes_0.1.12  
[5] dplyr_0.8.5      tibble_3.0.1    

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.4.6       pillar_1.4.4       compiler_4.0.0     gower_0.2.1       
 [5] iterators_1.0.12   class_7.3-16       tools_4.0.0        rpart_4.1-15      
 [9] ipred_0.9-9        packrat_0.5.0      lubridate_1.7.8    lifecycle_0.2.0   
[13] lattice_0.20-41    pkgconfig_2.0.3    rlang_0.4.6        foreach_1.5.0     
[17] Matrix_1.2-18      cli_2.0.2          rstudioapi_0.11    prodlim_2019.11.13
[21] withr_2.2.0        generics_0.0.2     vctrs_0.2.4        glmnet_3.0-2      
[25] grid_4.0.0         nnet_7.3-13        tidyselect_1.0.0   glue_1.4.0        
[29] R6_2.4.1           fansi_0.4.1        survival_3.1-12    lava_1.6.7        
[33] purrr_0.3.4        tidyr_1.0.2        magrittr_1.5       codetools_0.2-16  
[37] ellipsis_0.3.0     MASS_7.3-51.5      splines_4.0.0      hardhat_0.1.2     
[41] assertthat_0.2.1   shape_1.4.4        timeDate_3043.102  utf8_1.1.4        
[45] crayon_1.3.4
4

1 回答 1

1

好的,我刚刚想通了。似乎不应该准备添加到工作流中的配方。所以只需更改这部分:

rec <- 
  recipes::recipe(y ~ ., data = dat) %>% 
  recipes::step_dummy(all_nominal()) %>% 
  recipes::prep()

通过以下方式:

rec <- 
  recipes::recipe(y ~ ., data = dat) %>% 
  recipes::step_dummy(all_nominal())
于 2020-05-11T18:24:17.307 回答