Skip to content

Commit 29419f3

Browse files
authored
Merge branch 'main' into use_check_arg
2 parents 965a1fe + 2ad0527 commit 29419f3

12 files changed

+207
-170
lines changed

R/4_standard_error.R

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,27 @@
1010
#' to the corresponding numeric value). By default, `NA` is returned for
1111
#' factors or character vectors.
1212
#' @param vcov Variance-covariance matrix used to compute uncertainty estimates
13-
#' (e.g., for robust standard errors). This argument accepts a covariance matrix,
14-
#' a function which returns a covariance matrix, or a string which identifies
15-
#' the function to be used to compute the covariance matrix.
13+
#' (e.g., for robust standard errors). This argument accepts a covariance
14+
#' matrix, a function which returns a covariance matrix, or a string which
15+
#' identifies the function to be used to compute the covariance matrix.
1616
#' * A covariance matrix
1717
#' * A function which returns a covariance matrix (e.g., `stats::vcov()`)
1818
#' * A string which indicates the kind of uncertainty estimates to return.
19-
#' - Heteroskedasticity-consistent: `"vcovHC"`, `"HC"`, `"HC0"`, `"HC1"`,
20-
#' `"HC2"`, `"HC3"`, `"HC4"`, `"HC4m"`, `"HC5"`. See `?sandwich::vcovHC`.
21-
#' - Cluster-robust: `"vcovCR"`, `"CR0"`, `"CR1"`, `"CR1p"`, `"CR1S"`,
22-
#' `"CR2"`, `"CR3"`. See `?clubSandwich::vcovCR`.
23-
#' - Bootstrap: `"vcovBS"`, `"xy"`, `"residual"`, `"wild"`, `"mammen"`,
24-
#' `"webb"`. See `?sandwich::vcovBS`.
25-
#' - Other `sandwich` package functions: `"vcovHAC"`, `"vcovPC"`, `"vcovCL"`,
26-
#' `"vcovPL"`.
19+
#' - Heteroskedasticity-consistent: `"HC"`, `"HC0"`, `"HC1"`, `"HC2"`,
20+
#' `"HC3"`, `"HC4"`, `"HC4m"`, `"HC5"`. See `?sandwich::vcovHC`
21+
#' - Cluster-robust: `"CR"`, `"CR0"`, `"CR1"`, `"CR1p"`, `"CR1S"`,
22+
#' `"CR2"`, `"CR3"`. See `?clubSandwich::vcovCR`
23+
#' - Bootstrap: `"BS"`, `"xy"`, `"residual"`, `"wild"`, `"mammen"`,
24+
#' `"fractional"`, `"jackknife"`, `"norm"`, `"webb"`.
25+
#' See `?sandwich::vcovBS`
26+
#' - Other `sandwich` package functions: `"HAC"`, `"PC"`, `"vCL"`, `"PL"`.
2727
#' @param vcov_args List of arguments to be passed to the function identified by
28-
#' the `vcov` argument. This function is typically supplied by the **sandwich**
29-
#' or **clubSandwich** packages. Please refer to their documentation (e.g.,
30-
#' `?sandwich::vcovHAC`) to see the list of available arguments.
28+
#' the `vcov` argument. This function is typically supplied by the
29+
#' **sandwich** or **clubSandwich** packages. Please refer to their
30+
#' documentation (e.g., `?sandwich::vcovHAC`) to see the list of available
31+
#' arguments. If no estimation type (argument `type`) is given, the default
32+
#' type for `"HC"` equals the default from the **sandwich** package; for type
33+
#' `"CR"`, the default is set to `"CR3"`.
3134
#' @param effects Should standard errors for fixed effects (`"fixed"`), random
3235
#' effects (`"random"`), or both (`"all"`) be returned? Only applies
3336
#' to mixed models. May be abbreviated. When standard errors for random

WIP/model_parameters_robust.Rmd

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Robust Estimation of Standard Errors, Confidence Intervals and p-values"
3-
output:
3+
output:
44
github_document:
55
toc: true
66
fig_width: 10.08
@@ -14,7 +14,7 @@ vignette: >
1414
%\VignetteIndexEntry{Robust Estimation of Standard Errors}
1515
\usepackage[utf8]{inputenc}
1616
%\VignetteEngine{knitr::rmarkdown}
17-
editor_options:
17+
editor_options:
1818
chunk_output_type: console
1919
bibliography: bibliography.bib
2020
---
@@ -40,13 +40,26 @@ if (!requireNamespace("poorman", quietly = TRUE) ||
4040
set.seed(333)
4141
```
4242

43-
The [`model_parameters()`](https://easystats.github.io/parameters/articles/model_parameters.html) function also allows the computation of standard errors, confidence intervals and p-values based on robust covariance matrix estimation from model parameters. Robust estimation is based on the packages **sandwich** and **clubSandwich**, so all models supported by either of these packages work with `model_parameters()` when `robust = TRUE`.
43+
The [`model_parameters()`](https://easystats.github.io/parameters/articles/model_parameters.html) function also allows the computation of standard errors, confidence intervals and p-values based on robust covariance matrix estimation from model parameters. Robust estimation is based on the packages **sandwich** and **clubSandwich**, so all models supported by either of these packages work with `model_parameters()`.
44+
45+
There are two arguments responsible to define the heteroscedasticity-consistent covariance matrix, that should be used for robust estimation: `vcov` and `vcov_args`.
46+
47+
The `vcov` argument defines the variance-covariance matrix used to compute uncertainty estimates. This argument accepts following inputs:
48+
49+
* A covariance matrix
50+
* A function which returns a covariance matrix (e.g., `stats::vcov()`)
51+
* A string which indicates the kind of uncertainty estimates to return.
52+
- Heteroskedasticity-consistent: `"HC"`, `"HC0"`, `"HC1"`, `"HC2"`, `"HC3"`, `"HC4"`, `"HC4m"`, `"HC5"`. See `?sandwich::vcovHC`
53+
- Cluster-robust: `"CR"`, `"CR0"`, `"CR1"`, `"CR1p"`, `"CR1S"`, `"CR2"`, `"CR3"`. See `?clubSandwich::vcovCR`
54+
- Bootstrap: `"BS"`, `"xy"`, `"residual"`, `"wild"`, `"mammen"`, `"fractional"`, `"jackknife"`, `"norm"`, `"webb"`. See `?sandwich::vcovBS`
55+
- Other `sandwich` package functions: `"HAC"`, `"PC"`, `"vCL"`, `"PL"`.
56+
4457

4558
## Classical Regression Models
4659

4760
### Robust Covariance Matrix Estimation from Model Parameters
4861

49-
By default, when `model_parameters(robust = TRUE)`, it internally calls `sandwich::vcovHC(type = "HC3")`. However, there are three arguments that allow for choosing different methods and options of robust estimation: `vcov_estimation`, `vcov_type` and `vcov_args` (see [`?standard_error_robust`](https://easystats.github.io/parameters/reference/standard_error_robust.html) for further details).
62+
The simplest option, `model_parameters(vcov = "HC)`, internally calls `sandwich::vcovHC(type = "HC3")`. However, `vcov` and `vcov_args` can be used for different options to calculate the heteroscedasticity-consistent covariance matrix (see [`?standard_error_robust`](https://easystats.github.io/parameters/reference/standard_error_robust.html) for further details).
5063

5164
Let us start with a simple example, which uses a heteroskedasticity-consistent covariance matrix estimation with estimation-type "HC3" (i.e. `sandwich::vcovHC(type = "HC3")` is called):
5265

@@ -55,7 +68,7 @@ data(iris)
5568
model <- lm(Petal.Length ~ Sepal.Length * Species + Sepal.Width, data = iris)
5669
5770
# model parameters, where SE, CI and p-values are based on robust estimation
58-
mp <- model_parameters(model, robust = TRUE)
71+
mp <- model_parameters(model, vcov = "HC")
5972
mp
6073
6174
# compare standard errors to result from sandwich-package
@@ -65,42 +78,43 @@ unname(sqrt(diag(sandwich::vcovHC(model))))
6578

6679
### Cluster-Robust Covariance Matrix Estimation (sandwich)
6780

68-
If another covariance matrix estimation is required, use the `vcov_estimation`-argument. This argument needs the suffix for the related `vcov*()`-functions as value, i.e. `vcov_estimation = "CL"` would call `sandwich::vcovCL()`, or `vcov_estimation = "HAC"` would call `sandwich::vcovHAC()`.
81+
The specific estimation type can be either passed as string to `vcov`, or changed with the `type` option that is passed to the `vcov_args` argument:
82+
83+
* `vcov = "HC1"`
84+
* `vcov = "HC", vcov_args = list(type = "HC1")`.
6985

70-
The specific estimation type can be changed with `vcov_type`. E.g., `sandwich::vcovCL()` accepts estimation types HC0 to HC3. In the next example, we use a clustered covariance matrix estimation with HC1-estimation type.
86+
If the `type` is acceptes by different functions, e.g., both `sandwich::vcovHC()` and `sandwich::vcovCL()` accepts estimation types HC0 to HC3, we need the "long" form to specify all relevant options. In the next example, we use a clustered covariance matrix estimation with HC1-estimation type.
7187

7288
```{r}
7389
# change estimation-type
74-
mp <- model_parameters(model, robust = TRUE, vcov_estimation = "CL", vcov_type = "HC1")
90+
mp <- model_parameters(model, vcov = "CL", vcov_args = list(type = "HC2"))
7591
mp
7692
7793
# compare standard errors to result from sandwich-package
7894
mp$SE
79-
unname(sqrt(diag(sandwich::vcovCL(model))))
95+
unname(sqrt(diag(sandwich::vcovCL(model, type = "HC2"))))
8096
```
8197

82-
Usually, clustered covariance matrix estimation is used when there is a cluster-structure in the data. The variable indicating the cluster-structure can be defined in `sandwich::vcovCL()` with the `cluster`-argument. In `model_parameters()`, additional arguments that should be passed down to functions from the **sandwich** package can be specified in `vcov_args`:
98+
Usually, clustered covariance matrix estimation is used when there is a cluster-structure in the data. The variable indicating the cluster-structure can be defined in `sandwich::vcovCL()` with the `cluster`-argument. As mentioned above, in `model_parameters()`, additional arguments that should be passed down to functions from the **sandwich** package can be specified in `vcov_args`:
8399

84100
```{r}
85101
iris$cluster <- factor(rep(LETTERS[1:8], length.out = nrow(iris)))
86102
# change estimation-type, defining additional arguments
87103
mp <- model_parameters(
88104
model,
89-
robust = TRUE,
90-
vcov_estimation = "CL",
91-
vcov_type = "HC1",
92-
vcov_args = list(cluster = iris$cluster)
105+
vcov = "CL",
106+
vcov_args = list(type = "HC2", cluster = iris$cluster)
93107
)
94108
mp
95109
96110
# compare standard errors to result from sandwich-package
97111
mp$SE
98-
unname(sqrt(diag(sandwich::vcovCL(model, cluster = iris$cluster))))
112+
unname(sqrt(diag(sandwich::vcovCL(model, type = "HC2", cluster = iris$cluster))))
99113
```
100114

101115
### Cluster-Robust Covariance Matrix Estimation (clubSandwich)
102116

103-
Cluster-robust estimation of the variance-covariance matrix can also be achieved using `clubSandwich::vcovCR()`. Thus, when `vcov_estimation = "CR"`, the related function from the **clubSandwich** package is called. Note that this function _requires_ the specification of the `cluster`-argument.
117+
Cluster-robust estimation of the variance-covariance matrix can also be achieved using `clubSandwich::vcovCR()`. Thus, when `vcov = "CR"`, the related function from the **clubSandwich** package is called. Note that this function _requires_ the specification of the `cluster`-argument.
104118

105119
```{r}
106120
# create fake-cluster-variable, to demonstrate cluster robust standard errors
@@ -109,10 +123,8 @@ iris$cluster <- factor(rep(LETTERS[1:8], length.out = nrow(iris)))
109123
# cluster-robust estimation
110124
mp <- model_parameters(
111125
model,
112-
robust = TRUE,
113-
vcov_estimation = "CR",
114-
vcov_type = "CR1",
115-
vcov_args = list(cluster = iris$cluster)
126+
vcov = "CR",
127+
vcov_args = list(type = "CR1", cluster = iris$cluster)
116128
)
117129
mp
118130
@@ -128,7 +140,7 @@ Finally, robust estimation can be combined with standardization. However, robust
128140

129141
```{r}
130142
# model parameters, robust estimation on standardized model
131-
model_parameters(model, standardize = "refit", robust = TRUE)
143+
model_parameters(model, standardize = "refit", vcov = "HC")
132144
```
133145

134146

@@ -156,10 +168,8 @@ model_parameters(model)
156168
# model parameters, cluster robust estimation for mixed models
157169
model_parameters(
158170
model,
159-
robust = TRUE,
160-
vcov_estimation = "CR",
161-
vcov_type = "CR1",
162-
vcov_args = list(cluster = iris$grp)
171+
vcov = "CR",
172+
vcov_args = list(type = "CR1", cluster = iris$grp)
163173
)
164174
```
165175

@@ -172,9 +182,7 @@ Again, robust estimation can be combined with standardization for linear mixed m
172182
model_parameters(
173183
model,
174184
standardize = "refit",
175-
robust = TRUE,
176-
vcov_estimation = "CR",
177-
vcov_type = "CR1",
178-
vcov_args = list(cluster = iris$grp)
185+
vcov = "CR",
186+
vcov_args = list(type = "CR1", cluster = iris$grp)
179187
)
180188
```

man/equivalence_test.lm.Rd

Lines changed: 17 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/model_parameters.default.Rd

Lines changed: 17 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/model_parameters.merMod.Rd

Lines changed: 17 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)