【问题标题】:glmnet: how to set reference category for multinomial logitglmnet:如何为多项式 logit 设置参考类别
【发布时间】:2015-01-28 00:17:17
【问题描述】:

根据我在 Cross Validate glmnet: which is the reference category or class in multinomial regression? 中的问题,有人可以解释我们如何在 glmnet 中设置 reference category 以进行多项逻辑回归吗?

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

提前谢谢你

【问题讨论】:

    标签: r classification logistic-regression glmnet


    【解决方案1】:

    不,你不能在函数 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 类别进行引用。

    希望这会有所帮助!

    【讨论】:

    • 哇,感谢您的回复!所以这个cat_dummified是响应y参数,例如,下面的cv.glmnet命令?:cv.fit = cv.glmnet(x = vars_matrix_data, y = cat_dummified, family = "multinomial", nfolds = 10, alpha = 1)或者它是x矩阵的一部分,因为你说它会有3个系数?
    • 它是 x 矩阵的一部分,因为它有 3 个系数,因为您将它用作预测变量。但这是为了了解如何构建制作参考变量的假人。随意使用!
    • 那么,这有帮助吗?您想了解更多信息吗?
    • 感谢您保持联系。它不起作用......我认为这是由于我的x 矩阵(4694 obs x 1815 vars)的性质,它是一个非常稀疏的矩阵,带有(独立)二进制变量 {0,1}。每个变量至少有 5 个“1”。 GLMNET 以非零系数仅在虚拟变量上结束得太早。我正在编辑我的帖子,以便您查看矩阵。我已经为x 使用了Matrix 包的稀疏矩阵类型来节省资源。
    • 我不明白这里的问题。参考类别是您留在外面的类别。在本例中为 dummy_no_cat。所有其他假人(dummy_cat1、dummy_cat2、dummy_cat3)都被排除在外。这会自动发生,因为只要这 3 为 0,dummy_no_cat 就会隐含 1。您无需明确告诉 glmnet 哪一个是留在外面的那个。除非我错过了什么。在我看来,您上面的转换矩阵已经可以使用(至少根据前 3 列)
    猜你喜欢
    • 2020-03-30
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    相关资源
    最近更新 更多