我尝试在R中使用mlogit包,以便在面板数据集上选择模型。为此,我已经成功地在mlogit.data表单中指定了数据。Y变量由表示选择的TRUE / FALSE组成,service变量由所有选择选项组成,id变量由做出选择的所有个人组成。
data <- mlogit.data(df, choice = "y",
shape = "long",
alt.var = "service",
id.var = "id")
考虑到我得到以下错误:
Error in str2lang(x) : <text>:2:0: unexpected end of input
1: . ~ . |
^
我假设我错误地指定了我的公式。我尝试用以下两种方式来指定它,但是这两种方式都会导致相同的错误。
f1 <- mFormula(y ~ x1 + x2) # does not throw error
head(model.matrix(f1, data), 2) # throws the error
model1 <- mlogit(f1, data = data,
rpar = c(x1 = "n",
x2 = "n"),
panel = TRUE) # throws the error
和
f2 <- y ~ x1 + x2 # does not throw the error
head(model.matrix(f2, data), 2) # does not throw error
model2 <- mlogit(f2, data = data,
rpar = c(x1 = "n",
x2 = "n"),
panel = TRUE) # throws the error
我在公式中使用的变量是特定于备选方案的,这意味着对于周期为t的服务s,变量具有相同的值。
有人知道是什么导致了这个错误吗?提前感谢!
发布于 2021-07-15 18:35:24
我也遇到了同样的错误。首先,不推荐使用mlogit.data函数,因此需要使用dfidx来设置数据格式。其次,当我使用dfidx设置我的数据时,我离开了一个data.table对象,然后当我尝试拟合模型时,我得到了你所询问的错误。
我解决这个问题的方法是使用data.frame对象准备dfidx数据。
https://stackoverflow.com/questions/67984904
复制相似问题