格式化mlogit的数据 [英] Formatting data for mlogit
本文介绍了格式化mlogit的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我度过了一个残酷的时刻,需要通过mlogit来设置我的数据以进行多项logit分析.我的数据集可从下面代码中的 url 中获得.
I am having a murderous time getting my data set in shape for a multinomial logit analysis via mlogit. My data set is available from the url in the code below.
我遇到以下错误:
row.names<-.data.frame
(*tmp*
中的错误,值= c("1.Accessible", "1.Accessible" ,:不允许重复的"row.names"
Error in
row.names<-.data.frame
(*tmp*
, value = c("1.Accessible", "1.Accessible", : duplicate 'row.names' are not allowed
我在其他地方检查过,似乎出现了这个问题.我试过使用alt.levels
而不是alt.var
参数,但这不起作用.
I've checked elsewhere and this problem seems to come up. I've tried playing with the alt.levels
as opposed to the alt.var
argument, and that doesn't work.
#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)
推荐答案
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
这篇关于格式化mlogit的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文