大矩阵可在cv.glmnet()中运行以进行多项分类

时间:2018-10-22 22:43:31

标签: r sparse-matrix logistic-regression glmnet multinomial

我正在处理一个大型矩阵,其中的样本数为N = 40,特征为P = 7130。我正在尝试将cv.glmnet()用于山脊,但这样做时遇到错误。
数据集的维度为(40,7130)
cv.glmnet()的代码如下:

ridge2_cv <- cv.glmnet(x, y,
                   ## type.measure: loss to use for cross-validation.
                   type.measure = "deviance",
                   ## K = 10 is the default.
                   nfold = 10,
                   ## Multinomial regression
                   family = "multinomial",
                   ## ‘alpha = 1’ is the lasso penalty, and ‘alpha = 0’ the ridge penalty.
                   alpha = 0)

这里x是具有285160个元素的大矩阵。 y是大小为40的多类响应变量
当我运行上述功能时,我一直收到此错误。

Error in cbind2(1, newx) %*% (nbeta[[i]]) : invalid class 'NA' to dup_mMatrix_as_dgeMatrix In addition: Warning messages: 1: In lognet(x, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one multinomial or binomial class has fewer than 8 observations; dangerous ground 2: In lognet(x, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one multinomial or binomial class has fewer than 8 observations; dangerous ground

1 个答案:

答案 0 :(得分:0)

可以将data.matrix()用作矩阵而不是as.matrix吗?我记得尝试过类似的方法。

ridge2_cv <- cv.glmnet(data.matrix(x), y,
               ## type.measure: loss to use for cross-validation.
               type.measure = "deviance",
               ## K = 10 is the default.
               nfold = 10,
               ## Multinomial regression
               family = "multinomial",
               ## ‘alpha = 1’ is the lasso penalty, and ‘alpha = 0’ the ridge penalty.
               alpha = 0)