多项式 logit:对 R 中替代方案子集的估计

Eff*_*ffa 4 r subset mlogit

正如McFadden (1978)所表明的,如果多项 Logit 模型中的备选方案数量如此之多以至于计算变得不可能,通过随机子集备选方案来获得一致的估计仍然是可行的,因此每个个体的估计概率基于所选择的替代方案和 C 其他随机选择的替代方案。在这种情况下,每个个体的备选方案子集的大小为 C+1。

我的问题是关于这个算法在 R 中的实现。它是否已经嵌入到任何多项式 logit 包中?如果不是——根据我目前所知,这似乎很可能——如何在不进行广泛重新编码的情况下将该过程包含在预先存在的包中?

LmW*_*mW. 5

不确定问题更多的是关于对替代方案进行抽样还是在对替代方案进行抽样后对 MNL 模型进行估计。据我所知,到目前为止,还没有 R 包可以对替代方案(前者)进行采样,但后者可以通过 mlogit 等现有包进行采样。我认为原因是采样过程根据数据的组织方式而有所不同,但使用您自己的一些代码相对容易完成。下面是根据我在本文中使用的代码改编的代码。

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()
Run Code Online (Sandbox Code Playgroud)

当然,也可以不使用purrr::map函数,通过使用apply变体或循环遍历 的每一行id_alt