gpt4 book ai didi

r - 我应该如何格式化 R mlogit 包的数据?

转载 作者:行者123 更新时间:2023-12-02 15:43:55 24 4
gpt4 key购买 nike

我正在将 mlogit 包与 R 结合使用。

使用以下方式导入我的数据后:

t <-read.csv('junk.csv',header=TRUE, sep=",", dec=".")

并调用:

x <- mlogit.data(t,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’

有什么解决办法吗?

我的数据以以下格式存在于 csv 文件中:

[垃圾.csv]

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,

最佳答案

我对 mlogit 的体验是,它对于不正确的数据不太宽容。

就您的情况而言,我注意到第一个受访者有 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)

对所有数据运行 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")

结果:

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

关于r - 我应该如何格式化 R mlogit 包的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9355535/

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