由于您在不同的选择中使用了不同的键值对,模型无法将您的dishId解释为备选索引(alt.var
)。例如,在您的.csv文件中,第一个选择的替代索引键为“TS”和“RS”,但是在3634个选择中,您的键为“RR”和“RS”。此外,您还没有指定备选项的名称(alt.levels
)。由于未填写alt.levels
,mlogit.data
将自动尝试基于备选索引检测备选项,它无法正确解释。这基本上是一切出错的地方:'food'和'plate'变量不被解释为备选项,而被视为最终导致奇异性问题的个体特定变量。
您有两个选项来解决此问题。您可以通过alt.levels
参数将实际备选项作为输入提供给mlogit.data
:
TM <- mlogit.data(raw, choice = "selected", shape = "long", alt.levels = c("food","plate"),chid.var = "individuals",drop.index=TRUE)
model1 <- mlogit(selected ~ food + plate | sex + age +hand, data = TM)
或者,您可以选择使索引键一致,以便可以通过alt.var
将其作为输入。现在mlogit.data
能够正确地猜测您的替代品:
raw[,3] <- rep(1:2,nrow(raw)/2)
TM <- mlogit.data(raw, choice = "selected", shape = "long", alt.var="dishId", chid.var = "individuals")
model2 <- model <- mlogit(selected ~ food + plate | sex + age +hand, data = TM)
我们验证两个模型确实是相同的。第一个模型的结果为:
> summary(model1)
Call:
mlogit(formula = selected ~ food + plate | sex + age + hand,
data = TM, method = "nr", print.level = 0)
Frequencies of alternatives:
food plate
0.42847 0.57153
nr method
4 iterations, 0h:0m:0s
g'(-H)^-1g = 0.00423
successive function values within tolerance limits
Coefficients :
Estimate Std. Error t-value Pr(>|t|)
plate:(intercept) -0.0969627 0.0764117 -1.2689 0.2044589
foodCirc 1.0374881 0.0339559 30.5540 < 2.2e-16 ***
plateCirc -0.0064866 0.0524547 -0.1237 0.9015835
plate:sexmale -0.0811157 0.0416113 -1.9494 0.0512512 .
plate:age16-34 0.1622542 0.0469167 3.4583 0.0005435 ***
plate:age35-54 0.0312484 0.0555634 0.5624 0.5738492
plate:age55-74 0.0556696 0.0836248 0.6657 0.5055987
plate:age75+ 0.1057646 0.2453797 0.4310 0.6664508
plate:handright -0.0177260 0.0539510 -0.3286 0.7424902
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Log-Likelihood: -8284.6
McFadden R^2: 0.097398
Likelihood ratio test : chisq = 1787.9 (p.value = < 2.22e-16)
与模型2的结果相比较,可以看出备选方案被正确识别,但是其名称并未明确添加到模型中。
> summary(model2)
Call:
mlogit(formula = selected ~ food + plate | sex + age + hand,
data = TM, method = "nr", print.level = 0)
Frequencies of alternatives:
1 2
0.42847 0.57153
nr method
4 iterations, 0h:0m:0s
g'(-H)^-1g = 0.00423
successive function values within tolerance limits
Coefficients :
Estimate Std. Error t-value Pr(>|t|)
2:(intercept) -0.0969627 0.0764117 -1.2689 0.2044589
foodCirc 1.0374881 0.0339559 30.5540 < 2.2e-16 ***
plateCirc -0.0064866 0.0524547 -0.1237 0.9015835
2:sexmale -0.0811157 0.0416113 -1.9494 0.0512512 .
2:age16-34 0.1622542 0.0469167 3.4583 0.0005435 ***
2:age35-54 0.0312484 0.0555634 0.5624 0.5738492
2:age55-74 0.0556696 0.0836248 0.6657 0.5055987
2:age75+ 0.1057646 0.2453797 0.4310 0.6664508
2:handright -0.0177260 0.0539510 -0.3286 0.7424902
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Log-Likelihood: -8284.6
McFadden R^2: 0.097398
Likelihood ratio test : chisq = 1787.9 (p.value = < 2.22e-16)
dishId
还是selected
? - Randy Laisolve()
中,使用一个更小的公差,例如solve(..., tol = 1e-20)
。这应该没问题,因为您得到的“倒数条件数 = 1.71139e-19”。更多信息请参见帮助文件和这个相关问题。 - Konstantinos