为什么在glmnet
函数中从glmnet
函数中只有1个解释变量可以传递给glm
函数中的模型时,为什么不可能?代码和错误如下:
> modelX<-glm( ifelse(train$cliks <1,0,1)~(sparseYY[,40]), family="binomial")
> summary(modelX)
Call:
glm(formula = ifelse(train$cliks < 1, 0, 1) ~ (sparseYY[, 40]),
family = "binomial")
Deviance Residuals:
Min 1Q Median 3Q Max
-0.2076 -0.2076 -0.2076 -0.2076 2.8641
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -3.82627 0.00823 -464.896 <2e-16 ***
sparseYY[, 40] -0.25844 0.15962 -1.619 0.105
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 146326 on 709677 degrees of freedom
Residual deviance: 146323 on 709676 degrees of freedom
AIC: 146327
Number of Fisher Scoring iterations: 6
> modelY<-glmnet( y =ifelse(train$cliks <1,0,1), x =(sparseYY[,40]), family="binomial" )
Błąd wif (is.null(np) | (np[2] <= 1)) stop("x should be a matrix with 2 or more columns")
我不知道为什么,但这是某种内部限制。它与罗马人声称的家庭没有关系。
glmnet(x = as.matrix(iris[2:4]), y = as.matrix(iris[1]))
## long output
glmnet(x = as.matrix(iris[1]), y = as.matrix(iris[1]))
Error in glmnet(x = as.matrix(iris[2]), y = as.matrix(iris[1])) :
x should be a matrix with 2 or more columns
这是一个简单的检查代码https://github.com/cran/glmnet/blob/master/R/glmnet.R#L20
因为文档说明了这一点。
对于family =“二项式”应该是具有两个级别的因子,或者是计数或比例的两列矩阵(第二列被视为目标类;对于因子,按字母顺序的最后一级是目标类)。
你有两个选择。构造一个矩阵,其中两列代表计数,或者将x
转换为具有两个级别的因子。