Skip to content

Commit

Permalink
vcov argument not supported for plm models (#912)
Browse files Browse the repository at this point in the history
* `vcov` argument not supported for `plm` models
Fixes #911

* add tests

* update snapshots

* Update test-plm.R

* update wordlist

* lintr
  • Loading branch information
strengejacke authored Oct 24, 2023
1 parent a04deb7 commit 21dcad5
Show file tree
Hide file tree
Showing 19 changed files with 170 additions and 49 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: parameters
Title: Processing of Model Parameters
Version: 0.21.2.9
Version: 0.21.2.10
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

* Minor fixes for `print_html()` method for `model_parameters()`.

* Robust standard errors (argument `vcov`) now works for `plm` models.

# parameters 0.21.2

## Changes
Expand Down
43 changes: 36 additions & 7 deletions R/methods_plm.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,41 @@ degrees_of_freedom.plm <- function(model, method = "wald", ...) {


#' @export
standard_error.plm <- function(model, ...) {
se <- stats::coef(summary(model))
standard_error.plm <- function(model, vcov = NULL, vcov_args = NULL, verbose = TRUE, ...) {
dots <- list(...)
se <- NULL
se_standard <- stats::coef(summary(model))

# vcov: matrix
if (is.matrix(vcov)) {
se <- sqrt(diag(vcov))
}

# vcov: function which returns a matrix
if (is.function(vcov)) {
args <- c(list(model), vcov_args, dots)
se <- .safe(sqrt(diag(do.call("vcov", args))))
}

# vcov: character (with backward compatibility for `robust = TRUE`)
if (is.character(vcov) || isTRUE(dots[["robust"]])) {
.vcov <- insight::get_varcov(
model,
vcov = vcov,
vcov_args = vcov_args,
verbose = verbose,
...
)
se <- sqrt(diag(.vcov))
}

if (is.null(se)) {
se <- as.vector(se_standard[, 2])
}

.data_frame(
Parameter = .remove_backticks_from_string(rownames(se)),
SE = as.vector(se[, 2])
Parameter = .remove_backticks_from_string(rownames(se_standard)),
SE = se
)
}

Expand Down Expand Up @@ -106,10 +135,10 @@ standard_error.pgmm <- function(model, component = c("conditional", "all"), ...)

#' @export
ci.pgmm <- function(x, ci = 0.95, dof = Inf, method = NULL, component = "conditional", ...) {
if (!is.null(method)) {
method <- tolower(method)
} else {
if (is.null(method)) {
method <- "wald"
} else {
method <- tolower(method)
}

.ci_generic(model = x, ci = ci, dof = dof, method = method, component = component)
Expand Down
1 change: 1 addition & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ DoFs
Dom
Dupont
EFA
EGAnet
ESS
Eingenvalues
Eivenvalues
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/_snaps/glmer.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
period [2] | -0.98 | 0.30 | [-1.57, -0.38] | -3.22 | 0.001
period [3] | -1.11 | 0.32 | [-1.75, -0.48] | -3.43 | < .001
period [4] | -1.56 | 0.42 | [-2.39, -0.73] | -3.67 | < .001
Message <simpleMessage>
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a Wald z-distribution approximation.
Expand All @@ -38,7 +38,7 @@
Parameter | Coefficient
----------------------------------
SD (Intercept: herd) | 0.64
Message <simpleMessage>
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a Wald z-distribution approximation.
Expand All @@ -62,7 +62,7 @@
Parameter | Coefficient | SE | 95% CI
--------------------------------------------------------
SD (Intercept: herd) | 0.64 | 0.18 | [0.37, 1.11]
Message <simpleMessage>
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a Wald z-distribution approximation.
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/glmmTMB-profile_CI.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
SD (Days: Subject) | 5.72 | [ 3.81, 8.59]
Cor (Intercept~Days: Subject) | 0.08 | [-0.49, 0.59]
SD (Residual) | 25.59 | [22.80, 28.72]
Message <simpleMessage>
Message
Uncertainty intervals (profile-likelihood) and p-values (two-tailed)
computed using a Wald z-distribution approximation. Uncertainty
Expand All @@ -45,7 +45,7 @@
SD (Days: Subject) | 5.72 | [ 3.81, 8.59]
Cor (Intercept~Days: Subject) | 0.08 | [-0.49, 0.59]
SD (Residual) | 25.59 | [22.80, 28.72]
Message <simpleMessage>
Message
Uncertainty intervals (profile-likelihood) and p-values (two-tailed)
computed using a Wald z-distribution approximation. Uncertainty
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/ivreg.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
(Intercept) | 9.89 | 1.06 | [ 7.76, 12.03] | 9.35 | < .001
rprice [log] | -1.28 | 0.26 | [-1.81, -0.75] | -4.85 | < .001
rincome [log] | 0.28 | 0.24 | [-0.20, 0.76] | 1.18 | 0.246
Message <simpleMessage>
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a Wald t-distribution approximation.
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/mipo.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
(Intercept) | -92.86 | 67.62 | [-232.47, 46.75] | -1.37 | 23.87 | 0.182
disp | 0.26 | 0.40 | [ -0.57, 1.09] | 0.64 | 23.53 | 0.528
hp | 0.43 | 0.63 | [ -0.88, 1.73] | 0.67 | 24.15 | 0.507
Message <simpleMessage>
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a Wald distribution approximation.
Expand All @@ -35,7 +35,7 @@
(Intercept) | 30.65 | 1.43 | [27.72, 33.58] | 21.46 | 26.96 | < .001
disp | -0.03 | 7.52e-03 | [-0.05, -0.02] | -4.40 | 25.57 | < .001
hp | -0.02 | 0.01 | [-0.05, 0.01] | -1.37 | 24.82 | 0.182
Message <simpleMessage>
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a Wald distribution approximation.
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/_snaps/model_parameters.fixest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# model_parameters.fixest

Code
model_parameters(m1, summary = TRUE, verbose = FALSE)
Output
# Fixed Effects
Parameter | Coefficient | SE | 95% CI | t(187) | p
-----------------------------------------------------------------
time | 1.09 | 0.67 | [-0.23, 2.41] | 1.63 | 0.106
phq4 | -3.66 | 0.67 | [-4.98, -2.34] | -5.45 | < .001
Model: QoL ~ time + phq4 (564 Observations)
Residual standard deviation: 12.365 (df = 561)
r2: 0.743; ar2: 0.613; wr2: 0.180; war2: 0.175

6 changes: 3 additions & 3 deletions tests/testthat/_snaps/model_parameters.fixest_multi.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
x | 0.03 | 0.04 | [-0.15, 0.22] | 0.81 | 0.502
Petal.Length | -0.04 | 0.19 | [-0.85, 0.78] | -0.20 | 0.858
Sepal.Length | 0.37 | 0.20 | [-0.49, 1.23] | 1.86 | 0.205
Message <simpleMessage>
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a Wald t-distribution approximation.
Expand Down Expand Up @@ -99,7 +99,7 @@
----------------------------------------------------------------
x | 0.04 | 0.06 | [-0.22, 0.31] | 0.71 | 0.553
Petal Length | 0.30 | 0.06 | [ 0.05, 0.54] | 5.15 | 0.036
Message <simpleMessage>
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a Wald t-distribution approximation.
Expand Down Expand Up @@ -140,7 +140,7 @@
x | 0.01 | 0.02 | [-0.07, 0.10] | 0.74 | 0.539
Petal.Length | 0.23 | 0.08 | [-0.11, 0.58] | 2.93 | 0.099
Sepal.Length | -4.24e-03 | 0.03 | [-0.14, 0.13] | -0.13 | 0.906
Message <simpleMessage>
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a Wald t-distribution approximation.
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/model_parameters.glm.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
---------------------------------------------------------------------------
(Intercept) | 245.48 | 46.72 | [173.66, 351.67] | 28.92 | < .001
u [log] | 0.55 | 0.03 | [ 0.49, 0.61] | -10.88 | < .001
Message <simpleMessage>
Message
Uncertainty intervals (profile-likelihood) and p-values (two-tailed)
computed using a Wald t-distribution approximation.
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/model_parameters.mclogit.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
--------------------------------------------------------------
distance | -1.44 | 0.05 | [-1.54, -1.34] | -27.07 | < .001
cost | -0.98 | 0.04 | [-1.06, -0.90] | -24.52 | < .001
Message <simpleMessage>
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a Wald z-distribution approximation.
Expand Down Expand Up @@ -40,7 +40,7 @@
TypeAtrium | -0.41 | 0.21 | [-0.82, 0.01] | -1.93 | 0.054
TypeTerrace | -1.41 | 0.20 | [-1.80, -1.02] | -7.06 | < .001
ContHigh | 0.48 | 0.12 | [ 0.24, 0.73] | 3.88 | < .001
Message <simpleMessage>
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a Wald z-distribution approximation.
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/_snaps/model_parameters.mixed.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Parameter | Coefficient | SE | 95% CI | t(558) | p
--------------------------------------------------------------------
phq4 between | -6.28 | 0.50 | [-7.27, -5.30] | -12.53 | < .001
Message <simpleMessage>
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a Wald t-distribution approximation.
Expand All @@ -42,7 +42,7 @@
--------------------------------------------------------
SD (Intercept: gear) | 0.27 | 0.24 | [0.05, 1.54]
SD (Residual) | 0.59 | 0.08 | [0.46, 0.77]
Message <simpleMessage>
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a Wald t-distribution approximation. Uncertainty intervals for
Expand All @@ -64,7 +64,7 @@
Model: wt ~ cyl (32 Observations)
Residual standard deviation: 0.594 (df = 28)
Conditional R2: 0.628; Marginal R2: 0.550
Message <simpleMessage>
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a Wald t-distribution approximation.
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/model_parameters_ordinal.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
Parameter | Estimate | SE | 95% CI | z | p
------------------------------------------------------------
Stim [Old] | -0.04 | 0.04 | [0.47, 0.63] | 13.64 | < .001
Message <simpleMessage>
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a Wald z-distribution approximation.
Expand Down Expand Up @@ -55,7 +55,7 @@
Parameter | Estimate | SE | 95% CI | z | p
------------------------------------------------------------
Stim [Old] | -0.04 | 0.04 | [-0.12, 0.04] | -1.11 | 0.268
Message <simpleMessage>
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a Wald z-distribution approximation.
Expand Down
34 changes: 34 additions & 0 deletions tests/testthat/_snaps/plm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# vcov standard errors

Code
print(model_parameters(ran))
Output
# Fixed Effects
Parameter | Coefficient | SE | 95% CI | z | df | p
-----------------------------------------------------------------------------
(Intercept) | 817.10 | 188.26 | [445.84, 1188.36] | 4.34 | 197 | < .001
capital | -0.58 | 0.15 | [ -0.88, -0.29] | -3.95 | 197 | < .001
inv | 2.92 | 0.30 | [ 2.33, 3.51] | 9.80 | 197 | < .001
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a Wald z-distribution approximation.

---

Code
print(model_parameters(ran, vcov = "HC1"))
Output
# Fixed Effects
Parameter | Coefficient | SE | 95% CI | z | df | p
----------------------------------------------------------------------------
(Intercept) | 817.10 | 274.75 | [275.28, 1358.92] | 2.97 | 197 | 0.003
capital | -0.58 | 0.43 | [ -1.43, 0.26] | -1.37 | 197 | 0.173
inv | 2.92 | 0.89 | [ 1.16, 4.67] | 3.28 | 197 | 0.001
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a Wald z-distribution approximation.

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/pool_parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
age [60-99] | -6.71 | 3.84 | [-17.75, 4.34] | -1.75 | 3.67 | 0.162
hyp [yes] | 1.52 | 2.26 | [ -3.49, 6.54] | 0.67 | 10.26 | 0.515
chl | 0.06 | 0.03 | [ -0.01, 0.12] | 2.11 | 6.44 | 0.077
Message <simpleMessage>
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a Wald distribution approximation.
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/_snaps/printing-stan.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
Parameter | Mean | 95% CI | pd | Rhat | ESS
--------------------------------------------------------
sigma | 2.67 | [2.06, 3.51] | 100% | 1.000 | 2390.00
Message <simpleMessage>
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a MCMC distribution approximation.
Expand Down Expand Up @@ -47,7 +47,7 @@
SD (Intercept: gear) | 3.88 | [ 0.21, 10.30] | 100% | 1.010 | 424.00
SD (wt: gear) | 1.96 | [ 0.06, 5.06] | 100% | 1.385 | 9.00
Cor (Intercept~wt: gear) | -0.25 | [-0.99, 0.83] | 62.48% | 1.106 | 36.00
Message <simpleMessage>
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a MCMC distribution approximation.
Expand Down Expand Up @@ -77,7 +77,7 @@
SD (Intercept: Subject) | 26.63 | [15.46, 42.36] | 100% | 1.002 | 1823.00
SD (Days: Subject) | 6.58 | [ 4.12, 10.16] | 100% | 1.000 | 1228.00
Cor (Intercept~Days: Subject) | 0.09 | [-0.47, 0.67] | 60.42% | 1.003 | 899.00
Message <simpleMessage>
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a MCMC distribution approximation.
Expand Down Expand Up @@ -107,7 +107,7 @@
SD (Intercept: grp) | 8.22 | [ 0.44, 25.69] | 100% | 1.000 | 1604.00
SD (Intercept: grp:subgrp) | 7.41 | [ 0.44, 16.87] | 100% | 1.003 | 770.00
SD (Intercept: Subject) | 38.51 | [26.89, 55.98] | 100% | 1.003 | 1254.00
Message <simpleMessage>
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a MCMC distribution approximation.
Expand Down Expand Up @@ -135,7 +135,7 @@
Parameter | Mean | 95% CI | pd | Rhat | ESS
---------------------------------------------------------------------
SD (Intercept: Species) | 1.68 | [0.64, 3.64] | 100% | 1.003 | 796.00
Message <simpleMessage>
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a MCMC distribution approximation.
Expand Down Expand Up @@ -166,7 +166,7 @@
SD (Intercept: gear) | 3.76 | [ 0.14, 10.13] | 100% | 1.015 | 643.00
SD (wt: gear) | 1.47 | [ 0.06, 3.96] | 100% | 1.039 | 94.00
Cor (Intercept~wt: gear) | -0.38 | [-0.99, 0.82] | 76.85% | 1.003 | 854.00
Message <simpleMessage>
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a MCMC distribution approximation.
Expand Down Expand Up @@ -236,7 +236,7 @@
Intercept[1] | -38.42 | [-67.76, -19.66] | 100% | 1.002 | 992.00
Intercept[2] | -33.26 | [-59.09, -16.53] | 100% | 1.001 | 1039.00
mpg | -1.80 | [ -3.20, -0.90] | 100% | 1.002 | 1021.00
Message <simpleMessage>
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a MCMC distribution approximation.
Expand Down
Loading

0 comments on commit 21dcad5

Please sign in to comment.