多项混合logit模型mlogit r-package。

时间:2022-09-21 17:42:11

I discovered the mlogit-package for multinomial logit models in search of estimating a multinomial mixed logit model. After reading the excellent vignette I discovered that I could not apply my data on any of the described examples.

我发现了多项logit模型的mlogit-package,用于估计多项混合的logit模型。在阅读了优秀的vignette之后,我发现我无法将我的数据应用到任何描述的示例中。

I now write in hope of help with my problem and created a minimal example to illustrate my situation.

现在我希望能帮助解决我的问题,并创建了一个最小的例子来说明我的情况。

The Problem is as follows: There are words with the consonant 'Q' somewhere. Now an experiment was conducted with people who were tasked to listen to these words and say if they heard a Q, an U or some OTHER consonant. This has to modeled in dependence of some factors like syllable position or real/non-real-word.

问题是这样的:在某个地方有辅音“Q”的单词。现在有一项实验是对那些被要求听这些单词的人进行的,他们会说,如果他们听到了一个Q,一个U或者其他的辅音。这必须依赖于一些因素,如音节位置或真实/非实词。

In the minimal example I created 4 people and their answers with the syllable position.

在最小的例子中,我用音节的位置创造了4个人和他们的答案。

library(mlogit)
library(nnet)
set.seed(1234)
data <- data.frame(personID = as.factor(sample(1:4, 40, replace=TRUE)),
               decision = as.factor(sample(c("Q","U", "other"), 40, replace=TRUE)),
               syllable = as.factor(sample(1:4, 40, replace=TRUE)))
summary(data)
 personID  decision  syllable
 1:11     other:10   1:18    
 2:10     Q    :18   2: 9    
 3:10     U    :12   3: 5    
 4: 9                4: 8 

As far as I know nnet's multinomfunction does not cover mixed models.

据我所知,nnet的多项功能不包括混合模型。

modNnet1 <- multinom(decision ~ syllable, data=data)

First I used the mlogit.data-function to reshape the file. After discussion with a colleague we came to the conclusion that there is no alternative.specific.variable.

首先,我使用了mlogit。数据功能来重塑文件。在和同事讨论之后,我们得出结论:没有其他选择。

 dataMod <- mlogit.data(data, shape="wide", choice="decision", id.var="personID")

 mod1 <- mlogit(formula = decision ~ 0|syllable,
           data = dataMod,
           reflevel="Q", rpar=c(personID="n"), panel=TRUE)
  Error in names(sup.coef) <- names.sup.coef : 
    'names' attribute [1] must be the same length as the vector [0]

 mod2 <- mlogit(formula = decision ~ personID|syllable,
           data = dataMod,
           reflevel="Q", rpar=c(personID="n"), panel=TRUE)
  Error in solve.default(H, g[!fixed]) : 
     Lapack routine dgesv: system is exactly singular: U[3,3] = 0

No I do not know what to do, so I ask for help in here. But I believe this kind of problem can be solved with mlogit and I just don't see it yet ;)

不,我不知道该怎么做,所以我在这里寻求帮助。但是我相信这类问题可以用mlogit解决,而我只是还没有看到。

1 个解决方案

#1


3  

The rpar argument accepts only alternative-specific variables. There is no need to specify the person-specific id in the model formula -- this is handled by including id.var = something in the mlogit.data command. For example, if you had an alternative specific covariate acov, you could allow random slopes for acov across a panel:

rpar参数只接受可替换的特定变量。没有必要在模型公式中指定特定于人的id——这是通过包含id.var =在mlogit中的某个东西来处理的。数据的命令。例如,如果您有一个可选的特定covariate acov,您可以在一个面板上允许acov的随机斜坡:

N = 200
dat <- data.frame(personID = as.factor(sample(1:4, N, replace=TRUE)),
               decision = as.factor(sample(c("Q","U", "other"), N, replace=TRUE)),
               syllable = as.factor(sample(1:4, N, replace=TRUE)),
               acov.Q = rnorm(N), acov.U = rnorm(N), acov.other = rnorm(N))
dataMod <- mlogit.data(dat, shape="wide", choice="decision", id.var="personID", varying = 4:6)
mlogit(formula = decision ~ acov|syllable, rpar = c(acov = "n"), panel = T, data = dataMod)

It seems you are trying to fit a model with a random, person-specific intercept for each alternative (not random slopes). Unfortunately, I don't think you can do so in mlogit (but see this post).

看起来你是在尝试用一个随机的、人为的截距来匹配一个模型(不是随机的斜率)。不幸的是,我不认为您可以在mlogit中这样做(但是请参阅本文)。

One option that would work to fit random intercepts in the absence of alternative-specific covariates is MCMCglmm.

MCMCglmm是一种选择,可以在没有替代特定的协变量的情况下进行随机截取。

library(MCMCglmm)
priors = list(R = list(fix = 1, V = 0.5 * diag(2), n = 2),
              G = list(G1 = list(V = diag(2), n = 2)))
m <- MCMCglmm(decision ~ -1 + trait + syllable,
              random = ~ idh(trait):personID,
              rcov = ~ us(trait):units,
              prior = priors,
              nitt = 30000, thin = 20, burnin = 10000,
              family = "categorical",
              data = dat)

Relevant issues are prior selection, convergence of Markov chains, etc. Florian Jaeger's lab's blog has a short tutorial on multinomial models via MCMCglmm that you might find helpful, in addition to the MCMCglmm documentation.

相关的问题是之前的选择,Markov链的聚合,等等。Florian Jaeger的实验室的博客上有一个关于MCMCglmm的多项式模型的简短教程,除了MCMCglmm文档之外,你可能会发现它是有用的。

#1


3  

The rpar argument accepts only alternative-specific variables. There is no need to specify the person-specific id in the model formula -- this is handled by including id.var = something in the mlogit.data command. For example, if you had an alternative specific covariate acov, you could allow random slopes for acov across a panel:

rpar参数只接受可替换的特定变量。没有必要在模型公式中指定特定于人的id——这是通过包含id.var =在mlogit中的某个东西来处理的。数据的命令。例如,如果您有一个可选的特定covariate acov,您可以在一个面板上允许acov的随机斜坡:

N = 200
dat <- data.frame(personID = as.factor(sample(1:4, N, replace=TRUE)),
               decision = as.factor(sample(c("Q","U", "other"), N, replace=TRUE)),
               syllable = as.factor(sample(1:4, N, replace=TRUE)),
               acov.Q = rnorm(N), acov.U = rnorm(N), acov.other = rnorm(N))
dataMod <- mlogit.data(dat, shape="wide", choice="decision", id.var="personID", varying = 4:6)
mlogit(formula = decision ~ acov|syllable, rpar = c(acov = "n"), panel = T, data = dataMod)

It seems you are trying to fit a model with a random, person-specific intercept for each alternative (not random slopes). Unfortunately, I don't think you can do so in mlogit (but see this post).

看起来你是在尝试用一个随机的、人为的截距来匹配一个模型(不是随机的斜率)。不幸的是,我不认为您可以在mlogit中这样做(但是请参阅本文)。

One option that would work to fit random intercepts in the absence of alternative-specific covariates is MCMCglmm.

MCMCglmm是一种选择,可以在没有替代特定的协变量的情况下进行随机截取。

library(MCMCglmm)
priors = list(R = list(fix = 1, V = 0.5 * diag(2), n = 2),
              G = list(G1 = list(V = diag(2), n = 2)))
m <- MCMCglmm(decision ~ -1 + trait + syllable,
              random = ~ idh(trait):personID,
              rcov = ~ us(trait):units,
              prior = priors,
              nitt = 30000, thin = 20, burnin = 10000,
              family = "categorical",
              data = dat)

Relevant issues are prior selection, convergence of Markov chains, etc. Florian Jaeger's lab's blog has a short tutorial on multinomial models via MCMCglmm that you might find helpful, in addition to the MCMCglmm documentation.

相关的问题是之前的选择,Markov链的聚合,等等。Florian Jaeger的实验室的博客上有一个关于MCMCglmm的多项式模型的简短教程,除了MCMCglmm文档之外,你可能会发现它是有用的。