【问题标题】:Setting up an Mlogit in R with many observations for each category在 R 中设置一个 Mlogit,每个类别都有许多观察结果
【发布时间】:2017-07-04 00:54:56
【问题描述】:

我正在尝试在 R 中使用 Mlogit,我对 logits 有点陌生,并且在 Mlogit 框架中设置我的问题时遇到了麻烦。我实际上并不完全确定 mlogit 是正确的方法。这是一个类似的问题。

考虑一个棒球数据集,其结果变量包含“出局”、“单人”、“双人”、“三人”和“本垒打”。对于解释变量,我们有击球手的名字、投手的名字和体育场。每个击球手都有数百个观察结果,其中许多击球手面对同一个投手。

我认为这绝对是多项式 logit,因为我有多个分类结果,但我不确定,因为所有文档似乎都在处理备选方案之间的“选择”,而事实并非如此。我尝试通过为击球手设置一个因子变量、为投手设置另一个变量、为体育场设置另一个变量来开始我的 logit 模型。当我在 R 中尝试这个时,我得到了

Error in row.names<-.data.frame(*tmp*, value = value) : invalid 'row.names' length

通过一些谷歌搜索,我认为可能只期望对击球手、投手和公园的每种组合进行一次观察?也许不吧?我究竟做错了什么?我应该如何设置?

编辑: 这里的数据示例

https://docs.google.com/spreadsheets/d/19fiq_QEMj4nAPcTqIRxeaYNPgqeHxKAEuPrfHMeIJ7o/edit?usp=sharing

【问题讨论】:

  • 请包含您的数据和代码的reproducible example,否则人们很难知道发生了什么。
  • 我想添加数据,但我该怎么做呢?我可以使用指向 Google 表格的链接吗?

标签: r mlogit


【解决方案1】:

以下是有关如何开始分析数据的一些建议。

# Your dataset
dts <- structure(list(outcome = c(1L, 1L, 2L, 3L, 1L, 3L, 2L, 3L, 3L, 
3L, 3L, 1L, 2L, 2L, 2L, 1L, 3L, 2L, 2L, 2L, 1L, 2L, 3L, 2L, 2L, 
2L, 2L, 1L, 1L, 2L, 3L, 2L, 3L, 1L, 2L, 2L, 3L, 2L, 3L, 3L, 3L, 
2L, 1L, 1L, 1L, 2L, 3L, 2L, 1L), hitter = structure(c(3L, 3L, 
3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 2L, 2L, 2L, 1L, 1L, 1L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("james", 
"jill", "john"), class = "factor"), pitcher = structure(c(3L, 
3L, 1L, 1L, 1L, 1L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 3L, 1L, 1L, 
2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 3L, 2L, 1L, 2L, 3L, 2L, 
3L, 2L, 1L, 1L, 2L, 2L, 1L, 3L, 3L, 1L, 2L, 2L, 1L, 1L, 2L, 2L
), .Label = c("bill", "bob", "brett"), class = "factor"), place = structure(c(3L, 
3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 5L, 
5L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L
), .Label = c("ca", "co", "dc", "ny", "tn"), class = "factor")), .Names = c("outcome", 
"hitter", "pitcher", "place"), class = "data.frame", row.names = c(NA, 
-49L))

# Estimation of a multinomial logistic regression model
library(mlogit)
dts.wide <- mlogit.data(dts, choice="outcome", shape="wide")
fit.mlogit <- mlogit(outcome ~ 1 | hitter+pitcher+place, data=dts.wide)

# Results
library(stargazer)
stargazer(fit.mlogit, type="text")

# Model coefficients with standard errors and statistical significance (stars)
==========================================
                   Dependent variable:    
               ---------------------------
                         outcome          
------------------------------------------
2:(intercept)            19.456           
                       (3,056.626)        

3:(intercept)            35.179           
                       (4,172.540)        

2:hitterjill             -17.543          
                       (3,056.625)        

3:hitterjill             -33.117          
                       (4,172.540)        

2:hitterjohn             -0.188           
                         (0.996)          

3:hitterjohn             -1.410           
                         (1.056)          

2:pitcherbob             -0.070           
                         (1.005)          

3:pitcherbob             -1.270           
                         (1.091)          

2:pitcherbrett           -0.908           
                         (1.063)          

3:pitcherbrett           -2.284*          
                         (1.257)          

2:placeco                -1.655           
                         (1.557)          

3:placeco                -17.688          
                       (2,840.270)        

2:placedc                -19.428          
                       (3,056.626)        

3:placedc                -34.479          
                       (4,172.540)        

2:placeny                -18.802          
                       (3,056.625)        

3:placeny                -32.873          
                       (4,172.540)        

2:placetn                -18.885          
                       (3,056.626)        

3:placetn                -32.140          
                       (4,172.540)        

------------------------------------------
Observations               49             
R2                        0.155           
Log Likelihood           -44.605          
LR Test             16.388 (df = 18)      
==========================================
Note:          *p<0.1; **p<0.05; ***p<0.01

更多关于 R 中多项式逻辑模型估计的详细信息,请访问here

【讨论】:

  • 谢谢,你能告诉我为什么你用击球手设置公式并放在“|”的右边吗?我无法在 Mlogit 想要的“替代”、“个人”、“选择”框架中理解我的问题。
  • 文档说:“用于多项 logit 估计的数据集涉及一些个人,他们在一组备选方案中做出一个或顺序选择一个备选方案。”我的数据集显然根本没有个人做出选择,甚至使用这些模型是否合适?我应该如何考虑将其融入该框架?
  • @SamAsin 我意识到这看起来很奇怪,但我确信这是使用 mlogit 包中的 mlogit 估计多项逻辑模型的正确方法。作为替代方案,您可以使用globaltest 的更“简单”的mlogit 函数。公式为outcome ~ hitter+pitcher+place
猜你喜欢
  • 2012-11-09
  • 1970-01-01
  • 2016-09-10
  • 2020-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-08
  • 1970-01-01
相关资源
最近更新 更多