I want to take a list of matched data sets (where observations are being matched on their propensity scores, using the MatchIt Package) for subsequent modelling in the Zelig Package.
In this example, there are two treatments I'll match on (t1
and t2
), two independent variables (x1
and x2
), and an outcome (y1
).
library(Zelig)
library(MatchIt)
library(plyr)
d1 <- data.frame(y1 = rbinom(100, 1, .5),
x1 = runif(100),
x2 = runif(100),
t1 = rbinom(100, 1, .5),
t2 = rbinom(100, 1, .5))
First, I'll make a list of matched data frames:
list.dfs <- llply(c("t1", "t2"),
function(i)
matchit(as.formula(paste0(i, "~ x1 + x2")), data= d1))
Just a check--each element of list.dfs
has the right class:
class(list.dfs[[1]])
[1] "matchit"
Next, I want to take element matched data frame from this list, and make a list of Zelig model objects
list.mods <- llply(list.dfs,
function(i)
zelig(y1 ~ x1 + x2, model = "logit", data = match.data(i)))
Which provides the following error:
Error in match.data(i) : object 'i' not found
But this is clearly something to do with the list, since everything works if I do the same function to a single element of list.dfs
:
class(zelig(y1 ~ x1 + x2, model = "logit", data = match.data(list.dfs[[1]])))
[1] "zelig" "logit"
What am I missing? How can I get Zelig to work on separate items in this list?
There seems to be some weird stuff inside zelig
that looks for the value of data
by name. Looks like you're going to have to do an explicit loop:
list.mods <- list()
for(i in seq_along(list.dfs)) {
list.mods[[i]] <- zelig(y1 ~ x1 + x2, model = "logit", data = match.data(list.dfs[[i]]))
}
list.mods