├── .Rbuildignore ├── .gitignore ├── DESCRIPTION ├── LICENSE ├── LICENSE.md ├── NAMESPACE ├── R ├── install_solutions.R └── install_workshop.R ├── README.Rmd ├── README.md ├── causalworkshop.Rproj ├── data-raw └── net_data.R ├── data ├── net_data.rda └── net_data_full.rda └── man ├── install_solutions.Rd └── install_workshop.Rd /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | ^LICENSE\.md$ 4 | ^README\.Rmd$ 5 | ^data-raw$ 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: causalworkshop 2 | Title: Install Workshop Materials for Causal Inference in R 3 | Version: 0.1.0 4 | Authors@R: c( 5 | person("Malcolm", "Barrett", , "malcolmbarrett@gmail.com", role = c("aut", "cre"), 6 | comment = c(ORCID = "0000-0003-0299-5825")), 7 | person("Lucy", "D'Agostino McGowan", , "lucydagostino@gmail.com", role = "aut", 8 | comment = c(ORCID = "0000-0001-7297-9359")), 9 | person("Andrew", "Heiss", , "andrew@andrewheiss.com", role = "ctb", 10 | comment = "Wrote simulation code to generate `net_data`") 11 | ) 12 | Description: Install workshop materials for the Causal Inference in R. 13 | License: MIT + file LICENSE 14 | URL: https://github.com/malcolmbarrett/causalworkshop 15 | BugReports: https://github.com/malcolmbarrett/causalworkshop/issues 16 | Depends: 17 | R (>= 2.10) 18 | Imports: 19 | broom, 20 | causaldata, 21 | estimatr, 22 | ggdag (>= 0.2.10.9000), 23 | ggokabeito, 24 | gtsummary (>= 1.5.0), 25 | halfmoon (>= 0.1.0), 26 | MatchIt, 27 | optmatch, 28 | propensity (>= 0.0.0.9000), 29 | quartets, 30 | rsample (>= 0.0.5), 31 | smd (>= 0.6.6), 32 | survey, 33 | tidysmd, 34 | tidyverse, 35 | tipr (>= 1.0.1), 36 | touringplans (>= 0.0.1), 37 | usethis (>= 2.1.0) 38 | Remotes: 39 | LucyMcGowan/touringplans, 40 | malcolmbarrett/propensity, 41 | malcolmbarrett/ggdag 42 | Encoding: UTF-8 43 | Language: en-US 44 | LazyData: true 45 | RoxygenNote: 7.1.1 46 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2020 2 | COPYRIGHT HOLDER: Malcolm Barrett 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2020 Malcolm Barrett 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(install_solutions) 4 | export(install_workshop) 5 | -------------------------------------------------------------------------------- /R/install_solutions.R: -------------------------------------------------------------------------------- 1 | #' Install workshop solutions 2 | #' 3 | #' `install_solutions()` will download the solutions to the exercises for the workshop 4 | #' Causal Inference in R on your computer. Then, it will open a new RStudio 5 | #' Project containing the files you'll need. 6 | #' 7 | #' @param destdir The path on your computer where you would like the workshop 8 | #' installed. By default, this will install somewhere conspicuous, like your 9 | #' desktop, although you can tell `install_solutions()` exactly where you want 10 | #' it to download. 11 | #' 12 | #' @export 13 | install_solutions <- function(destdir = getOption("usethis.destdir")) { 14 | usethis::use_course( 15 | "malcolmbarrett/causal_inference_r_workshop_solutions", 16 | destdir = destdir 17 | ) 18 | } 19 | -------------------------------------------------------------------------------- /R/install_workshop.R: -------------------------------------------------------------------------------- 1 | #' Install workshop materials 2 | #' 3 | #' `install_course()` will install the workshop materials for the workshop 4 | #' Causal Inference in R on your computer. Then, it will open a new RStudio 5 | #' Project containing the files you'll need. 6 | #' 7 | #' @param destdir The path on your computer where you would like the workshop 8 | #' installed. By default, this will install somewhere conspicuous, like your 9 | #' desktop, although you can tell `install_workshop()` exactly where you want 10 | #' it to download. 11 | #' 12 | #' @export 13 | install_workshop <- function(destdir = getOption("usethis.destdir")) { 14 | usethis::use_course( 15 | "r-causal/causal_inference_r_workshop", 16 | destdir = destdir 17 | ) 18 | } 19 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: github_document 3 | --- 4 | 5 | 6 | 7 | ```{r, include = FALSE} 8 | knitr::opts_chunk$set( 9 | collapse = TRUE, 10 | comment = "#>", 11 | fig.path = "man/figures/README-", 12 | out.width = "100%" 13 | ) 14 | ``` 15 | 16 | # Install the workshop materials for Causal Inference in R 17 | 18 | 19 | 20 | 21 | ## Installation 22 | 23 | You can install causalworkshop from this repository with 24 | 25 | ``` r 26 | install.packages("pak") 27 | pak::pak("r-causal/causalworkshop") 28 | ``` 29 | 30 | Once you've installed the package, install the workshop with 31 | 32 | ``` r 33 | causalworkshop::install_workshop() 34 | ``` 35 | 36 | By default, this package downloads the materials to a conspicuous place like your Desktop. You can also tell `install_workshop()` exactly where to put the materials: 37 | 38 | ``` r 39 | causalworkshop::install_workshop("a/path/on/your/computer") 40 | ``` 41 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # Install the workshop materials for Causal Inference in R 5 | 6 | 7 | 8 | 9 | ## Installation 10 | 11 | You can install causalworkshop from this repository with 12 | 13 | ``` r 14 | install.packages("pak") 15 | pak::pak("r-causal/causalworkshop") 16 | ``` 17 | 18 | Once you’ve installed the package, install the workshop with 19 | 20 | ``` r 21 | causalworkshop::install_workshop() 22 | ``` 23 | 24 | By default, this package downloads the materials to a conspicuous place 25 | like your Desktop. You can also tell `install_workshop()` exactly where 26 | to put the materials: 27 | 28 | ``` r 29 | causalworkshop::install_workshop("a/path/on/your/computer") 30 | ``` 31 | -------------------------------------------------------------------------------- /causalworkshop.Rproj: -------------------------------------------------------------------------------- 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 23 | -------------------------------------------------------------------------------- /data-raw/net_data.R: -------------------------------------------------------------------------------- 1 | # sourced from https://evalsp21.classes.andrewheiss.com/example/matching-ipw/ 2 | # The true average treatment effect (ATE) is -10 3 | # our thanks to Andrew, who wrote most of this code! 4 | library(tidyverse) 5 | library(scales) 6 | create_nets <- function() { 7 | num <- 1752 8 | 9 | # Create confounder variables that are related to each other 10 | mu <- c(income = 900, temperature = 75, health = 50) 11 | stddev <- c(income = 200, temperature = 10, health = 20) 12 | lower <- c(income = 100, temperature = 60, health = 5) 13 | upper <- c(income = 2000, temperature = 90, health = 100) 14 | 15 | # https://stackoverflow.com/a/46563034/120898 16 | correlations_confounders <- tribble( 17 | ~var1, ~var2, ~correlation, 18 | "income", "temperature", 0.2, 19 | "income", "health", 0.8, 20 | # "temperature", "health", 0.6, 21 | "temperature", "health", 0.2, 22 | ) %>% 23 | mutate_at(vars(starts_with("var")), 24 | ~factor(., levels = c("income", "temperature", "health"))) %>% 25 | xtabs(correlation ~ var1 + var2, ., drop.unused.levels = FALSE) %>% 26 | '+'(., t(.)) %>% 27 | `diag<-`(1) %>% 28 | as.data.frame.matrix() %>% as.matrix() 29 | 30 | # Convert correlation matrix to covariance matrix using fancy math 31 | cov_matrix_confounders <- stddev %*% t(stddev) * correlations_confounders 32 | 33 | # Force the covariance matrix to be positive definite and symmetric 34 | # https://stats.stackexchange.com/q/153166/3025 35 | sigma <- as.matrix(Matrix::nearPD(cov_matrix_confounders)$mat) 36 | 37 | set.seed(1234) 38 | confounders <- tmvtnorm::rtmvnorm(num, mean = mu, sigma = sigma, 39 | lower = lower, upper = upper) %>% 40 | magrittr::set_colnames(names(mu)) %>% as_tibble() %>% 41 | mutate(health = round(health, 0), 42 | temperature = round(temperature, 1)) 43 | 44 | set.seed(1234) 45 | mosquito_nets <- tibble(id = 1:num) %>% 46 | bind_cols(confounders) %>% 47 | mutate(household = rpois(n(), 2) + 1) %>% 48 | mutate(genetic_resistance = rbinom(n(), 1, .1)) %>% 49 | mutate(enrolled = household > 4 & income < 700) %>% 50 | mutate(insecticide_resistance = rescale(rnorm(n(), 0, 1), to = c(5, 95))) %>% 51 | # Simulate data from a logit model: https://stats.stackexchange.com/a/46525/3025 52 | # But then do all sorts of weird distortion to change the likelihood of using a net 53 | mutate(net_effect = (1.85 * income / 10) + (-1.7 * temperature) + (1.8 * health / 10) + 54 | (150 * enrolled) + (2.9 * household) + (150 * genetic_resistance), 55 | net_diff = net_effect - mean(net_effect), 56 | net_effect = ifelse(net_diff < 0, net_effect - (net_diff / 2), net_effect), 57 | net_effect_rescaled = rescale(net_effect, to = c(-2.2, 2.2)), 58 | inv_logit = 1 / (1 + exp(-net_effect_rescaled)), 59 | net_num = rbinom(n(), 1, inv_logit), 60 | net = net_num == 1) %>% 61 | mutate(malaria_risk_effect = (-5 * income / 10) + (3.9 * temperature) + 62 | (1.4 * insecticide_resistance) + (9 * health / 10) + (-80 * net_num) + (-80 * genetic_resistance), 63 | malaria_risk_diff = malaria_risk_effect - mean(malaria_risk_effect), 64 | malaria_risk_effect = ifelse(malaria_risk_diff < 0, 65 | malaria_risk_effect - (malaria_risk_diff / 2), 66 | malaria_risk_effect), 67 | malaria_risk_effect_rescaled = rescale(malaria_risk_effect, to = c(-2.2, 2.2)), 68 | malaria_risk = 1 / (1 + exp(-malaria_risk_effect_rescaled)), 69 | malaria_risk = round(malaria_risk * 100, 0)) %>% 70 | mutate_at(vars(income, insecticide_resistance), ~round(., 0)) %>% 71 | mutate(temperature = (temperature - 32) * 5/9, 72 | temperature = round(temperature, 1)) %>% 73 | mutate(malaria_risk = malaria_risk) 74 | 75 | mosquito_nets_final <- mosquito_nets %>% 76 | select(id, net, net_num, malaria_risk, income, health, household, 77 | eligible = enrolled, temperature, insecticide_resistance, 78 | genetic_resistance) 79 | 80 | mosquito_nets_final 81 | } 82 | 83 | net_data_full <- create_nets() 84 | net_data <- net_data_full %>% select(-genetic_resistance) 85 | 86 | 87 | usethis::use_data(net_data, overwrite = TRUE) 88 | usethis::use_data(net_data_full, overwrite = TRUE) 89 | 90 | -------------------------------------------------------------------------------- /data/net_data.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/r-causal/causalworkshop/767960a61a7a29703779459340c0d7363800f42f/data/net_data.rda -------------------------------------------------------------------------------- /data/net_data_full.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/r-causal/causalworkshop/767960a61a7a29703779459340c0d7363800f42f/data/net_data_full.rda -------------------------------------------------------------------------------- /man/install_solutions.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/install_solutions.R 3 | \name{install_solutions} 4 | \alias{install_solutions} 5 | \title{Install workshop solutions} 6 | \usage{ 7 | install_solutions(destdir = getOption("usethis.destdir")) 8 | } 9 | \arguments{ 10 | \item{destdir}{The path on your computer where you would like the workshop 11 | installed. By default, this will install somewhere conspicuous, like your 12 | desktop, although you can tell `install_solutions()` exactly where you want 13 | it to download.} 14 | } 15 | \description{ 16 | `install_solutions()` will download the solutions to the exercises for the workshop 17 | Causal Inference in R on your computer. Then, it will open a new RStudio 18 | Project containing the files you'll need. 19 | } 20 | -------------------------------------------------------------------------------- /man/install_workshop.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/install_workshop.R 3 | \name{install_workshop} 4 | \alias{install_workshop} 5 | \title{Install workshop materials} 6 | \usage{ 7 | install_workshop(destdir = getOption("usethis.destdir")) 8 | } 9 | \arguments{ 10 | \item{destdir}{The path on your computer where you would like the workshop 11 | installed. By default, this will install somewhere conspicuous, like your 12 | desktop, although you can tell `install_workshop()` exactly where you want 13 | it to download.} 14 | } 15 | \description{ 16 | `install_course()` will install the workshop materials for the workshop 17 | Causal Inference in R on your computer. Then, it will open a new RStudio 18 | Project containing the files you'll need. 19 | } 20 | --------------------------------------------------------------------------------