-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
107 lines (80 loc) · 4.31 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
---
output: github_document
bibliography: vignettes/chantrics.bib
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# chantrics
<!-- badges: start -->
![R-CMD-check](https://github.com/tbruckbauer/chantrics/workflows/R-CMD-check/badge.svg?branch=master)
[![codecov](https://codecov.io/gh/tbruckbauer/chantrics/branch/master/graph/badge.svg?token=EQQ177WODM)](https://codecov.io/gh/tbruckbauer/chantrics)
![pkgdown](https://github.com/tbruckbauer/chantrics/workflows/pkgdown/badge.svg)
[![CodeFactor](https://www.codefactor.io/repository/github/tbruckbauer/chantrics/badge?s=8292aacb6a947f6280b202c4f29e21e4510dce53)](https://www.codefactor.io/repository/github/tbruckbauer/chantrics)
<!-- badges: end -->
`chantrics` adjusts the loglikelihood of common econometric models for clustered data based on the estimation process suggested in @chanbate07, using the [chandwich](https://cran.r-project.org/package=chandwich) package, and provides convenience functions for inference on the adjusted models. `adj_loglik()` adjusts the parameter covariance matrix of the models to incorporate clustered data, and can mitigate model misspecification by wrapping `chandwich::adjust_loglik` for the supported models.
The returned model of class `chantrics` can be plugged into standard model evaluation and model comparison methods, for example `summary()`, `confint()` and `anova()`, and a hypothesis test framework provided by `alrtest()`.
## Installation
You can install the released version of chantrics from [CRAN](https://CRAN.R-project.org) with:
```{r eval=FALSE}
install.packages("chantrics")
```
The development version is available from [GitHub](https://github.com/) with:
```{r eval=FALSE}
## Remove the # if "devtools" is not installed yet.
# install.packages("devtools")
devtools::install_github("tbruckbauer/chantrics")
```
## Usage
This example is using the misspecified count data regression from Chapter 5.1 in the Object-Oriented Computation of Sandwich Estimators vignette from the *sandwich* package [@zeileis06]. First, data from a negative binomial model is generated, and then a Poisson model is fit, which is clearly misspecified.
```{r data}
library(chantrics)
set.seed(123)
x <- rnorm(250)
y <- rnbinom(250, mu = exp(1 + x), size = 1)
## Fit the Poisson glm model, which is not correctly specified
fm_pois <- glm(y ~ x + I(x ^ 2), family = poisson)
lmtest::coeftest(fm_pois)
# The I(x^2) term is spuriously significant.
```
In order to mitigate the misspecification, the loglikelihood adjustment is applied to the model object using `adj_loglik(model_object)`. If clustered data is available, a vector or factor indicating from which cluster the observation originates can be passed into the function using `cluster`. If it is not supplied, it is assumed that each observation originates from its own cluster and is independent. More information on this can be found in the [clustering vignette](https://chantrics.theobruckbauer.eu/articles/clustering-vignette.html).
```{r demo}
## Apply the loglikelihood adjustment to the model
fm_pois_adj <- adj_loglik(fm_pois)
summary(fm_pois_adj)
lmtest::coeftest(fm_pois_adj)
# The I(x^2) term is no longer significant.
# Produce confidence intervals for the parameter estimates
chandwich::conf_intervals(fm_pois_adj)
# Generate a nested model from fm_pois_adj
fm_pois_small_adj <- update(fm_pois_adj, . ~ . - I(x^2))
lmtest::coeftest(fm_pois_small_adj)
# Perform an adjusted likelihood ratio test on the two models
anova(fm_pois_adj, fm_pois_small_adj)
# Similarly, specify to remove I(x^2) from the larger model
alrtest(fm_pois_adj, "I(x^2)")
# Plot confidence regions for the parameter estimates
fm_pois_adj_vert <-
chandwich::conf_region(fm_pois_adj, which_pars = c("x", "I(x^2)"))
fm_pois_adj_none <-
chandwich::conf_region(fm_pois_adj,
which_pars = c("x", "I(x^2)"),
type = "none"
)
plot(
fm_pois_adj_vert,
fm_pois_adj_none,
conf = c(60, 80, 90, 95),
col = c("brown", "darkgreen"),
lty = c(1, 2),
lwd = 2.5
)
```
More information on the different methods can be found in the [Introducing chantrics vignette](https://chantrics.theobruckbauer.eu/articles/chantrics-vignette.html), and in the corresponding help pages.
## References