|
| 1 | +# CausalGPS | Example 1 |
| 2 | +## Matching with parametric GPS model |
| 3 | + |
| 4 | +In this example, we will use the CausalGPS R package to generate pseudo population based on parametric GPS model. We use synthetic Medicare data that is available in the package. All processing are done in R. |
| 5 | + |
| 6 | +Step 1: Install and load the package |
| 7 | + |
| 8 | +```r |
| 9 | +install.packages("CausalGPS") |
| 10 | +library(CausalGPS) |
| 11 | +``` |
| 12 | + |
| 13 | +Step 2: Load the data |
| 14 | + |
| 15 | +```r |
| 16 | +data("synthetic_us_2010", package = "CausalGPS") |
| 17 | +synthetic_us_2010$cdc_mean_bmi[synthetic_us_2010$cdc_mean_bmi > 9000] <- NA |
| 18 | +data <- synthetic_us_2010 |
| 19 | + |
| 20 | +confounders_s1 <- c("cs_poverty","cs_hispanic", |
| 21 | + "cs_black", |
| 22 | + "cs_ed_below_highschool", |
| 23 | + "cs_median_house_value", |
| 24 | + "cs_population_density", |
| 25 | + "cdc_mean_bmi","cdc_pct_nvsmoker", |
| 26 | + "gmet_mean_summer_tmmx", |
| 27 | + "gmet_mean_summer_rmx", |
| 28 | + "gmet_mean_summer_sph", |
| 29 | + "cms_female_pct", "region" |
| 30 | +) |
| 31 | + |
| 32 | +data$region <- as.factor(data$region) |
| 33 | +``` |
| 34 | + |
| 35 | +Step 3: Generate Pseudo Population |
| 36 | + |
| 37 | +```r |
| 38 | +set.seed(574) |
| 39 | +ps_pop_obj_1 <- generate_pseudo_pop(data$cms_mortality_pct, |
| 40 | + data$qd_mean_pm25, |
| 41 | + data.frame(data[, confounders_s1, drop=FALSE]), |
| 42 | + ci_appr = "matching", |
| 43 | + gps_model = "parametric", |
| 44 | + bin_seq = NULL, |
| 45 | + trim_quantiles = c(0.25 , |
| 46 | + 0.99), |
| 47 | + optimized_compile = TRUE, |
| 48 | + use_cov_transform = TRUE, |
| 49 | + sl_lib = c("m_xgboost"), |
| 50 | + params = list(xgb_nrounds=seq(10,60), |
| 51 | + xgb_eta=seq(0.04, 0.4, 0.02)), |
| 52 | + nthread = 12, |
| 53 | + covar_bl_method = "absolute", |
| 54 | + covar_bl_trs = 0.1, |
| 55 | + covar_bl_trs_type= "maximal", |
| 56 | + max_attempt = 10, |
| 57 | + matching_fun = "matching_l1", |
| 58 | + delta_n = 0.1, |
| 59 | + scale = 1) |
| 60 | + |
| 61 | +``` |
| 62 | + |
| 63 | +In the previous code, we trimmed data using `trim_quantiles`. This helps to focus on common support range. `m_` in `m_xgboost` stands for modified xgboost library. So we can pass a range of hyperparameter with `xgb_` prefix. Covariate balance has 3 options including method (which only `absolute` has been implemented so far), threshold (`covar_bl_trs`) and threshold type (`covar_bl_trs_type`) which includes `mean`, `median`, and `maximal`. `max_attempt` is the maximum number of attempts to generate pseudo population. `matching_fun` is the matching function (only `matching_l1` has been implemented so far). `delta_n` is the size of caliper and `scale` is a specified scale parameter to control the relative weight that |
| 64 | +is attributed to the distance measures of the exposure versus the GPS. |
| 65 | + |
| 66 | +After 6 iterations, a covariate balance is achieved one can run summary to see the summary of the results. |
| 67 | + |
| 68 | +```{r, eval=FALSE} |
| 69 | +summary(ps_pop_obj_1) |
| 70 | +``` |
| 71 | + |
| 72 | +```s |
| 73 | +--- CausalGPS pseudo population object summary --- |
| 74 | +Pseudo population met the covariate balance requirement: TRUE |
| 75 | +Absolute correlation of the original data: |
| 76 | + mean: 0.151 |
| 77 | + median: 0.134 |
| 78 | + maximal: 0.367 |
| 79 | +
|
| 80 | + cs_poverty : 0.084 |
| 81 | + cs_hispanic : 0.134 |
| 82 | + cs_black : 0.245 |
| 83 | + cs_ed_below_highschool : 0.234 |
| 84 | + cs_median_house_value : 0.085 |
| 85 | + cs_population_density : 0.190 |
| 86 | + cdc_mean_bmi : 0.047 |
| 87 | + cdc_pct_nvsmoker : 0.020 |
| 88 | + gmet_mean_summer_tmmx : 0.254 |
| 89 | + gmet_mean_summer_rmx : 0.218 |
| 90 | + gmet_mean_summer_sph : 0.367 |
| 91 | + cms_female_pct : 0.003 |
| 92 | + region : 0.078 |
| 93 | +
|
| 94 | + Absolute correlation of the pseudo population: |
| 95 | + mean: 0.060 |
| 96 | + median: 0.057 |
| 97 | + maximal: 0.099 |
| 98 | +
|
| 99 | + cs_poverty : 0.079 |
| 100 | + cs_hispanic : 0.025 |
| 101 | + cs_median_house_value : 0.003 |
| 102 | + cdc_mean_bmi : 0.057 |
| 103 | + cdc_pct_nvsmoker : 0.035 |
| 104 | + gmet_mean_summer_tmmx : 0.096 |
| 105 | + gmet_mean_summer_sph : 0.055 |
| 106 | + cms_female_pct : 0.072 |
| 107 | + cs_black : 0.099 |
| 108 | + cs_ed_below_highschool : 0.089 |
| 109 | + cs_population_density : 0.087 |
| 110 | + gmet_mean_summer_rmx : 0.047 |
| 111 | + region : 0.031 |
| 112 | +
|
| 113 | + Hyperparameters used for the select population: |
| 114 | + xgb_nrounds : 21 |
| 115 | + xgb_max_depth : 6 |
| 116 | + xgb_eta : 0.32 |
| 117 | + xgb_min_child_weight : 1 |
| 118 | + xgb_verbose : 0 |
| 119 | +
|
| 120 | +Number of data samples: 2299 |
| 121 | +Number of iterations: 6 |
| 122 | +--- *** --- |
| 123 | +
|
| 124 | +``` |
| 125 | + |
| 126 | +It is important to note that the package ignores any data with missing values. As a result, number of data samples are different from the original data. We can also plot the covariate balance. |
| 127 | + |
| 128 | +```{figure} figures/pdf/example_w_1_covar.pdf |
| 129 | +--- |
| 130 | +height: 600 px |
| 131 | +name: Covariate Balance |
| 132 | +``` |
| 133 | + |
| 134 | +Now, we can conduct the analysis using the pseudo population. |
| 135 | + |
| 136 | +```r |
| 137 | +set.seed(168) |
| 138 | +erf <- estimate_npmetric_erf(m_Y = ps_pop_obj_1$pseudo_pop$Y, |
| 139 | + m_w = ps_pop_obj_1$pseudo_pop$w, |
| 140 | + counter_weight = ps_pop_obj_1$pseudo_pop$counter_weight, |
| 141 | + bw_seq = seq(0.2,10,0.05), |
| 142 | + w_vals = seq(7,13, 0.05), |
| 143 | + nthread = 12) |
| 144 | +``` |
| 145 | + |
| 146 | +The `erf` object contains the estimated exposure-response function. The following plot shows the exposure function. |
| 147 | + |
| 148 | +```r |
| 149 | +plot(erf) |
| 150 | +``` |
| 151 | + |
| 152 | +```{figure} figures/pdf/example_w_1_erf.pdf |
| 153 | +
|
| 154 | +--- |
| 155 | +height: 400 px |
| 156 | +name: Exposure Response Function |
| 157 | +``` |
0 commit comments