You will need to install the tableone, survey, and sandwich package
## Installing the tableone, survey, and sandwich package
library(tableone)
library(sandwich)
library(survey)
## Loading required package: grid
## Loading required package: Matrix
## Loading required package: survival
##
## Attaching package: 'survey'
## The following object is masked from 'package:graphics':
##
## dotchart
Stones.data2 <- Stones.data # copy original data set with factors
Stones.data2$success <- as.numeric(Stones.data2$success=='Y')
Stones.data2$treatment <- as.numeric(Stones.data2$treatment=='B')
Stones.data2$stone_size <- as.numeric(Stones.data2$stone_size=='large')
#### look at a table 1
table1 <- CreateTableOne(vars = "stone_size", strata = "treatment",
data = Stones.data2, test = FALSE)
#### include standardized mean difference (SMD)
print(table1,smd=TRUE)
## Stratified by treatment
## 0 1 SMD
## n 350 350
## stone_size (mean (SD)) 0.75 (0.43) 0.23 (0.42) 1.225
#### propensity score model
psmodel <- glm(treatment ~ stone_size, family = binomial(link ="logit"), data = Stones.data2)
#### value of propensity score for each subject
## ps <- psmodel$fitted.values # alternative
ps <- predict(psmodel, type = "response")
#### create IP weights
Stones.data2$wt <- ifelse(Stones.data2$treatment==1, 1/(ps), 1/(1-ps))
#### apply weights to data
weighteddata <- svydesign(ids = ~ 1, data =Stones.data2, weights = ~ wt)
#### weighted table 1
weightedtable <- svyCreateTableOne(vars = "stone_size", strata = "treatment",
data = weighteddata, test = FALSE)
#### Show table with SMD
print(weightedtable, smd = TRUE)
## Stratified by treatment
## 0 1 SMD
## n 700.00 700.00
## stone_size (mean (SD)) 0.49 (0.50) 0.49 (0.50) <0.001
#### Causal Risk Difference with weigthed GLM
glm_obj <- glm(success ~ treatment, weights = wt, family =
quasibinomial(link="identity"), data = Stones.data2)
beta_ipw <- unname(coef(glm_obj))
SE <- unname(sqrt(diag(vcovHC(glm_obj, type="HC0"))))
#### get point estimate and CI for risk difference
cRD <- beta_ipw[2]
conf_level = 0.95
Z <- qnorm((1 + conf_level)/2)
cint <- beta_ipw[2] + c(-1,1) * Z * SE[2]
list(causal_RD = cRD, conf_int = cint, conf_level = conf_level )
## $causal_RD
## [1] -0.05367122
##
## $conf_int
## [1] -0.12154033 0.01419789
##
## $conf_level
## [1] 0.95
#### Causal Risk Ratio with weighted GLM
glm_obj <- glm(success ~ treatment, weights = wt, family =
quasibinomial(link = log), data = Stones.data2)
beta_ipw <- unname(coef(glm_obj))
#### To account for weighting, use asymptotic (sandwich) variance
SE <- unname(sqrt(diag(vcovHC(glm_obj, type="HC0"))))
cRR <- exp(beta_ipw[2])
conf_level = 0.95
Z <- qnorm((1 + conf_level)/2)
cint <- exp(beta_ipw[2] + c(-1,1) * Z * SE[2])
list(causal_RR = cRR, conf_int = cint, conf_level = conf_level)
## $causal_RR
## [1] 0.9355336
##
## $conf_int
## [1] 0.8590792 1.0187923
##
## $conf_level
## [1] 0.95
#### Causal Odds Ratio with weighted GLM
glm_obj <- glm(success ~ treatment, weights = wt, family =
quasibinomial(link = logit), data = Stones.data2)
beta_ipw <- unname(coef(glm_obj))
#### To account for weighting, use asymptotic (sandwich) variance
SE <- unname(sqrt(diag(vcovHC(glm_obj, type="HC0"))))
#### get point estimate and CI for odds ratio
cOR <- exp(beta_ipw[2])
conf_level = 0.95
Z <- qnorm((1 + conf_level)/2)
cint <- exp(beta_ipw[2] + c(-1,1) * Z * SE[2])
list(causal_OR = cOR, conf_int = cint, conf_level = conf_level)
## $causal_OR
## [1] 0.7084619
##
## $conf_int
## [1] 0.4617441 1.0870053
##
## $conf_level
## [1] 0.95