多项式逻辑:对 R 中备选方案子集的估计
Multinomial logit: estimation on a subset of alternatives in R
如McFadden (1978)所示,如果多项式logit模型中的备选方案数量大到无法计算,通过对备选方案进行随机子集化来获得一致的估计仍然是可行的,因此估计的概率每个人的 C 都基于所选择的备选方案和 C 其他随机选择的备选方案。在这种情况下,每个人的备选方案子集的大小为 C+1。
我的问题是关于这个算法在 R 中的实现。它是否已经嵌入到任何多项式 logit 包中?如果不是——根据我目前所知道的,这似乎很可能——如何在不广泛重新编码的情况下将程序包含在预先存在的包中?
我的建议是查看 mlogit 包。
小插图:
该软件包有一组(在我看来)值得一看的示例练习:
你可能还想看看gmnl包(我没用过)
- https://cran.r-project.org/web/packages/gmnl/index.html
- Multinomial Logit Models with Continuous and Discrete Individual Heterogeneity in R: The gmnl Package
- Mauricio Sarrias' (Author) gmnl Web page
问题:您还尝试应用多项式 Logit 模型的具体问题是什么?适当感兴趣。
除了上述问题,我希望以上内容能为您指明正确的方向。
不确定问题更多的是关于对备选方案进行抽样还是对备选方案抽样后的 MNL 模型进行估计。据我所知,到目前为止,还没有 R 包可以对备选方案(前者)进行抽样,但后者可以通过现有包(例如 mlogit)实现。我相信原因是抽样过程因数据的组织方式而异,但使用您自己的一些代码相对容易。下面是改编自我用于 this paper 的代码。
library(tidyverse)
# create artificial data
set.seed(6)
# data frame of choser id and chosen alt_id
id_alt <- data.frame(
id = 1:1000,
alt_chosen = sample(1:30, 1)
)
# data frame for universal choice set, with an alt-specific attributes (alt_x2)
alts <- data.frame(
alt_id = 1:30,
alt_x2 = runif(30)
)
# conduct sampling of 9 non-chosen alternatives
id_alt <- id_alt %>%
mutate(.alts_all =list(alts$alt_id),
# use weights to avoid including chosen alternative in sample
.alts_wtg = map2(.alts_all, alt_chosen, ~ifelse(.x==.y, 0, 1)),
.alts_nonch = map2(.alts_all, .alts_wtg, ~sample(.x, size=9, prob=.y)),
# combine chosen & sampled non-chosen alts
alt_id = map2(alt_chosen, .alts_nonch, c)
)
# unnest above data.frame to create a long format data frame
# with rows varying by choser id and alt_id
id_alt_lf <- id_alt %>%
select(-starts_with(".")) %>%
unnest(alt_id)
# join long format df with alts to get alt-specific attributes
id_alt_lf <- id_alt_lf %>%
left_join(alts, by="alt_id") %>%
mutate(chosen=ifelse(alt_chosen==alt_id, 1, 0))
require(mlogit)
# convert to mlogit data frame before estimating
id_alt_mldf <- mlogit.data(id_alt_lf,
choice="chosen",
chid.var="id",
alt.var="alt_id", shape="long")
mlogit( chosen ~ 0 + alt_x2, id_alt_mldf) %>%
summary()
当然,可以不使用 purrr::map
函数,使用 apply
变体或遍历 id_alt
.
的每一行
mlogit 包中当前未实现备选方案抽样。如前所述,解决方案是使用备选方案的子集生成 data.frame,然后使用 mlogit(重要的是使用没有截距的公式)。请注意,mlogit 可以处理不平衡数据,即备选方案的数量不必在所有选择情况下都相同。
如McFadden (1978)所示,如果多项式logit模型中的备选方案数量大到无法计算,通过对备选方案进行随机子集化来获得一致的估计仍然是可行的,因此估计的概率每个人的 C 都基于所选择的备选方案和 C 其他随机选择的备选方案。在这种情况下,每个人的备选方案子集的大小为 C+1。
我的问题是关于这个算法在 R 中的实现。它是否已经嵌入到任何多项式 logit 包中?如果不是——根据我目前所知道的,这似乎很可能——如何在不广泛重新编码的情况下将程序包含在预先存在的包中?
我的建议是查看 mlogit 包。
小插图:
该软件包有一组(在我看来)值得一看的示例练习:
你可能还想看看gmnl包(我没用过)
- https://cran.r-project.org/web/packages/gmnl/index.html
- Multinomial Logit Models with Continuous and Discrete Individual Heterogeneity in R: The gmnl Package
- Mauricio Sarrias' (Author) gmnl Web page
问题:您还尝试应用多项式 Logit 模型的具体问题是什么?适当感兴趣。
除了上述问题,我希望以上内容能为您指明正确的方向。
不确定问题更多的是关于对备选方案进行抽样还是对备选方案抽样后的 MNL 模型进行估计。据我所知,到目前为止,还没有 R 包可以对备选方案(前者)进行抽样,但后者可以通过现有包(例如 mlogit)实现。我相信原因是抽样过程因数据的组织方式而异,但使用您自己的一些代码相对容易。下面是改编自我用于 this paper 的代码。
library(tidyverse)
# create artificial data
set.seed(6)
# data frame of choser id and chosen alt_id
id_alt <- data.frame(
id = 1:1000,
alt_chosen = sample(1:30, 1)
)
# data frame for universal choice set, with an alt-specific attributes (alt_x2)
alts <- data.frame(
alt_id = 1:30,
alt_x2 = runif(30)
)
# conduct sampling of 9 non-chosen alternatives
id_alt <- id_alt %>%
mutate(.alts_all =list(alts$alt_id),
# use weights to avoid including chosen alternative in sample
.alts_wtg = map2(.alts_all, alt_chosen, ~ifelse(.x==.y, 0, 1)),
.alts_nonch = map2(.alts_all, .alts_wtg, ~sample(.x, size=9, prob=.y)),
# combine chosen & sampled non-chosen alts
alt_id = map2(alt_chosen, .alts_nonch, c)
)
# unnest above data.frame to create a long format data frame
# with rows varying by choser id and alt_id
id_alt_lf <- id_alt %>%
select(-starts_with(".")) %>%
unnest(alt_id)
# join long format df with alts to get alt-specific attributes
id_alt_lf <- id_alt_lf %>%
left_join(alts, by="alt_id") %>%
mutate(chosen=ifelse(alt_chosen==alt_id, 1, 0))
require(mlogit)
# convert to mlogit data frame before estimating
id_alt_mldf <- mlogit.data(id_alt_lf,
choice="chosen",
chid.var="id",
alt.var="alt_id", shape="long")
mlogit( chosen ~ 0 + alt_x2, id_alt_mldf) %>%
summary()
当然,可以不使用 purrr::map
函数,使用 apply
变体或遍历 id_alt
.
mlogit 包中当前未实现备选方案抽样。如前所述,解决方案是使用备选方案的子集生成 data.frame,然后使用 mlogit(重要的是使用没有截距的公式)。请注意,mlogit 可以处理不平衡数据,即备选方案的数量不必在所有选择情况下都相同。