You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: WIP/model_parameters_robust.Rmd
+37-29Lines changed: 37 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
title: "Robust Estimation of Standard Errors, Confidence Intervals and p-values"
3
-
output:
3
+
output:
4
4
github_document:
5
5
toc: true
6
6
fig_width: 10.08
@@ -14,7 +14,7 @@ vignette: >
14
14
%\VignetteIndexEntry{Robust Estimation of Standard Errors}
15
15
\usepackage[utf8]{inputenc}
16
16
%\VignetteEngine{knitr::rmarkdown}
17
-
editor_options:
17
+
editor_options:
18
18
chunk_output_type: console
19
19
bibliography: bibliography.bib
20
20
---
@@ -40,13 +40,26 @@ if (!requireNamespace("poorman", quietly = TRUE) ||
40
40
set.seed(333)
41
41
```
42
42
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.
- Other `sandwich` package functions: `"HAC"`, `"PC"`, `"vCL"`, `"PL"`.
56
+
44
57
45
58
## Classical Regression Models
46
59
47
60
### Robust Covariance Matrix Estimation from Model Parameters
48
61
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).
50
63
51
64
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):
52
65
@@ -55,7 +68,7 @@ data(iris)
55
68
model <- lm(Petal.Length ~ Sepal.Length * Species + Sepal.Width, data = iris)
56
69
57
70
# 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")
59
72
mp
60
73
61
74
# compare standard errors to result from sandwich-package
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")`.
69
85
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.
# compare standard errors to result from sandwich-package
78
94
mp$SE
79
-
unname(sqrt(diag(sandwich::vcovCL(model))))
95
+
unname(sqrt(diag(sandwich::vcovCL(model, type = "HC2"))))
80
96
```
81
97
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`:
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.
104
118
105
119
```{r}
106
120
# create fake-cluster-variable, to demonstrate cluster robust standard errors
0 commit comments