thanks, yes it was easier than I thought.
There may be a simpler method but flattening the match.matrix to a vector
and merging with row sums seemed easiest.
makeMatchItWeights <- function(x)
{
stopifnot(inherits(x, "matchit"))
m <- x$match.matrix # each row is 1 treated person, elements are the matched
control IDs
# columns in matrix are maximum # of matches per treated
max.matched <- ncol(m)
# make matrix into vector of controlIDs
# each consecutive block of max.matched are the controls for 1 treated
# treated IDs as repeated vector to align with control IDs
mdf <- data.frame(tid=rep(rownames(m),
each=max.matched),cid=as.vector(t(m)))
mdf<-mdf[complete.cases(mdf),]
# make df of the row counts
rowmm <- ifelse(is.na(x$match.matrix), 0,1)
r<- rowSums(rowmm)
rms <- data.frame(rid=names(r), s=r)
mdfs<- merge(mdf, rms, all.x=T, by.x="tid", by.y="rid")
mdfs$w<-1/mdfs$s
mdfs
}
On Thu, Jan 27, 2011 at 6:02 PM, Kosuke Imai <kimai(a)princeton.edu> wrote:
You would have to do a bit of R programming here.
First, for each treated
unit, you can compute the number of matched controls. This can be done by
apply(!is.na(match.matrix), 1, sum). Then, for each control unit, you
check which treated unit it was matched to (you probably need to use a loop
and "%in%").
Kosuke
On Jan 27, 2011, at 8:09 PM, Chris Hane wrote:
Hello,
I am using nearest matching with ratio of 20 or 50 and a caliper. When I
do this
I get the message that not every treated observation has the
requested number of matches. This is OK for my application, but the weights
of the controls are all 1.
I would like the weights to reflect the number of controls for each
treated
observation, That is, control weight=1/K where K is the number of
non-NAs in the match.matrix in the row where the control obsID occurs.
Otherwise the weights do not properly reflect the balance achieved by
matching.
Is there a simple way to compute this weight vector from the match
matrix?
Thanks,
Chris