-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.Rmd
More file actions
90 lines (66 loc) · 2.46 KB
/
Copy pathREADME.Rmd
File metadata and controls
90 lines (66 loc) · 2.46 KB
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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
options(digits = 3)
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# ife
<!-- badges: start -->
[](https://CRAN.R-project.org/package=ife)
<!-- badges: end -->
S7 class for influence function estimands with forward mode automatic differentiation for variance estimation.
## Installation
You can install the development version of *ife* from [GitHub](https://github.com/) with:
``` r
# install.packages("pak")
pak::pak("nt-williams/ife")
```
## Example
Consider estimating the population mean outcome under treatment $E[E[Y \mid A=1,W]]$ and control $E[E[Y \mid A=0,W]]$ using augmented inverse probability weighting (AIPW).
```{r example}
library(ife)
set.seed(7654)
# Generate simulated data
n <- 500
w <- runif(n) # confounder
a <- rbinom(n, 1, 0.5) # treatment (randomized)
y <- rbinom(n, 1, plogis(-0.75 + a + w)) # outcome
# Create data-frames for counterfactual predictions
foo <- data.frame(w, a, y)
foo1 <- foo0 <- foo
foo1$a <- 1 # everyone treated
foo0$a <- 0 # everyone untreated
# Fit outcome model and generate predictions
pi <- 0.5 # known propensity score
m <- glm(y ~ a + w, data = foo, family = binomial())
Qa <- predict(m, type = "response") # predicted outcomes
Q1 <- predict(m, newdata = foo1, type = "response") # under treatment
Q0 <- predict(m, newdata = foo0, type = "response") # under control
# Calculate un-centered influence functions
if1 <- a / pi * (y - Qa) + Q1
if0 <- (1 - a) / (1 - pi) * (y - Qa) + Q0
```
Create *ife* objects for these estimates using `influence_func_estimate()` or `ife()`:
```{r}
ife1 <- influence_func_estimate(mean(if1), if1)
ife0 <- ife(mean(if0), if0)
```
*ife* then allows you to estimate contrasts between estimates, with variance estimated using automatic differentiation. The additive effect (risk difference) can be calculated as:
```{r riskdiff}
ife1 - ife0
```
The multiplicative effect (risk ratio) can be estimated as:
```{r}
ife1 / ife0
```
For the risk ratio, which is strictly positive, you can estimate the effect on the log scale and exponentiate the confidence intervals to ensure the lower bound is always positive:
```{r}
exp(log(ife1 / ife0)@conf_int)
```