Skip to content

Instantly share code, notes, and snippets.

@markmfredrickson
markmfredrickson / strata.R
Created July 22, 2015 20:41
Replacing explict strata arg
library(RItools)
library(optmatch)
set.seed(20130801)
n <- 500
library(MASS)
xs <- mvrnorm(n,
mu = c(1,2,3),
Sigma = matrix(c(1, 0.5, 0.2,
0.5, 1, 0,
@markmfredrickson
markmfredrickson / gist:6157476
Created August 5, 2013 16:52
How to look at submatrices of InfinitySparseMatrix objects
library(optmatch)
example(match_on)
#' @param x An ISM or blocked ISM
#' @param rs Names of rows to keep
#' @param cs Names of columns to keep
submatrix <- function(x, rs, cs) {
subset(x,
x@rownames %in% rs,
module Lady
where
{--
- The Lady Tasting Tea
-
- In "Statistical Methods for Research Workers" (1925) Fisher illustrates the basic
- ideas of randomization inference in the story of a woman who claims she can
- tell the difference between tea in which the milk is added before or after
- the water. The experiment consists of a tray of 8 cups of tea, 4 of which
@markmfredrickson
markmfredrickson / gist:1294468
Created October 18, 2011 02:28
Comparing the size of expand.grid with a list of functions of the same size.
gsize <- function(n) {
grid <- expand.grid(rep(list(1:10), n))
object.size(grid) / 1024
}
fsize <- function(n) {
f <- function(i) {force(i) ; function(j) { i + j}}
flst <- lapply(1:(10^n), f)
object.size(flst) / 1024
}
@markmfredrickson
markmfredrickson / gist:1121546
Created August 2, 2011 23:54
A quick simulation of block size effect on estimated treatment effects
library(RItools)
# set up the potential outcomes, n = 20
Yc <- rnorm(20)
Yt <- Yc + 1 # additive effect of 2
# "randomly" sample 1/2 of the data, this works because we know how Yc is created
Z <- rep(c(T,F), 10)
R <- ifelse(Z, Yt, Yc)
(defn data->maps "Turns a vector of vectors into a seq of maps"
[keys data]
(map #(zipmap keys %) data))
(defn data-merge
"Merge two data sets on the given key"
[merge-key a b]
(let [indexed-b (zipmap (map merge-key b) b)]
(map #(into % (indexed-b (merge-key %))) (filter #(contains? indexed-b (merge-key %)) a))))