我正在创建一个代码来估计条件logit模型。 与我使用“ mlogit”软件包调整的代码相比,我得到的系数有很大不同,请您帮我确定我错了吗?
function(b,x,y,lambda){
xb1 <- x[,1]*b[1]+x[,2]*b[2]+x[,3]*b[3]
xb2 <- x[,4]*b[4]+x[,5]*b[5]+x[,6]*b[6]
xb3 <- x[,7]+b[7]+x[,8]*b[8]
exb1 <- exp(xb1)
exb2 <- exp(xb2)
exb3 <- exp(xb3)
exb_total <- exb1+exb2+exb3
p1 <- exb1/exb_total
p2 <- exb2/exb_total
p3 <- exb3/exb_total
l1 <- y[,1]*xb1+y[,2]*xb2+y[,3]*xb3
l2 <- log(exb_total)
likelihood <- -sum(l1-l2)
return(likelihood)
}
model_8coef<-optim(par=B,fn=LL.MNLR,x=X2,y=Y,lambda=0,control = list(maxit=10000),method = "BFGS")
mlogit代码
mlogit_mode<-mlogit.data(mode,shape = "wide",choice = "mod",varying = c(2:3,6:9))
modelogit<-mlogit(mod~0|1|t+c,mlogit_mode,reflevel = "rail")
summary(modelogit)
$par
[1] 6.55061382 0.08901337 1.01372853 9.15278946 0.21330866 -0.19448328
[7] -18.70340647 -0.59548734
$value
[1] 1856.872
$counts
function gradient
44 18
$convergence
[1] 0
$message
NULL
与mlogit相比
Call:
mlogit(formula = mod ~ 0 | 1 | t + c, data = mlogit_mode, reflevel = "rail",
method = "nr", print.level = 0)
Frequencies of alternatives:
rail bus car
0.28979 0.19240 0.51781
nr method
6 iterations, 0h:0m:0s
g'(-H)^-1g = 0.000241
successive function values within tolerance limits
Coefficients :
Estimate Std. Error z-value Pr(>|z|)
bus:(intercept) -0.888491 0.977076 -0.9093 0.36317
car:(intercept) 2.977079 0.996054 2.9889 0.00280 **
rail:t -0.079409 0.010740 -7.3937 1.428e-13 ***
bus:t -0.100321 0.013061 -7.6808 1.577e-14 ***
car:t -0.093099 0.011628 -8.0065 1.110e-15 ***
rail:c -1.033796 0.411701 -2.5110 0.01204 *
bus:c -0.594146 0.492629 -1.2061 0.22779
car:c -0.865582 0.123303 -7.0199 2.220e-12 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Log-Likelihood: -246.07
McFadden R^2: 0.42518
Likelihood ratio test : chisq = 364.03 (p.value = < 2.22e-16)
在计算输出中,观察到我获得了非常不同的系数,因此我无法理解我的错误在哪里。数据库的每一行都包含每个可用替代方案的时间和成本变量,在这种情况下,替代方案是:汽车,公共汽车和火车。因此,我必须估算8个系数$ \ beta ^ {auto} {0} $,$ \ beta ^ {auto} {time} $,$ \ beta ^ {auto} { cost} $,$ \ beta ^ {bus} {0} $,$ \ beta ^ {bus} {time} $,$ \ beta ^ {bus} {cost} $ ,$ \ beta ^ {rail} {time} $和$ \ beta ^ {rail} {cost} $ 谢谢!