R中运行混合logit模型时的公式错误

Formula error when running mixed logit model in R

我已经为混合逻辑模型创建了模拟数据。它涉及 N 个垂钓者进行两次旅行,每次都可以到达两个地点之一。这是创建模拟数据的代码:

library(data.table)
library(mlogit)

### Angler choice probabilities for site a
N <- 1e4
beta <- rnorm(N, mean = 0.5)
val <- runif(N)
p <- exp(1 + beta*val)/(1+ exp(1 + beta*val))

### Choice for trip 1
Y11 <- rbinom(N, 1, p)
Y10 <- 1 - Y11

### Choice for trip 2
Y21 <- rbinom(N, 1, p)
Y20 <- 1 - Y21

### Data set
id <- as.character(1:N)
sim <- data.table(angler = rep(id, 4), 
                  trip = paste(id, rep(1:2, each = 2*N), sep = "_"),
                  site = c(rep(letters[1:2], each = N),
                           rep(letters[1:2], each = N)),
                  is_choice = as.logical(c(Y11, Y10, Y21, Y20)),
                  term = c(val, rep(0, N), val, rep(0, N)))
sim[, term2 := ifelse(site == "a", 1, 0)]

当我运行 mlogit 时,出现以下错误:

> mlogit(is_choice ~ 1, sim, shape = "long", alt.var = "site", chid.var = "trip", id.var = "angler")
Error in str2lang(x) : <text>:2:0: unexpected end of input
1: . ~   . |  
  ^

我可以 运行 来自小插图的示例,所以似乎没有安装问题。

我犯了什么错误?谢谢!

这个问题可以用data.frame解决,不用data.table

sim2 <- as.data.frame(sim)
mlogit(is_choice ~ 1, sim2, shape = "long", alt.var = "site", chid.var = "trip", id.var = "angler")

Call:
mlogit(formula = is_choice ~ 1, data = sim2, shape = "long",     alt.var = "site", chid.var = "trip", id.var = "angler", method = "nr")

Coefficients:
(Intercept):b  
      -1.1595