提问人:RobertoAS 提问时间:7/7/2023 更新时间:7/7/2023 访问量:32
R 包 mlogit 中命令 mlogit 的系数的方差-协方差矩阵存储在哪里,或者我该如何计算它?
Where is the variance-covariance matrix for the coefficients from the command mlogit from the R package mlogit stored or how can I calculate it?
问:
我使用 R 包中的命令估计了一个嵌套的 logit。输出采用包含 17 个元素的列表形式。如果我在输出上运行,我会得到一个表格,其中包含估计系数及其各自的标准误差。但是,我在命令的输出(包含 17 个元素的列表)中找不到此类标准错误。我错过了它所在的地方吗?下面我提供了一个不可重现的示例,说明我正在运行的代码类型,以及输出中的元素列表。mlogit()
mlogit
summary()
mlogit()
我的猜测是,也许该命令正在以某种方式计算估计系数的标准误差。鉴于系数是使用最大似然法估计的,我猜这些标准误差来自计算费舍尔信息矩阵的逆数。但是,鉴于我从命令中获得的输出,我不确定如何做到这一点。如果有人能帮助我,我将不胜感激。summary()
mlogit()
mlogit()
提前感谢您的帮助!
代码示例:
library(dfidx)
library(mlogit)
ml_data <- data %>% dfidx(idx = c("id", "alternative"))
model_output <- mlogit(choice ~ regresor1 + regresor2,
data = ml_data,
reflevel = "alt1",
nests = list(nest1 = c("alt2", "alt3", "alt4"),
nest2 = c("alt1")),
un.nest.el = FALSE, constPar = c("iv:nest2" = 1))
summary(model_output)
mlogit()
的输出:
更多信息可在此处获得:Rdocumentation(尽管我的列表有 17 个元素,而文档只描述了其中的 13 个)
答:
看?vcov.mlogit
您还可以通过键入并按 Enter 来检查函数(请注意三冒号以显示 R 包的“隐藏”机制)mlogit:::summary.mlogit
通过浏览按 TAB 两次后弹出的建议,您将找到这两个选项。如果您已经知道自己在寻找什么(例如 或者对于特定于 class 的结果的方法,键入已知片段以缩小 TAB 扩展的结果范围。summary.xyz
plot.xyz
xyz
您的示例不可重现,但我们可以从以下位置运行示例:?mlogit
library(mlogit)
data("Fishing", package = "mlogit")
Fish <- dfidx(Fishing, varying = 2:9, shape = "wide", choice = "mode")
model <- mlogit(mode ~ price + catch, data = Fish)
这导致
model
#>
#> Call:
#> mlogit(formula = mode ~ price + catch, data = Fish, method = "nr")
#>
#> Coefficients:
#> (Intercept):boat (Intercept):charter (Intercept):pier
#> 0.87137 1.49889 0.30706
#> price catch
#> -0.02479 0.37717
请注意,对象的类是:model
"mlogit"
class(model)
#> [1] "mlogit"
在 R 中,该函数是 S3 泛型函数,当我们调用 时,它实际上调用了该函数:summary
summary(model)
mlogit:::summary.mlogit
summary(model)
#>
#> Call:
#> mlogit(formula = mode ~ price + catch, data = Fish, method = "nr")
#>
#> Frequencies of alternatives:choice
#> beach boat charter pier
#> 0.11337 0.35364 0.38240 0.15059
#>
#> nr method
#> 7 iterations, 0h:0m:0s
#> g'(-H)^-1g = 6.22E-06
#> successive function values within tolerance limits
#>
#> Coefficients :
#> Estimate Std. Error z-value Pr(>|z|)
#> (Intercept):boat 0.8713749 0.1140428 7.6408 2.154e-14 ***
#> (Intercept):charter 1.4988884 0.1329328 11.2755 < 2.2e-16 ***
#> (Intercept):pier 0.3070552 0.1145738 2.6800 0.0073627 **
#> price -0.0247896 0.0017044 -14.5444 < 2.2e-16 ***
#> catch 0.3771689 0.1099707 3.4297 0.0006042 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Log-Likelihood: -1230.8
#> McFadden R^2: 0.17823
#> Likelihood ratio test : chisq = 533.88 (p.value = < 2.22e-16)
这不仅仅是将摘要打印到屏幕上 - 它实际上会进行进一步的计算,从而生成一个 类 的对象。我们可以通过存储对象并检查它来了解这一点:"summary.mlogit"
model_summary <- summary(model)
class(model_summary)
#> [1] "summary.mlogit" "mlogit"
我们将看到它包含的数据成员与以下组织不同:model
names(model_summary)
#> [1] "coefficients" "logLik" "gradient" "hessian"
#> [5] "est.stat" "fitted.values" "probabilities" "linpred"
#> [9] "indpar" "residuals" "omega" "rpar"
#> [13] "nests" "model" "freq" "formula"
#> [17] "call" "CoefTable" "lratio" "mfR2"
其中之一,成员是摘要中打印的数据表:CoefTable
model_summary$CoefTable
#> Estimate Std. Error z-value Pr(>|z|)
#> (Intercept):boat 0.87137491 0.114042831 7.640769 2.153833e-14
#> (Intercept):charter 1.49888838 0.132932796 11.275535 0.000000e+00
#> (Intercept):pier 0.30705525 0.114573796 2.679978 7.362701e-03
#> price -0.02478955 0.001704403 -14.544420 0.000000e+00
#> catch 0.37716885 0.109970659 3.429723 6.041986e-04
如果我们不想浏览对象的所有成员来查找汇总表,我们总是可以尝试 通用 S3 ,看看作者是否定义了方法。事实证明,他们有:coef
coef(model_summary)
#> Estimate Std. Error z-value Pr(>|z|)
#> (Intercept):boat 0.87137491 0.114042831 7.640769 2.153833e-14
#> (Intercept):charter 1.49888838 0.132932796 11.275535 0.000000e+00
#> (Intercept):pier 0.30705525 0.114573796 2.679978 7.362701e-03
#> price -0.02478955 0.001704403 -14.544420 0.000000e+00
#> catch 0.37716885 0.109970659 3.429723 6.041986e-04
看来这就是你要找的。但是,您在问题中提到了方差-协方差矩阵,这与系数表的概念不同。如果你真的想要方差-协方差矩阵,你也可以在模型上使用一种方法:vcov
vcov(model)
#> (Intercept):boat (Intercept):charter (Intercept):pier
#> (Intercept):boat 1.300577e-02 1.025681e-02 7.519846e-03
#> (Intercept):charter 1.025681e-02 1.767113e-02 7.145189e-03
#> (Intercept):pier 7.519846e-03 7.145189e-03 1.312715e-02
#> price -1.807016e-06 -9.194307e-05 8.030833e-07
#> catch 7.717477e-04 -5.247810e-03 7.401401e-04
#> price catch
#> (Intercept):boat -1.807016e-06 7.717477e-04
#> (Intercept):charter -9.194307e-05 -5.247810e-03
#> (Intercept):pier 8.030833e-07 7.401401e-04
#> price 2.904989e-06 1.010511e-05
#> catch 1.010511e-05 1.209355e-02
创建于 2023-07-07 with reprex v2.0.2
评论
summary.mlogit()