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 b473351
Showing 1 changed file with 7 additions and 7 deletions.
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 b473351

Please sign in to comment.