0

I have installed R and Rstudio just to use glmnet for a multinomial logistic regression. I keep getting the following error:

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

whenever I plug in the dependent variable observations of my data. That comprises 120 "numeric" entries: 1 through 4. The predictors are 6=4 continuous+ 2 categorical.

I created a table "data" with 6 columns in which I changed the class of the categorical observations to "factor"

data <- cbind(x1,x1fat)  # x1=4 columns, x1fat=2 factor columns 

Then I set

x <- model.matrix(~.+0, data=data)  # 120x12 matrix

to create the matrix of regressors observations with one-hot-encoded factor data. To feed the dependent variable y observations in glmnet, I use the following line

y <- model.matrix(~.+0, data=y1)  # y1 is the 1-column table of imported observations

Now, the command

fit <- glmnet(x, y, family = "multinomial")

prompts the aforementioned error.

I do not comprehend why the following do not prompt any errors:

  1. If I randomly generate the y vector through y=sample(1:4,120,replace=true)

  2. If I include the intercept column in y: y <- model.matrix(~., data=y1)

I am completely stuck here. Any comment is appreciated, thanks.

alistaire
  • 42,459
  • 4
  • 77
  • 117
  • What is the count of examples in each category level? Error sounds like you don't have sufficient examples. – merv Apr 30 '23 at 02:45
  • Thanks for the input. They are 91 20 8 1. Very unbalanced. Why would glmnet not even return any model in unbalanced cases, instead of providing one albeit with very poor predicting performance? – mandu soju Apr 30 '23 at 11:04
  • It's not about the imbalance, but rather that the level with only 1 sample is degenerate. I don't know the internals for `glmnet`, but generally one should always have more samples than coefficients being estimated, which is violated when estimating a coefficient for only one sample. Either obtain more samples or discard that level. – merv Apr 30 '23 at 20:12

0 Answers0