【问题标题】:Formatting data for mlogit为 mlogit 格式化数据
【发布时间】:2015-06-15 18:15:18
【问题描述】:

我正在为通过 mlogit 进行多项 logit 分析整理我的数据集而度过了一段痛苦的时光。我的数据集可从下面代码中的url 获得。

我收到以下错误:

row.names<-.data.frame(*tmp*, value = c("1.Accessible", "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)

【问题讨论】:

  • 啊。 使用 reshape/reshape2/cast。当我同样花了几天时间试图将我的数据转换成 mlogit 的形式,与 reshape/reshape2/cast 争吵时,你给了我模糊的回忆。最后我发现在我的特定问题上,mlogit 的表现不如其他算法。哦,我怎么笑了。好时光,好时光。

标签: r mlogit


【解决方案1】:

我的建议是你试试 nnet 包中的multinom() 函数。它不需要 mlogit 或 mnlogit 的特殊格式。

library(RCurl)
library(nnet)

Data<-getURL("https://raw.githubusercontent.com/sjkiss/Survey/master/mlogit.out.csv")
Data<-read.csv(text=Data,header=T)
Data<-na.omit(Data) # Get rid of NA's
Data<-as.data.frame(Data)
# relevel the dependent variable (must be a factor)
Data$Job<-factor(Data$Job)
# Using "Online Blogger" as the reference, substitute with your choice
Data$Job<-relevel(Data$Job,ref="Online blogger")
# Run the multinomial logistic regression
# (seems like an awful lot of variables btw)
Data<-multinom(formula=Job~Accessible+Information+Responsive+Debate+Officials+Social+Trade.Offs+economic+gender+age,data=Data)

【讨论】:

  • 我想使用 mlogit 包的原因之一是因为它明确地处理小插图中的排名数据。我的数据是排名数据,尽管所有协变量都是个体水平的协变量。没有一个是替代的特定协变量。在这种情况下,我能否将其视为直接多项逻辑回归?
  • 你的意思是因变量是有序的吗?如果是这样,您仍然可以使用多项逻辑回归,与序数逻辑回归相比,只会有轻微的功率损失。如果要运行简单的序数逻辑回归,可以使用 MASS 包中的 polr()。自变量是否是序数无关紧要。
  • 您只需通过X, Open, Rank订购即可。看我的回答。
  • 安迪,它是序数是的,但也是排名。因此,如果一个人将 1 分配给项目,他们就不能将 1 分配给任何其他项目。
  • 安迪,我将如何格式化上面的数据集以与 multinom() 一起使用?我无法让 effects() 命令在 mlogit 环境中工作。
【解决方案2】:
dat.out<-dat %>%
gather(Open, Rank, -c(1,9:12)) %>%    
arrange(X, Open, Rank)
    mlogit.out<-mlogit.data(dat.out, shape='long',alt.var='Open',choice='Rank', ranked=TRUE,child.var='X')

head(mlogit.out)
              X economic gender  age                     Job        Open  Rank
1.Accessible  1        5   Male 1970 Professional journalist  Accessible FALSE
1.Information 1        5   Male 1970 Professional journalist Information FALSE
1.Responsive  1        5   Male 1970 Professional journalist  Responsive  TRUE
1.Debate      1        5   Male 1970 Professional journalist      Debate FALSE
1.Officials   1        5   Male 1970 Professional journalist   Officials FALSE
1.Social      1        5   Male 1970 Professional journalist      Social FALSE

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 2016-07-12
    相关资源
    最近更新 更多