我正在从以下合成混合效应模型生成数据,用于代理选择运输模式的效用:
其中的代理的价格和时间。是总线,则等于 1 。
真正的模型有。
对于每个代理,我为每种交通方式(红色巴士、火车、汽车)生成价格和时间协变量()和中对每个代理的随机效应进行采样,然后从相应的 logit 模型中对运输选择进行采样。
这是生成选择数据的 R 代码:
library(mlogit)
set.seed(1987)
n.samples <- 10000
re.sd <- 10 # random effect standard deviation
## generate price, time covariates
mode <- c('red.bus', 'train', 'car')
is.bus <- c(1, 0, 0)
price.data <- rnorm(3*n.samples) + c(2,5,10)
time.data <- rnorm(3*n.samples) + c(10, 5, 2)
obs <- data.frame(mode=mode,
price=price.data,
time=time.data,
bus=is.bus)
## generate random effect + choice data
obs$agent.id <- rep(1:n.samples, each=3)
obs$choice <- NA
obs$util <- NA
for( a.id in unique(obs$agent.id) ) {
xx.sub <- obs[obs$agent.id == a.id,]
obs$util[obs$agent.id == a.id] <- xx.sub$price*-1 + xx.sub$time*-1 + xx.sub$bus*rnorm(1,sd=re.sd)
uu <- obs$util[obs$agent.id == a.id]
p.vec <- exp(uu)/sum(exp(uu))
obs$choice[obs$agent.id == a.id] <- rmultinom(1, 1, p.vec)==1
}
logit.data <- mlogit.data(obs,
shape = "long",
choice = "choice",
varying = which( colnames(obs) %in% c('price', 'time', 'bus') ),
alt.var = 'mode')
以下是生成的数据集的前几行:
> head(logit.data)
mode price time bus agent.id choice util
1.red.bus red.bus 0.7113747 9.700200 1 1 FALSE -21.306406
1.train train 5.2730015 5.908244 0 1 TRUE -11.181246
1.car car 12.8485015 2.256558 0 1 FALSE -15.105060
2.red.bus red.bus 1.7021472 9.642485 1 2 TRUE -7.677141
2.train train 5.2443204 5.671528 0 2 FALSE -10.915848
2.car car 10.2686018 2.250377 0 2 FALSE -12.518979
我试图用mlogit包拟合正确指定的模型,但我发现估计是错误的(特别是随机效应的标准偏差):
m.mixed <- mlogit(choice ~ price + time + bus | 0,
data=logit.data,
rpar= c(bus = 'n'),
R = 300, halton = NA)
summary(m.mixed)
> summary(m.mixed)
Call:
mlogit(formula = choice ~ price + time + bus | 0, data = logit.data,
rpar = c(bus = "n"), R = 300, halton = NA)
Frequencies of alternatives:
car red.bus train
0.1317 0.4084 0.4599
bfgs method
8 iterations, 0h:1m:55s
g'(-H)^-1g = 594
last step couldn't find higher value
Coefficients :
Estimate Std. Error t-value Pr(>|t|)
price -1.253720 0.026698 -46.9592 < 2.2e-16 ***
time -1.329888 0.032389 -41.0603 < 2.2e-16 ***
bus 1.402522 0.318802 4.3993 1.086e-05 ***
sd.bus 19.455180 2.107521 9.2313 < 2.2e-16 ***
真实sd.bus值为 10,但mlogit估计接近 20。
为什么 mlogit 不能恢复真实模型?