knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = TRUE, fig.height = 5, fig.width = 10) library(knitr)
You need to install package optmatch, rcbalance before installing the [SnapMatch] package. It is recommanded to install package lpSolve and RItools as well.
install.packages('optmatch') install.packages('rcbalance') install.packages('lpSolve') install.packages('RItools') library('optmatch') library('rcbalance') library('lpSolve') library('RItools') library('SnapMatch')
set.seed(188) library('optmatch') library('rcbalance') library('lpSolve') library('RItools') library('SnapMatch')
Put some introduction here.
The demo dataset contains 20 subjects in the treatment group and 100 subject in the control group. Each subject in the control group has 5 time points.
dat <- SnapMatch:::demo() head(dat) tail(dat)
We can match by mahalanobis/euclidean/rank_mahalanobis distance.
# calculate the distance matrix dis <- match_on(trt~X1+X2, data = dat, method="mahalanobis") # conduct optimal matching. 'Id' is the id column for each subject pm <- optmatch_snap(dis, data = dat, id = 'id') # plot result plot(xBalance(trt ~ X1+X2+strata(pm), data=dat), xlim=c(-2,1), ylim=c(0,3))
We find that covariate balance is achieved after snapshot matching.
# calculate propensity score prop <- glm(trt ~ X1+X2, data = dat, family = binomial()) boxplot(prop)
The groups do overlap, if not greatly. It may be wise to restrict the sample to the region of overlap, at least roughly.
# conduct optimal matching by propensity score. 'Id' is the id column for each subject pm <- optmatch_snap(trt ~ X1+X2, data = dat, id = 'id') # plot result plot(xBalance(trt ~ X1+X2+strata(pm), data=dat), xlim=c(-2,1), ylim=c(0,3))
We find that similar covariate balance is achieved after snapshot matching.
The package accepts 'match_on' function from 'optmatch' package as a distance matrix. For example, here we show how to perform a snapshot matching within a propensity score caliper.
# calculate distance by propensity score dis <- match_on(glm(trt ~ X1+X2, data = dat, family = binomial())) dis[1:5,1:5] # add caliper dis <- caliper(dis,2) + dis dis[1:5,1:5] # conduct optimal snapshot matching pm <- optmatch_snap(dis, data = dat, id = 'id') # plot result plot(xBalance(trt ~ X1+X2+strata(pm), data=dat), xlim=c(-2,1), ylim=c(0,3))
The demo dataset contains 20 subjects in the treatment group and 100 subject in the control group. Each subject in the control group has 5 time points.
dat <- SnapMatch:::demo()
We can match by mahalanobis/euclidean/rank_mahalanobis distance.
# calculate the distance matrix dis <- match_on(trt~X1+X2, data = dat, method="mahalanobis") # conduct nearest matching. 'Id' is the id column for each subject pm <- match_snap(dis, data = dat, id = 'id') # plot result plot(xBalance(trt ~ X1+X2+strata(pm), data=dat), xlim=c(-2,1), ylim=c(0,3))
We find that covariate balance is achieved after snapshot matching.
# calculate propensity score prop <- glm(trt ~ X1+X2, data = dat, family = binomial()) boxplot(prop)
The groups do overlap, if not greatly. It may be wise to restrict the sample to the region of overlap, at least roughly.
# conduct nearest matching by propensity score. 'Id' is the id column for each subject pm <- match_snap(trt ~ X1+X2, data = dat, id = 'id') # plot result plot(xBalance(trt ~ X1+X2+strata(pm), data=dat), xlim=c(-2,1), ylim=c(0,3))
We find that similar covariate balance is achieved after snapshot matching.
The package accepts 'match_on' function from 'optmatch' package as a distance matrix. For example, here we show how to perform a snapshot matching within a propensity score caliper.
# calculate distance by propensity score dis <- match_on(glm(trt ~ X1+X2, data = dat, family = binomial())) dis[1:5,1:5] # add caliper dis <- caliper(dis,2) + dis dis[1:5,1:5] # conduct nearest snapshot matching pm <- match_snap(dis, data = dat, id = 'id') # plot result plot(xBalance(trt ~ X1+X2+strata(pm), data=dat), xlim=c(-2,1), ylim=c(0,3))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.