|
4 | 4 | # ife |
5 | 5 |
|
6 | 6 | <!-- badges: start --> |
| 7 | + |
| 8 | +[](https://CRAN.R-project.org/package=ife) |
7 | 10 | <!-- badges: end --> |
8 | 11 |
|
9 | | -S7 class (with Ops) for influence function based estimands |
| 12 | +S7 class for influence function estimands with forward mode automatic |
| 13 | +differentiation for variance estimation. |
10 | 14 |
|
11 | 15 | ## Installation |
12 | 16 |
|
13 | | -You can install the development version of ife from |
| 17 | +You can install the development version of *ife* from |
14 | 18 | [GitHub](https://github.com/) with: |
15 | 19 |
|
16 | 20 | ``` r |
17 | 21 | # install.packages("pak") |
18 | 22 | pak::pak("nt-williams/ife") |
19 | 23 | ``` |
20 | 24 |
|
| 25 | +## Example |
| 26 | + |
| 27 | +Consider estimating the population mean outcome under treatment |
| 28 | +$E[E[Y \mid A=1,W]]$ and control $E[E[Y \mid A=0,W]]$ using augmented |
| 29 | +inverse probability weighting (AIPW). |
| 30 | + |
21 | 31 | ``` r |
22 | 32 | library(ife) |
23 | 33 |
|
| 34 | +# Generate simulated data |
24 | 35 | n <- 500 |
25 | | -w <- runif(n) |
26 | | -a <- rbinom(n, 1, 0.5) |
27 | | -y <- rbinom(n, 1, plogis(-0.75 + a + w)) |
| 36 | +w <- runif(n) # confounder |
| 37 | +a <- rbinom(n, 1, 0.5) # treatment (randomized) |
| 38 | +y <- rbinom(n, 1, plogis(-0.75 + a + w)) # outcome |
28 | 39 |
|
| 40 | +# Create data-frames for counterfactual predictions |
29 | 41 | foo <- data.frame(w, a, y) |
30 | 42 | foo1 <- foo0 <- foo |
31 | | -foo1$a <- 1 |
32 | | -foo0$a <- 0 |
| 43 | +foo1$a <- 1 # everyone treated |
| 44 | +foo0$a <- 0 # everyone untreated |
33 | 45 |
|
34 | | -pi <- 0.5 |
| 46 | +# Fit outcome model and generate predictions |
| 47 | +pi <- 0.5 # known propensity score |
35 | 48 | m <- glm(y ~ a + w, data = foo, family = binomial()) |
| 49 | +Qa <- predict(m, type = "response") # predicted outcomes |
| 50 | +Q1 <- predict(m, newdata = foo1, type = "response") # under treatment |
| 51 | +Q0 <- predict(m, newdata = foo0, type = "response") # under control |
36 | 52 |
|
37 | | -Qa <- predict(m, type = "response") |
38 | | -Q1 <- predict(m, newdata = foo1, type = "response") |
39 | | -Q0 <- predict(m, newdata = foo0, type = "response") |
| 53 | +# Calculate un-centered influence functions |
| 54 | +if1 <- a / pi * (y - Qa) + Q1 |
| 55 | +if0 <- (1 - a) / (1 - pi) * (y - Qa) + Q0 |
| 56 | +``` |
40 | 57 |
|
41 | | -if1 <- a / pi * (y - Qa) + Q1 |
42 | | -if0 <- (1 - a) / pi * (y - Qa) + Q0 |
| 58 | +Create *ife* objects for these estimates using |
| 59 | +`influence_func_estimate()` or `ife()`: |
43 | 60 |
|
| 61 | +``` r |
44 | 62 | ife1 <- influence_func_estimate(mean(if1), if1) |
45 | 63 | ife0 <- ife(mean(if0), if0) |
| 64 | +``` |
46 | 65 |
|
| 66 | +*ife* then allows you to estimate contrasts between estimates, with |
| 67 | +variance estimated using automatic differentiation. The additive effect |
| 68 | +(risk difference) can be calculated as: |
| 69 | + |
| 70 | +``` r |
47 | 71 | ife1 - ife0 |
| 72 | +#> Estimate: 0.254 |
| 73 | +#> Std. error: 0.042 |
| 74 | +#> 95% Conf. int.: 0.172, 0.336 |
48 | 75 | ``` |
49 | 76 |
|
50 | | - #> Estimate: 0.15 |
| 77 | +The multiplicative effect (risk ratio) can be estimated as: |
51 | 78 |
|
52 | | - #> Std. error: 0.04 |
53 | | - #> 95% Conf. int.: 0.07, 0.24 |
54 | | - ife1 / ife0 |
| 79 | +``` r |
| 80 | +ife1 / ife0 |
| 81 | +#> Estimate: 1.583 |
| 82 | +#> Std. error: 0.129 |
| 83 | +#> 95% Conf. int.: 1.33, 1.837 |
| 84 | +``` |
55 | 85 |
|
56 | | - #> Estimate: 1.32 |
| 86 | +For the risk ratio, which is strictly positive, you can estimate the |
| 87 | +effect on the log scale and exponentiate the confidence intervals to |
| 88 | +ensure the lower bound is always positive: |
57 | 89 |
|
58 | | - #> Std. error: 0.11 |
59 | | - #> 95% Conf. int.: 1.11, 1.52 |
| 90 | +``` r |
| 91 | +exp(log(ife1 / ife0)@conf_int) |
| 92 | +#> [1] 1.35 1.86 |
| 93 | +``` |
0 commit comments