### Install and load devtools Source: http://optmat.ch Prepares the environment for installing development packages. ```R > install.packages("devtools") > library("devtools") ``` -------------------------------- ### Install and load optmatch Source: http://optmat.ch Install the package from CRAN and load it into the R session. ```R > install.packages("optmatch") > library("optmatch") ``` -------------------------------- ### Install development version Source: http://optmat.ch Installs the master branch of Optmatch from GitHub while in dev_mode. ```R d> install_github("markmfredrickson/optmatch") ``` -------------------------------- ### Install local binary package Source: http://optmat.ch Installs a locally built binary version of the optmatch package. ```r install.packages("../optmatch_VERSION.tgz", repo=NULL) ``` -------------------------------- ### Install package dependencies Source: http://optmat.ch Ensures all required dependencies for the optmatch package are installed. ```r devtools::install_deps(dependencies = TRUE) ``` -------------------------------- ### Development commands via Make Source: http://optmat.ch Common command-line tasks for managing the package build and test lifecycle. ```bash make test ``` ```bash make document ``` ```bash make interactive ``` ```bash make interactive-emacs ``` ```bash make check ``` ```bash make build ``` ```bash make vignette ``` ```bash make clean ``` ```bash make release ``` -------------------------------- ### Load Optmatch in dev_mode Source: http://optmat.ch Loads the development version of the package. ```R d> library(optmatch) ``` -------------------------------- ### Generate simulated data for matching Source: http://optmat.ch Create a dataset with treated and control groups based on covariates to demonstrate matching workflows. ```R set.seed(20120111) # set this to get the exact same answers as I do n <- 26 # chosen so we can divide the alphabet in half W <- data.frame(w1 = rbeta(n, 4, 2), w2 = rbinom(n, 1, p = .33)) # nature assigns to treatment tmp <- numeric(n) tmp[sample(1:n, prob = W$w1^(1 + W$w2), size = n/2)] <- 1 W$z <- tmp # for convenience, let's give the treated units capital letter names tmp <- character(n) tmp[W$z == 1] <- LETTERS[1:(n/2)] tmp[W$z == 0] <- letters[(26 - n/2 + 1):26] rownames(W) <- tmp ``` -------------------------------- ### Print match results Source: http://optmat.ch Displays the match object with units grouped by factor level. ```R print(mahal.match, grouped = T) ``` -------------------------------- ### Toggle dev_mode Source: http://optmat.ch Activates or deactivates the development mode for package management. ```R > dev_mode() d> ``` ```R d> dev_mode() > ``` -------------------------------- ### Revert to CRAN version Source: http://optmat.ch Removes the local development version and reinstalls the package from CRAN. ```r remove.packages("optmatch") install.packages("optmatch") ``` -------------------------------- ### Generate pair matches Source: http://optmat.ch Creates a one-to-one match using the Mahalanobis distance matrix. ```R mahal.match <- pairmatch(distances$mahal, data = W) ``` -------------------------------- ### Evaluate distance specifications Source: http://optmat.ch Applies fullmatch to a list of distance specifications. ```R matches <- lapply(distances, function(x) { fullmatch(x, data = W) }) ``` -------------------------------- ### Inspect group balance Source: http://optmat.ch Examine the distribution of covariates across treatment groups using tables and density plots. ```R table(W$w2, W$z) library(lattice) ; densityplot(W$w1, groups = W$z) ``` -------------------------------- ### Append match results to data Source: http://optmat.ch Joins the match factor back to the original data frame. ```R W.matched <- cbind(W, matches = mahal.match) ``` -------------------------------- ### Combine distance metrics Source: http://optmat.ch Aggregates multiple distance matrices into a single metric using standard arithmetic operations. ```R distances$all <- with(distances, euclid + mahal + propensity) ``` -------------------------------- ### Calculate Euclidean distances Source: http://optmat.ch Generate a distance matrix for matching based on Euclidean distance between covariates. ```R distances <- list() distances$euclid <- match_on(z ~ w1 + w2, data = W, method = "euclidean") ``` -------------------------------- ### Compute Mahalanobis distance Source: http://optmat.ch Calculates Mahalanobis distance using the match_on function with a formula interface. ```R distances$mahal <- match_on(z ~ w1 + w2, data = W) ``` -------------------------------- ### Stitch distance matrices Source: http://optmat.ch Combines different distance specifications using cbind to merge results from subsets of data. ```R W.n.to.s <- W[c(LETTERS[1:13], letters[14:19]),] W.t.to.z <- W[c(LETTERS[1:13], letters[20:26]),] mahal.n.to.s <- match_on(z ~ w1 + w2, data = W.n.to.s) ps.t.to.z <- match_on(glm(z ~ w1 + w2, data = W.t.to.z, family = binomial())) distances$combined <- cbind(mahal.n.to.s, ps.t.to.z) ``` -------------------------------- ### Compute Mahalanobis distance with caliper Source: http://optmat.ch Calculates Mahalanobis distance restricted to units differing by less than one on the propensity score. ```R distances$mahal.trimmed <- match_on(z ~ w1 + w2, data = W, within = match_on(propensity.model, caliper = 1)) ``` -------------------------------- ### Apply a caliper to distances Source: http://optmat.ch Trims a distance matrix by setting values exceeding a specified threshold to infinity. ```R distances$median.caliper <- caliper(distances$all, median(distances$all)) distances$all.trimmed <- with(distances, all + median.caliper) ``` -------------------------------- ### Compute propensity score distance Source: http://optmat.ch Uses a GLM model to estimate propensity scores and compute distances based on the model output. ```R propensity.model <- glm(z ~ w1 + w2, data = W, family = binomial()) distances$propensity <- match_on(propensity.model) ``` -------------------------------- ### Filter distance computation with exact matching Source: http://optmat.ch Uses the within argument to restrict distance calculations to valid pairs defined by an exact match structure. ```R tmp <- exactMatch(z ~ w2, data = W) distances$exact <- match_on(z ~ w1, data = W, within = tmp) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.