r - glmnet:如何为多项式 logit 设置引用类别

标签 r classification logistic-regression glmnet

按照我在交叉验证中的问题 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/

相关文章:

r - 查找 R 中发生错误的位置

r - 是否可以在 dplyr 中进行完全连接并保留连接中使用的所有列?

r - 如何在 dplyr 中 select_if,其中逻辑条件被否定

machine-learning - 哪种分类器可以有效地处理不属于经过训练的类的测试查询?

matlab - 逻辑回归成本函数

r - 你能在 R 中执行内核逻辑回归吗

matlab - Matlab 中的逻辑回归梯度下降

R : How can I get all combinations from a bigger set to a smaller set?

python - 从 sklearn 的 MLPClassifier 中检索最终的隐藏激活层输出

python - 随机森林不进行分类