本文介绍了Logm回归/二项式的glmnet错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试为family ="binomial"拟合glmnet()时,为Logistic回归拟合而出现此错误:

I get this error when trying to fit glmnet() with family="binomial", for Logistic Regression fit:

> data <- read.csv("DAFMM_HE16_matrix.csv", header=F)
> x <- as.data.frame(data[,1:3])
> x <- model.matrix(~.,data=x)
> y <- data[,4]

> train=sample(1:dim(x)[1],287,replace=FALSE)

> xTrain=x[train,]
> xTest=x[-train,]
> yTrain=y[train]
> yTest=y[-train]

> fit = glmnet(xTrain,yTrain,family="binomial")

Error in lognet(x, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  :
one multinomial or binomial class has 1 or 0 observations; not allowed

任何帮助将不胜感激-我已经搜索了互联网,却找不到任何有帮助的东西

Any help would be greatly appreciated - I've searched the internet and haven't been able to find anything that helps

数据如下:

> data
          V1       V2    V3      V4
1   34927.00   156.60 20321  -12.60
2   34800.00   156.60 19811  -18.68
3   29255.00   156.60 19068    7.50
4   25787.00   156.60 19608    6.16
5   27809.00   156.60 24863   -0.87
...
356 26495.00 12973.43 11802    6.35
357 26595.00 12973.43 11802   14.28
358 26574.00 12973.43 11802    3.98
359 25343.00 14116.18 11802   -2.05

推荐答案

我认为这是由于因子变量的级别所致.假设有10个级别,而您的1个级别只有一个记录,请尝试删除此级别.您可以使用gdata包中的下降级别.

I think it is because of the levels of your factor variable. Suppose there are 10 levels and your 1 level has only one record, try to remove this level. You can use drop levels from gdata package.

这篇关于Logm回归/二项式的glmnet错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:28