0

I'm trying to fit a poisson model using glmnet, but I can't seem to get the data in the right form right. All the variables are categorical. The is an earlier question dealing with pretty much the same problem, but the solutions given didn't help me- as.matrix() and model.matrix just threw out more error messages.

For as.matrix here's what I got:

x<-as.matrix(ld2)

> res<-glmnet(x,y,family = "poisson")
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

Any ideas?

LyzandeR
  • 37,047
  • 12
  • 77
  • 87
JenSCDC
  • 101
  • 3
  • 1
    what is Id2? A reproducible example would be helpful – Rusan Kax Nov 30 '14 at 13:28
  • 1
    can you show the results/errors based on trying `x <- model.matrix(~.,data=ld2)` ? – Ben Bolker Nov 30 '14 at 13:33
  • The data frame with the independent variables which I'm trying to get into matrix form for input to glmnet. By reproducible, do you mean something that's small enough to be posted here? That's no problem. – JenSCDC Nov 30 '14 at 13:34
  • Ben, that seemed to have worked, thanks! Could you tell me what "~." does? This is the first time that I've had to deal with matrices, so obviously I'm pretty curious. – JenSCDC Nov 30 '14 at 13:45

1 Answers1

0

You should try

x <- model.matrix(~.,data=ld2)

model.matrix() is the R function for translating a data frame (containing numeric and categorical predictors) into a numerical model matrix; categorical predictors get turned into sets of binary dummy predictor variables. The formula ~. and specifically the dot, denotes "use all of the variables in the data frame as main effects" (this is a one-sided formula -- there is no need to specify a response variable on the left-hand side of the ~).

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453