为了通过 mlogit 进行多项 Logit 分析,我正在经历一段艰难的时间来整理我的数据集。我的数据集可从url https://raw.githubusercontent.com/sjkiss/Survey/master/mlogit.out.csv在下面的代码中。
我收到以下错误:
错误于row.names<-.data.frame
(*tmp*
, value = c("1.可访问",
"1.Accessible", : 不允许重复的“row.names”
我检查过其他地方,似乎也出现了这个问题。我试过玩alt.levels
与alt.var
争论,这是行不通的。
#Loadpackages
library(RCurl)
library(mlogit)
library(tidyr)
library(dplyr)
#URL where data is stored
dat.url<- 'https://raw.githubusercontent.com/sjkiss/Survey/master/mlogit.out.csv'
#Get data
dat<-read.csv(dat.url)
#Complete cases only as it seems mlogit cannot handle missing values or tied data which in this case you might get because of median imputation
dat<-dat[complete.cases(dat),]
#Tidy data to get it into long format
dat.out<-dat %>%
gather(Open, Rank, -c(1,9:12))
#Try to replicate code on pp.26-27 of http://cran.r- project.org/web/packages/mlogit/vignettes/mlogit.pdf
mlogit.out<-mlogit.data(dat.out, shape='long',alt.var='Open',choice='Rank', id.var='X',ranked=TRUE)
#Try this option as per a discussion on stackexchange
mlogit.out<-mlogit.data(dat.out, shape='long',alt.levels='Open',choice='Rank', id.var='X',ranked=TRUE)