我收到了一些很好的帮助,让我的数据正确格式化,在这里生成了一个带有 mlogit 的多项逻辑模型(为 mlogit 格式化数据)
但是,我现在正在尝试分析协变量在我的模型中的影响。我发现其中的帮助文件mlogit.effects()
信息量不大。问题之一是该模型似乎产生了很多行 NA(见下文index(mod1)
)。
- 谁能澄清为什么我的数据会产生这些 NA?
- 谁能帮我
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)