Skip to content

Commit

Permalink
Improved conditional evaluation of vignette
Browse files Browse the repository at this point in the history
  • Loading branch information
ngreifer committed Jul 27, 2024
1 parent 62047e4 commit a3ca5df
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
7 changes: 4 additions & 3 deletions vignettes/WeightIt.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ link-citations: true
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, eval=T)
knitr::opts_chunk$set(echo = TRUE, eval=TRUE)
options(width = 200, digits= 4)
me_ok <- requireNamespace("marginaleffects", quietly = TRUE)
Expand Down Expand Up @@ -86,12 +86,13 @@ Indeed, we have achieved perfect balance on the means of the covariates. However

Now that we have our weights stored in `W.out`, let's extract them and estimate our treatment effect. The functions `lm_weightit()` and `glm_weightit()` make it easy to fit (generalized) linear models that account for estimation of of the weights in their standard errors. We can then use functions in `marginaleffects` to perform g-computation to extract a treatment effect estimation from the outcome model.

```{r, message=FALSE, eval = me_ok}
```{r, message=FALSE}
# Fit outcome model
fit <- lm_weightit(re78 ~ treat * (age + educ + race + married +
nodegree + re74 + re75),
data = lalonde, weightit = W.out)
```
```{r, message=FALSE, eval = me_ok}
# G-computation for the treatment effect
library("marginaleffects")
avg_comparisons(fit, variables = "treat",
Expand Down
14 changes: 7 additions & 7 deletions vignettes/estimating-effects.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ fit <- lm_weightit(Y_C ~ splines::ns(Ac, df = 4) *

Next we use `avg_predictions()` first to compute the expected potential outcome under a representative set of treatment values. We'll examine 31 treatment values from the 10th to 90th percentiles of `Ac` because estimates outside those ranges tend to be imprecise.

```{r}
```{r, eval = me_ok}
#Represenative values of Ac:
values <- with(d, seq(quantile(Ac, .1),
quantile(Ac, .9),
Expand All @@ -452,7 +452,7 @@ Although one can examine the expected potential outcomes, it is often more usefu

[^4]: You can also use `plot_predictions()`, though after requesting the predictions in the prior step it is quicker to use `ggplot()`.

```{r, fig.height=3.5, fig.width=7}
```{r, eval = me_ok, fig.height=3.5, fig.width=7}
library("ggplot2")
ggplot(p, aes(x = Ac)) +
geom_line(aes(y = estimate)) +
Expand All @@ -468,7 +468,7 @@ Another way to characterize the effect of continuous treatments is to examine th

[^5]: You can also use `plot_slopes()`

```{r, fig.height=3.5, fig.width=7}
```{r, eval = me_ok, fig.height=3.5, fig.width=7}
# Estimate the pointwise derivatives at representative
# values of Ac
s <- avg_slopes(fit,
Expand Down Expand Up @@ -520,14 +520,14 @@ fit <- glm_weightit(Y_B ~ A_1 * A_2 * A_3 * (X1_0 + X2_0),

Then, we compute the average expected potential outcomes under each treatment regime using `marginaleffects::avg_predictions()`:

```{r}
```{r, eval = me_ok}
(p <- avg_predictions(fit,
variables = c("A_1", "A_2", "A_3")))
```

We can compare individual predictions using `marginaleffects::hypotheses()`. For example, to compare all treatment histories to just the first treatment history (i.e., in which all units are untreated for all time periods), we can run the following:

```{r}
```{r, eval = me_ok}
hypotheses(p, "reference")
```

Expand Down Expand Up @@ -563,14 +563,14 @@ fit <- lm_weightit(Y_C ~ A * X5 * (X1 + X2 + X3),

To estimate the subgroup ATEs, we can use `avg_comparisons()`, this time specifying the `by` argument to signify that we want treatment effects stratified by the moderator.

```{r}
```{r, eval = me_ok}
avg_comparisons(fit, variables = "A",
by = "X5")
```

We can see that the subgroup mean differences differ from each other, and we can formally test for moderation using another call to `avg_comparisons()`, this time using the `hypothesis` argument to signify that we want to compare effects between subgroups:

```{r}
```{r, eval = me_ok}
avg_comparisons(fit, variables = "A",
by = "X5",
hypothesis = "pairwise")
Expand Down

0 comments on commit a3ca5df

Please sign in to comment.