r - gmnl错误'没有“choiceid”变量的索引'
问题描述
我已经能够制作一个 mlogit.data 对象以输入到 gmnl 包中以进行潜在类分析,但我一直遇到它说choiced
变量没有索引的问题。我看到有关于同一问题的另一个问题,但该问题没有答案。我已经包含了测试选择数据的样本。感谢您对此事的任何帮助。
personid choiceid alt payment management assessment crop
1 1 1 3 2 2 3
1 2 2 2 2 1 3
1 3 1 3 2 1 3
1 4 1 2 1 3 1
1 5 1 2 1 3 1
1 6 2 1 1 2 1
1 7 2 3 1 2 3
1 8 2 3 1 2 3
1 9 2 3 1 1 2
1 10 2 3 1 1 2
1 11 2 3 1 2 1
1 12 2 2 1 1 3
1 13 3 1 2 1 1
1 14 2 1 1 2 3
1 15 2 2 1 2 2
1 16 2 1 1 1 3
2 17 3 1 2 1 2
2 18 3 1 3 1 2
2 19 1 3 1 1 3
library("gmnl")
library("mlogit")
library("readr")
library("Formula")
testchoices <- testchoices <- read_csv("~/Documents/year 3/DISS/MODELS/choice/testchoices.csv")
#>
#> ── Column specification ────────────────────────────────────────────────────────
#> cols(
#> personid = col_double(),
#> choiceid = col_double(),
#> alt = col_double(),
#> payment = col_double(),
#> management = col_double(),
#> assessment = col_double(),
#> crop = col_double()
#> )
test <- as.data.frame(testchoices)
test$choiceid <- 1:nrow(test)
head(test, 19)
#> personid choiceid alt payment management assessment crop
#> 1 1 1 1 3 2 2 3
#> 2 1 2 2 2 2 1 3
#> 3 1 3 1 3 2 1 3
#> 4 1 4 1 2 1 3 1
#> 5 1 5 1 2 1 3 1
#> 6 1 6 2 1 1 2 1
#> 7 1 7 2 3 1 2 3
#> 8 1 8 2 3 1 2 3
#> 9 1 9 2 3 1 1 2
#> 10 1 10 2 3 1 1 2
nrow(test)
#> [1] 19
MC <- dfidx::dfidx(test, alt.levels = c("payment", "management", "assessment", "crop"),
idx = list(c("choiceid", "personid"), "alt"), drop.index = FALSE, package = "mlogit")
print(MC)
#> ~~~~~~~
#> first 10 observations out of 19
#> ~~~~~~~
#> personid choiceid alt payment management assessment crop idx
#> 1 1 1 1 3 2 2 3 1:1
#> 2 1 2 2 2 2 1 3 2:2
#> 3 1 3 1 3 2 1 3 3:1
#> 4 1 4 1 2 1 3 1 4:1
#> 5 1 5 1 2 1 3 1 5:1
#> 6 1 6 2 1 1 2 1 6:2
#> 7 1 7 2 3 1 2 3 7:2
#> 8 1 8 2 3 1 2 3 8:2
#> 9 1 9 2 3 1 1 2 9:2
#> 10 1 10 2 3 1 1 2 10:2
#>
#> ~~~ indexes ~~~~
#> choiceid personid alt
#> 1 1 1 1
#> 2 2 1 2
#> 3 3 1 1
#> 4 4 1 1
#> 5 5 1 1
#> 6 6 1 2
#> 7 7 1 2
#> 8 8 1 2
#> 9 9 1 2
#> 10 10 1 2
#> indexes: 1, 1, 2
final4 <- mlogit.data(MC, choice = "choiceid", shape = "wide", drop.index = TRUE)
head(final4)
#> ~~~~~~~
#> first 10 observations out of 361
#> ~~~~~~~
#> personid choiceid payment management assessment crop idx.choiceid
#> 1 1 TRUE 3 2 2 3 1
#> 2 1 FALSE 3 2 2 3 1
#> 3 1 FALSE 3 2 2 3 1
#> 4 1 FALSE 3 2 2 3 1
#> 5 1 FALSE 3 2 2 3 1
#> 6 1 FALSE 3 2 2 3 1
#> 7 1 FALSE 3 2 2 3 1
#> 8 1 FALSE 3 2 2 3 1
#> 9 1 FALSE 3 2 2 3 1
#> 10 1 FALSE 3 2 2 3 1
#> idx.personid idx.alt idx.choiceid.1 idx.personid.1 idx.alt.1 idx.choiceid.2
#> 1 1 1 1 1 1 1
#> 2 1 1 1 1 1 1
#> 3 1 1 1 1 1 1
#> 4 1 1 1 1 1 1
#> 5 1 1 1 1 1 1
#> 6 1 1 1 1 1 1
#> 7 1 1 1 1 1 1
#> 8 1 1 1 1 1 1
#> 9 1 1 1 1 1 1
#> 10 1 1 1 1 1 1
#> idx.personid.2 idx.alt.2 idx.choiceid.3 idx.personid.3 idx.alt.3
#> 1 1 1 1 1 1
#> 2 1 1 1 1 1
#> 3 1 1 1 1 1
#> 4 1 1 1 1 1
#> 5 1 1 1 1 1
#> 6 1 1 1 1 1
#> 7 1 1 1 1 1
#> 8 1 1 1 1 1
#> 9 1 1 1 1 1
#> 10 1 1 1 1 1
#> idx.choiceid.4 idx.personid.4 idx.alt.4 idx.choiceid.5 idx.personid.5
#> 1 1 1 1 1 1
#> 2 1 1 1 1 1
#> 3 1 1 1 1 1
#> 4 1 1 1 1 1
#> 5 1 1 1 1 1
#> 6 1 1 1 1 1
#> 7 1 1 1 1 1
#> 8 1 1 1 1 1
#> 9 1 1 1 1 1
#> 10 1 1 1 1 1
#>
#> ~~~ indexes ~~~~
#> chid alt
#> 1 1 1
#> 2 1 10
#> 3 1 11
#> 4 1 12
#> 5 1 13
#> 6 1 14
#> 7 1 15
#> 8 1 16
#> 9 1 17
#> 10 1 18
#> indexes: 1, 2
我不知道为什么这里有这么多变量的倍数idx.alt
,idx.personid
和idx.choiceid
。这是在该dfidx
行中指定的,但没有显示在那里。
View(final4)
lc <- gmnl(choiceid ~ payment | management + assessment + crop,
data = final4,
model = 'lc',
Q = 4,
panel = TRUE,
method = "bhhh")
#> Error in gmnl(choiceid ~ payment | management + assessment + crop, data = final4, : No individual index
非常感谢您查看此内容
解决方案
推荐阅读
- zsh - 查找与 zsh 关联数组中的值匹配的键?
- mongodb - azure cosmos db 在 mongodb.com 中创建了集合
- asp.net-core - 如何将 AzureAD 配置添加到 AspNetCore 应用程序
- excel - 通过 Excel VBA 将 Xlookup 添加到范围
- c# - c# Console App Giving Error: Index was outside the bounds of the array - 在成功发布后单击 Setup.exe 时
- php - 服务器升级后PHPExcel出错?
- php - 如何使存储在数据库中的自定义字段文本成为wordpress前端的链接
- python - 使用 OpenCV Python 使用模糊水平集进行视网膜流体分割
- java - 在mysql中,varchar类型用于存储Date类型数据,加分和0出现在末尾
- docker - 为什么我们必须使用带有 gunicorn 的 nginx 在容器内部署烧瓶