gpt4 book ai didi

R mlogit 模型,计算奇异

转载 作者:行者123 更新时间:2023-12-01 09:55:06 25 4
gpt4 key购买 nike

我今天一整天都在努力格式化我的 data (通过 BondedDust 的 table(TM) 建议发现错误后更新) 适合 mLogit:

raw <-read.csv("C:\\Users\\Andy\\Desktop\\research\\Oxford\\Prefs\\rData.csv", header=T, row.names = NULL,id="id")
raw <-na.omit(raw)

library(mlogit)

TM <- mlogit.data(raw, choice = "selected", shape = "long", alt.var = "dishId", chid.var = "individuals", drop.index = TRUE)

我失败的地方是尝试为我的数据建模。

model <- mlogit(selected ~ food + plate | sex + age +hand, data = TM)

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

我真的很想在这个话题上得到一些帮助。恐怕我会有点生气。

数据本身来自一项实验,我们让 1000 多人在两盘食物之间做出决定(我们改变食物的外观 - 角形或圆形 - 并改变盘子的形状 - 角形或圆形).

致以最良好的祝愿,安迪。

PS 恐怕我是 StackOverflow 上统计问题的新手。

最佳答案

该模型无法将您的 dishId 解释为替代索引 (alt.var),因为您有不同的 key 对用于不同的选择。例如,您将“TS”和“RS”作为 .csv 文件中第一个选择的替代索引键,但将“RR”和“RS”作为选择 3634 的键。此外,您也没有指定名称备选方案 (alt.levels)。由于未填写 alt.levelsmlogit.data 将自动尝试根据替代索引检测替代方案,它无法正确解释。这基本上就是一切出错的地方:“食物”和“盘子”变量没有被解释为替代项,而是被视为最终导致奇异性问题的个别特定变量。

您有两种选择来解决问题。您可以通过 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) # use 1 and 2 as unique alternative keys for all choices
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)

关于R mlogit 模型,计算奇异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29849640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com