3

遵循我在 Cross Validate glmnet 中的问题:多项回归中的参考类别或类是哪个?,有人能解释一下我们如何在 glmnet 中为多项逻辑回归设置参考类别吗?

尽管 glmnet 用于应用收缩方法(Ridge、Lasso 等),但它的文档和 glmnet 论坛都没有回答这个问题。

先感谢您

4

1 回答 1

3

不,你不能在函数 glmnet 中做到这一点,但你可以在使用 model.matrix 运行函数之前非常轻松地做到这一点:

a <- factor( rep(c("cat1", "cat2", "cat3", "no-cat"),50) ) #make a factor
levels(a) <- c("no-cat", "cat1", "cat2", "cat3") #change the order of the levels because 
#the first category is always the reference category using the model.matrix function
df <- data.frame(a) #put the factor in a dataframe

dummy_a <- model.matrix(~a,data=df) #make dummies for the factor. 
#Note the first category of the levels(a) will get excluded i.e. 
#become the reference category

cat_dummified <- dummy_a[,2:4] #the first column is the intercept i.e. a column of 1s
#which we exclude here

> head(cat_dummified)
  acat1 acat2 acat3
1     0     0     0
2     1     0     0
3     0     1     0
4     0     0     1
5     0     0     0
6     1     0     0

> class(cat_dummified)
[1] "matrix"

cat_dummified也是class matrix,准备在glmnet函数中使用。这样,您只有 3 个假人,您将获得系数并针对 no-cat 类别进行引用。

希望这可以帮助!

于 2014-11-29T18:58:11.593 回答