2017-07-04 27 views
2

我想在R來使用Mlogit很多意見,我有點新logits,和我有麻煩在Mlogit框架建立我的問題。我其實不是完全可以肯定 mlogit是正確的做法。這是一個類似的問題。建立R中的Mlogit爲每個類別

考慮一個棒球的數據集,與結果變量是發生在「走出去」「單」,「雙」,「三聯」和「本壘打」。對於解釋變量,我們有面糊的名字,投手的名字和體育場。每個麪糊都有數百個觀察點,其中包括許多面向同一個投手的麪糊。

我認爲這絕對是一個多項式邏輯,因爲我有多個分類結果,但我不確定,因爲所有的文檔似乎都在處理替代方案之間的「選擇」,但這並不是真的。我試圖通過讓擊球手有一個因子變量,另一個用於投手,另一個用於球場來啓動我的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

+0

請附上您的數據和代碼的[重複的例子(https://stackoverflow.com/q/5963269/1222578),或者它的人很難知道發生了什麼事情。 – Marius

+0

我想添加數據,但我該怎麼做?我可以使用谷歌表格的鏈接嗎? –

回答

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

+0

謝謝,你能否告訴我爲什麼你用「擊球手」和右邊的「|」來設置公式? 我無法理解我的「另類」的問題,「indivudal」,「選擇」的框架,Mlogit希望。 –

+0

文檔說:「用於與某些個體多元logit估計交易,使一個或一個替代的一組幾個備選方案中依次選擇的數據集。」 我的數據集顯然沒有一個人做出選擇的話,是不是合適,即使使用這些模型?我應該如何考慮將其納入該框架? –

+0

@SamAsin我意識到這看起來很奇怪,但我相信這是使用'mlogit'包中的'mlogit'來估計多項邏輯模型的正確方法。或者,您可以使用'globaltest'的更「簡單」'mlogit'功能。該公式是「結果〜擊球手+投手+位置」。 –