我的经历mlogit
是它对不符合的数据不是很宽容exactly它应该是这样的。
在你的例子中,我注意到第一个受访者有 6 个选择,而第二个受访者有 7 个选择。如果您将数据格式化为每个受访者都有相同数量的替代方案mlogit.data
功能工作原理:
dat <- read.table(sep=",",text="
key,altkey,A,B,C,D
201005131,1, 2.6,118.17,117,0
201005131,2,1.4,117.11,115,0
201005131,3,1.1,117.38,122,1
201005131,4,24.6,,122,0
201005131,5,48.6,91.90,122,0
201005131,6,59.8,,122,0
201005132,1,20.2,118.23,113,0
201005132,2,2.5,123.67,120,1
201005132,3,7.4,116.30,120,0
201005132,4,2.8,118.86,120,0
201005132,5,6.9,124.72,120,0
201005132,6,2.5,123.81,120,0
201005132,7,8.5,119.23,115,0
", header=TRUE)
Running mlogit
所有数据上都会重现错误:
> mlogit.data(dat, choice="D", shape="long", id.var="key", alt.var="altkey")
Error in `row.names<-.data.frame`(`*tmp*`, value = c("1.1", "1.2", "1.3", :
duplicate 'row.names' are not allowed
In addition: Warning message:
non-unique values when setting 'row.names': '1.1', '1.2', '1.3', '1.4', '1.5', '1.6'
但是,删除第 13 行(即第七个替代方案)是有效的:
> mlogit.data(dat[-13, ], choice="D", shape="long", id.var="key", alt.var="altkey")
key altkey A B C D
1.1 201005131 1 2.6 118.17 117 FALSE
1.2 201005131 2 1.4 117.11 115 FALSE
1.3 201005131 3 1.1 117.38 122 TRUE
1.4 201005131 4 24.6 NA 122 FALSE
1.5 201005131 5 48.6 91.90 122 FALSE
1.6 201005131 6 59.8 NA 122 FALSE
2.1 201005132 1 20.2 118.23 113 FALSE
2.2 201005132 2 2.5 123.67 120 TRUE
2.3 201005132 3 7.4 116.30 120 FALSE
2.4 201005132 4 2.8 118.86 120 FALSE
2.5 201005132 5 6.9 124.72 120 FALSE
2.6 201005132 6 2.5 123.81 120 FALSE
当然,这不是很令人满意,因为它破坏了一些数据。更好的解决方案是以以下格式构建数据:mlogit()
期望,然后调用mlogit()
直接地:
dat$key <- factor(as.numeric(as.factor(dat$key)))
dat$altkey <- as.factor(dat$altkey)
dat$D <- as.logical(dat$D)
row.names(dat) <- paste(dat$key, dat$altkey, sep = ".")
现在数据如下所示:
key altkey A B C D
1.1 1 1 2.6 118.17 117 FALSE
1.2 1 2 1.4 117.11 115 FALSE
1.3 1 3 1.1 117.38 122 TRUE
1.4 1 4 24.6 NA 122 FALSE
1.5 1 5 48.6 91.90 122 FALSE
1.6 1 6 59.8 NA 122 FALSE
2.1 2 1 20.2 118.23 113 FALSE
2.2 2 2 2.5 123.67 120 TRUE
2.3 2 3 7.4 116.30 120 FALSE
2.4 2 4 2.8 118.86 120 FALSE
2.5 2 5 6.9 124.72 120 FALSE
2.6 2 6 2.5 123.81 120 FALSE
2.7 2 7 8.5 119.23 115 FALSE
你可以打电话mlogit()
直接地:
mlogit(D ~ A + B + C, dat,
chid.var = "key",
alt.var = "altkey",
choice = "D",
shape = "long")
Result:
Call:
mlogit(formula = D ~ A + B + C, data = dat, chid.var = "key", alt.var = "altkey", choice = "D", shape = "long", method = "nr", print.level = 0)
Coefficients:
2:(intercept) 3:(intercept) 4:(intercept) 5:(intercept) 6:(intercept)
10.7774 4.8129 5.2257 -17.2522 -7.7364
7:(intercept) A B C
10.0389 1.6010 2.7156 2.9888