按照我在交叉验证中的问题 glmnet: which is the reference category or class in multinomial regression? ,有人可以解释一下我们如何在 glmnet 中为多项逻辑回归设置引用类别吗?
尽管 glmnet 用于应用收缩方法(Ridge、Lasso 等),但它的文档和 glmnet 论坛都没有回答这个问题。
提前致谢
最佳答案
不,你不能在函数 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
也是类矩阵
,准备在glmnet
函数中使用。
这样,您只有 3 个假人,您将拥有这些假人的系数,并根据无猫类别进行引用。
希望这对您有所帮助!
关于r - glmnet:如何为多项式 logit 设置引用类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27203888/