Error in running glinternet : a statistical function for automatic model selection using interaction terms by Stanford's professor T. Hastie

Viewed 517

The glinternet is an R package and a function that implements an algorithm developed by Trevor Hastie -- the eminent Stanford professor on Statistical Learning -- and his ex-phD student. glinternet() detects automatically interaction terms and as such it is very useful in building a model in a situation with many variables where the possible combinations are enormous.

When I run glinternet I get an error message which I reproduce here using the mtcars base R dataset:

data(mtcars)
setDT(mtcars)
glimpse(mtcars)
x = as.matrix(mtcars[, -c("am"), with = FALSE])
class(x)
y <- mtcars$am
class(y)

glinter_fit <- glinternet(x , y, numLevels = 2)
Error: pCat + pCont == ncol(X) is not TRUE

Your advice will be appreciated.

1 Answers

It's not very clear, but you need to provide a vector that is as long as your number of predictor columns, each element indicating the number of categories for each column.

In your example, in x it's all continuous, so we do:

glinternet(x,y,numLevels=rep(1,ncol(x)))

Call: glinternet(X = x, Y = y, numLevels = rep(1, ncol(x)))
     lambda objValue cat cont catcat contcont catcont
1  0.068900   0.1210   0    0      0        0       0
2  0.062800   0.1200   0    1      0        0       0
3  0.057100   0.1180   0    1      0        0       0
4  0.052000   0.1160   0    1      0        0       0
5  0.047300   0.1130   0    2      0        0       0
6  0.043100   0.1100   0    2      0        0       0
7  0.039200   0.1060   0    3      0        0       0
8  0.035700   0.1020   0    3      0        0       0
9  0.032500   0.0983   0    3      0        0       0
10 0.029600   0.0944   0    3      0        0       0
11 0.026900   0.0904   0    3      0        0       0
12 0.024500   0.0866   0    3      0        0       0
13 0.022300   0.0829   0    3      0        0       0
14 0.020300   0.0794   0    3      0        0       0
15 0.018500   0.0760   0    3      0        0       0
16 0.016800   0.0728   0    3      0        1       0
17 0.015300   0.0698   0    4      0        1       0
18 0.014000   0.0668   0    4      0        1       0
19 0.012700   0.0638   0    4      0        2       0
20 0.011600   0.0608   0    4      0        2       0
21 0.010500   0.0579   0    3      0        2       0
22 0.009580   0.0551   0    3      0        2       0
23 0.008720   0.0523   0    3      0        2       0
24 0.007940   0.0497   0    3      0        2       0
25 0.007230   0.0472   0    3      0        3       0
26 0.006580   0.0448   0    5      0        3       0
27 0.005990   0.0425   0    5      0        3       0
28 0.005450   0.0403   0    5      0        3       0
29 0.004960   0.0382   0    5      0        3       0
30 0.004520   0.0361   0    4      0        3       0
31 0.004110   0.0342   0    4      0        3       0
32 0.003740   0.0324   0    4      0        4       0
33 0.003410   0.0307   0    4      0        5       0
34 0.003100   0.0291   0    4      0        6       0
35 0.002820   0.0275   0    3      0        6       0
36 0.002570   0.0261   0    3      0        6       0
37 0.002340   0.0247   0    3      0        8       0
38 0.002130   0.0234   0    3      0        7       0
39 0.001940   0.0221   0    3      0        7       0
40 0.001760   0.0210   0    3      0        7       0
41 0.001610   0.0199   0    3      0        8       0
42 0.001460   0.0188   0    3      0        8       0
43 0.001330   0.0178   0    4      0       10       0
44 0.001210   0.0168   0    4      0       10       0
45 0.001100   0.0159   0    4      0       12       0
46 0.001000   0.0149   0    4      0       12       0
47 0.000914   0.0140   0    4      0       12       0
48 0.000832   0.0132   0    4      0       12       0
49 0.000757   0.0123   0    3      0       13       0
50 0.000689   0.0115   0    2      0       13       0
Related