Skip to content

Commit f84b07f

Browse files
Initial commit
0 parents  commit f84b07f

File tree

130 files changed

+7062
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+7062
-0
lines changed

.DS_Store

10 KB
Binary file not shown.

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData

README.Rmd

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
output: github_document
3+
---
4+
5+
<!-- README.md is generated from README.Rmd. Please edit that file -->
6+
7+
```{r, include = FALSE}
8+
knitr::opts_chunk$set(
9+
collapse = TRUE,
10+
comment = "#>"
11+
)
12+
```
13+
14+
## user2020! Causal Inference in R Workshop
15+
16+
### Slides
17+
* [00 Intro](https://user2020.lucymcgowan.com/00-intro.html)
18+
* [01 Whole Game](https://user2020.lucymcgowan.com/01-causal_modeling_whole_game.html)
19+
* [02 Causal Diagrams](https://user2020.lucymcgowan.com/02-dags.html)
20+
* [03 Introduction to Propensity Scores](https://user2020.lucymcgowan.com/03-pscores.html)
21+
* [04 Using Propensity Scores](https://user2020.lucymcgowan.com/04-pscore-weighting.html)
22+
* [05 Checking Propensity Scores](https://user2020.lucymcgowan.com/05-pscore-diagnostics.html)
23+
* [06 Fitting the outcome model](https://user2020.lucymcgowan.com/06-outcome-model.html)
24+
25+
### Installing materials locally
26+
27+
We will be using RStudio Cloud for the workshop, but if you would like to install the required packages and course materials, we have an R package called {[useRcausal2020](https://github.com/malcolmbarrett/useRcausal2020)} to help you do that! You can install {[useRcausal2020](https://github.com/malcolmbarrett/useRcausal2020)} from GitHub with:
28+
29+
``` r
30+
install.packages("remotes")
31+
remotes::install_github("malcolmbarrett/useRcausal2020")
32+
```
33+
34+
Once you've installed the package, install the workshop with
35+
36+
``` r
37+
useRcausal2020::install_workshop("path/to/your/computer")
38+
```
39+
40+
Replace "path/to/your/computer" with where on your computer you want the workshop installed.

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
<!-- README.md is generated from README.Rmd. Please edit that file -->
3+
4+
## user2020\! Causal Inference in R Workshop
5+
6+
### Slides
7+
8+
- [00 Intro](https://user2020.lucymcgowan.com/00-intro.html)
9+
- [01 Whole
10+
Game](https://user2020.lucymcgowan.com/01-causal_modeling_whole_game.html)
11+
- [02 Causal Diagrams](https://user2020.lucymcgowan.com/02-dags.html)
12+
- [03 Introduction to Propensity
13+
Scores](https://user2020.lucymcgowan.com/03-pscores.html)
14+
- [04 Using Propensity
15+
Scores](https://user2020.lucymcgowan.com/04-pscore-weighting.html)
16+
- [05 Checking Propensity
17+
Scores](https://user2020.lucymcgowan.com/05-pscore-diagnostics.html)
18+
- [06 Fitting the outcome
19+
model](https://user2020.lucymcgowan.com/06-outcome-model.html)
20+
21+
### Installing materials locally
22+
23+
We will be using RStudio Cloud for the workshop, but if you would like
24+
to install the required packages and course materials, we have an R
25+
package called
26+
{[useRcausal2020](https://github.com/malcolmbarrett/useRcausal2020)} to
27+
help you do that\! You can install
28+
{[useRcausal2020](https://github.com/malcolmbarrett/useRcausal2020)}
29+
from GitHub with:
30+
31+
``` r
32+
install.packages("remotes")
33+
remotes::install_github("malcolmbarrett/useRcausal2020")
34+
```
35+
36+
Once you’ve installed the package, install the workshop with
37+
38+
``` r
39+
useRcausal2020::install_workshop("path/to/your/computer")
40+
```
41+
42+
Replace “path/to/your/computer” with where on your computer you want the
43+
workshop installed.

causal_inference_r_workshop.Rproj

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Version: 1.0
2+
3+
RestoreWorkspace: No
4+
SaveWorkspace: No
5+
AlwaysSaveHistory: Default
6+
7+
EnableCodeIndexing: Yes
8+
UseSpacesForTab: Yes
9+
NumSpacesForTab: 2
10+
Encoding: UTF-8
11+
12+
RnwWeave: Sweave
13+
LaTeX: pdfLaTeX
14+
15+
AutoAppendNewline: Yes
16+
StripTrailingWhitespace: Yes
17+
LineEndingConversion: Posix
18+
19+
BuildType: Package
20+
PackageUseDevtools: Yes
21+
PackageInstallArgs: --no-multiarch --with-keep.source
22+
PackageRoxygenize: rd,collate,namespace

code/smd-plot.R

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
library(survey)
2+
library(tableone)
3+
library(tidyverse)
4+
library(broom)
5+
# remotes::install_github("malcolmbarrett/cidata")
6+
library(cidata)
7+
8+
propensity_model <- glm(
9+
qsmk ~ sex +
10+
race + age + I(age^2) + education +
11+
smokeintensity + I(smokeintensity^2) +
12+
smokeyrs + I(smokeyrs^2) + exercise + active +
13+
wt71 + I(wt71^2),
14+
family = binomial(),
15+
data = nhefs_complete
16+
)
17+
18+
df <- propensity_model %>%
19+
augment(type.predict = "response", data = nhefs_complete) %>%
20+
mutate(wts = 1 / ifelse(qsmk == 0, 1 - .fitted, .fitted))
21+
22+
svy_des <- svydesign(
23+
ids = ~ 1,
24+
data = df,
25+
weights = ~ wts)
26+
27+
smd_table_unweighted <- CreateTableOne(
28+
vars = c("sex", "race", "age", "education", "smokeintensity", "smokeyrs",
29+
"exercise", "active", "wt71"),
30+
strata = "qsmk",
31+
data = df,
32+
test = FALSE)
33+
34+
smd_table <- svyCreateTableOne(
35+
vars = c("sex", "race", "age", "education", "smokeintensity", "smokeyrs",
36+
"exercise", "active", "wt71"),
37+
strata = "qsmk",
38+
data = svy_des,
39+
test = FALSE)
40+
41+
42+
plot_df <- data.frame(
43+
var = rownames(ExtractSmd(smd_table)),
44+
Unadjusted = as.numeric(ExtractSmd(smd_table_unweighted)),
45+
Weighted = as.numeric(ExtractSmd(smd_table))) %>%
46+
pivot_longer(-var, names_to = "Method", values_to = "SMD")
47+
48+
ggplot(
49+
data = plot_df,
50+
mapping = aes(x = var, y = SMD, group = Method, color = Method)
51+
) +
52+
geom_line() +
53+
geom_point() +
54+
geom_hline(yintercept = 0.1, color = "black", size = 0.1) +
55+
coord_flip()

0 commit comments

Comments
 (0)