首页 > 解决方案 > 在 R mlogit 包中使用字符时出现计算奇点错误

问题描述

有一些相关的问题,但我真的无法理解它。我是统计、R、mlogit 包以及 stockoverflow 的新手。我会尽量准确地提出我的问题。这是[数据的链接]。(https://docs.google.com/spreadsheets/d/1IvN6ZgCgDERu3Mn4AglZMjicoXnFQQHc9GhAhbrpFRI/edit?usp=sharing)我有一个来自离散选择实验的数据集,带有一个因变量“选择”有两个水平(是/否)和 4 个自变量,每 3 个水平。

我尝试使用 mlogit 进行估算,但我遇到了一些实际问题,我的主管无法提供帮助。在我的数据集中,每个变量的值是 1,2,3,(1 代表品牌 1,2 代表品牌 2,等等...)

    t1 <- read_csv("~/Dokumente/UvA/Thesis/R/t1.csv")
t1 <- mlogit.data(data=t1, choice="choice",shape="long",alt.levels=paste("pos",1:4),id.var="id")

要运行估计,我使用以下函数:

m1 <- mlogit(choice~ 0 + Brand+ Features+ Valence+ Volume, data=t1)
summary(m1)

并得到了这个结果:模型 1 估计 并注意到 Rstudio 将我的数据集变量解释为整数。由于变量是 3 个不同的品牌、3 个不同的特征和 3 个不同类别的 valenve 和 volume(低、中和高),我想包括对水平的估计。因此,我厌倦了将它们上传到 Rstudio 并使用此功能将它们指定为字符

library(readr)
t1 <- read_csv("~/Dokumente/UvA/Thesis/R/t1.csv", 
col_types = cols(Brand = col_character(), 
    Features = col_character(), Valence = col_character(), 
    Volume = col_character()))

如果我现在运行相同的 mlogit 函数,我会得到一个错误:

Error in solve.default(H, g[!fixed]) : system is computationally singular: reciprocal condition number = 3.11303e-18

当我使用不同级别的字符(例如品牌名称而不是 1、2、3 参见数据表 2“t2”)时,我遇到了同样的奇点问题。a) 如果我使用第一个数据集中的数字,结果是否有意义?b)如何将我的值整合为字符来估计属性级别?

我希望有人可以帮助我,因为我对这一切感到非常困惑和陌生。我肯定犯了一个非常基本或愚蠢的错误。

干杯

标签: rmlogit

解决方案


有几个问题。第一个问题是您有一个标记为“10”的选择值,但您说它应该只有两个级别。

library(readxl)
library(dplyr)

t1 <- read_excel("~/Downloads/Data mlogit.xlsx", sheet=1) %>% as.data.frame
t1$choice %>% table

   0    1   10 
2770  925    1 

假设它只是被误标了,您也不应该运行多项式 logit,这仅适用于您有两个以上级别的情况。相反,您应该运行标准物流或类似的物流。例子:

# Correct mislabeled sample
t1$choice[t1$choice == 10] <- 1

# Make everything factors
for(i in 1:ncol(t1)) {
  t1[[i]] <- factor(t1[[i]])
}

# Run logistic
library(glmnet)

y <- t1$choice
t1d <- dplyr::select(t1, Brand, Features, Valence, Volume)
t1d <- model.matrix( ~ .-1, t1d)
fit <- glmnet(t1d,y, family="binomial", intercept=F, lambda = 0, alpha=0)
coefficients(fit)

(Intercept)  .        
Brand0      -2.0328103
Brand1      -0.4518273
Brand2      -1.4383109
Brand3      -1.4903840
Features1   -0.5857877
Features2    0.2900501
Features3    0.2717443
Valence1     1.4788752
Valence2    -0.1585652
Valence3    -1.9390001
Volume1     -0.6920187
Volume2     -0.1013821
Volume3      0.7010679

有很多方法可以在 R 中运行逻辑回归,我倾向于使用glmnet包。


推荐阅读