我得到了一些很好的帮助,使我的数据格式化正确,用mlogit (Formatting data for mlogit)生成了一个多项逻辑模型。
然而,我现在试图分析协变量在我的模型中的影响。我发现mlogit.effects()中的帮助文件内容不多。其中一个问题是,该模型似乎产生了大量的NAs (参见下面的index(mod1) )。
mlogit.effects处理下面的数据吗?multinom()。但是,我不知道如何格式化数据以适应使用multinom()的公式。我的数据是由七个不同项目(可访问性、信息、权衡、辩论、社交和响应)组成的系列排名,我是否只是对他们选择的第一名进行建模,而忽略了他们在其他排名中所选择的?我可以得到那个信息。可复制代码如下:
#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),]
#Change the choice index variable (X) to have no interruptions, as a result of removing some incomplete cases
dat$X <- seq(1,nrow(dat),1)
#Tidy data to get it into long format
dat.out <- dat %>%
gather(Open, Rank, -c(1,9:12)) %>%
arrange(X, Open, Rank)
#Create mlogit object
mlogit.out <- mlogit.data(dat.out, shape='long',alt.var='Open',choice='Rank', ranked=TRUE,chid.var='X')
#Fit Model
mod1 <- mlogit(Rank~1|gender+age+economic+Job,data=mlogit.out)下面是我试图设置一个类似于帮助文件中描述的数据框架的尝试。它不起作用。我承认,虽然我很了解应聘者的家庭,但对我来说,tapply是个模糊的人。
with(mlogit.out, data.frame(economic=tapply(economic, index(mod1)$alt, mean)))从帮助中比较:
data("Fishing", package = "mlogit")
Fish <- mlogit.data(Fishing, varying = c(2:9), shape = "wide", choice = "mode")
m <- mlogit(mode ~ price | income | catch, data = Fish)
# compute a data.frame containing the mean value of the covariates in
# the sample data in the help file for effects
z <- with(Fish, data.frame(price = tapply(price, index(m)$alt, mean),
catch = tapply(catch, index(m)$alt, mean),
income = mean(income)))
# compute the marginal effects (the second one is an elasticity
effects(m, covariate = "income", data = z)发布于 2016-06-23 21:43:05
您使用的是排序数据,而不仅仅是多项选择数据。mlogit中排名数据的结构是,一个人的第一组记录都是选项,其次是除排名第一之外的所有选项,依此类推。但是索引每次都假定相同数量的选项。所以一群纳特人。我们只需要除掉他们。
> with(mlogit.out, data.frame(economic=tapply(economic, index(mod1)$alt[complete.cases(index(mod1)$alt)], mean)))
economic
Accessible 5.13
Debate 4.97
Information 5.08
Officials 4.92
Responsive 5.09
Social 4.91
Trade.Offs 4.91https://stackoverflow.com/questions/30876000
复制相似问题