1

I have the panel data and I am applying propensity score matching. I have estimated the model on full sample using the following codes in CBPS package.

form1 <- (treat ~ X)
fit <- CBPS(formula=form1, data = paper1, time=year, id= bankid, ATT = TRUE)

I want to match now separately for the each year. I am using the if condition for this purpose and I have run the following code.

if (year==2001){
m.out1 <- matchit(t1 ~ fitted(fit), method = "nearest", data = paper1, replace = TRUE)
}

However, following warning is generated.

Warning message: In if (year == 2001) { : the condition has length > 1 and only the first element will be used

How I can perform the desired task?

I have replicated the problem with Blackwell data set. The complete codes are provided below: – There is a time id in the data set. I want to match if the time is equal to 1. I have run the model on full sample though.

library(CBPS)
data("Blackwell")
attach(Blackwell)
form1<-"d.gone.neg ~ d.gone.neg.l1 + d.gone.neg.l2 + d.neg.frac.l3 + camp.length + camp.length + deminc + base.poll + year.2002 + year.2004 + year.2006 + base.und + office"
fit <- CBPS(formula=form1, data = Blackwell, time=time, id= demName, ATT = TRUE)
m.out <- matchit(d.gone.neg ~ fitted(fit), method = "nearest", data = Blackwell, replace = TRUE)
summary(m.out)
if (time==1){
m.out1 <- matchit(d.gone.neg ~ fitted(fit), method = "nearest", data = Blackwell, replace = TRUE)
}
Hack-R
  • 22,422
  • 14
  • 75
  • 131
Ahmed Arif
  • 189
  • 1
  • 2
  • 10
  • Please check this out http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Hack-R Jun 12 '16 at 06:39
  • I have provided reproducible codes @Hack-R. I have provided in separate answer to my post. Looking for your response. – Ahmed Arif Jun 13 '16 at 16:19
  • Awesome! Thanks. We just need to move it from the answer section back to your original question. I'm at work right now but I will try to help as soon as I can. – Hack-R Jun 13 '16 at 17:03
  • OK, I've posted an updated answer, hope this helps. Since you can't combine the 3 different `matchit` objects (the results) in 1 object due to their special class I instead used `assign` with `paste` to create objects in the environment on each iteration of the `for` loop. The results in this example are `matchit_2002`,`matchit_2004`, and `matchit_2006`. – Hack-R Jun 13 '16 at 17:29

2 Answers2

1
if (!require("pacman")) install.packages("pacman")

pacman::p_load(CBPS, RRF)
data("Blackwell")
attach(Blackwell)

Blackwell$year <- NA
Blackwell$year[Blackwell$year.2002 == 1] <- 2002
Blackwell$year[Blackwell$year.2004 == 1] <- 2004
Blackwell$year[Blackwell$year.2006 == 1] <- 2006
Blackwell$year.2002 <- NULL
Blackwell$year.2004 <- NULL
Blackwell$year.2006 <- NULL

Blackwell <- na.roughfix(Blackwell)

form1<-"d.gone.neg ~ d.gone.neg.l1 + d.gone.neg.l2 + d.neg.frac.l3 + camp.length + camp.length + deminc + base.poll + year.2002 + year.2004 + year.2006 + base.und + office"
fit <- CBPS(formula=form1, data = Blackwell, time=time, id= demName, ATT = TRUE)
m.out <- matchit(d.gone.neg ~ fitted(fit), method = "nearest", data = Blackwell, replace = TRUE)
summary(m.out)


m.out1 <- list()
n      <- 0

for(i in unique(Blackwell$year)){
  n      <- n+1
  tmp <- matchit(d.gone.neg ~ fitted(fit)[Blackwell$year == i], method = "nearest", data = Blackwell[Blackwell$year == i,], replace = TRUE)
  nam <- paste("matchit_", i, sep = "")
  assign(nam, tmp)
}
Hack-R
  • 22,422
  • 14
  • 75
  • 131
0

I have replicated the problem with Blackwell data set. The complete codes are provided below: – There is a time id in the data set. I want to match if the time is equal to 1. I have run the model on full sample though.

library(CBPS)
data("Blackwell")
attach(Blackwell)
form1<-"d.gone.neg ~ d.gone.neg.l1 + d.gone.neg.l2 + d.neg.frac.l3 + camp.length + camp.length + deminc + base.poll + year.2002 + year.2004 + year.2006 + base.und + office"
fit <- CBPS(formula=form1, data = Blackwell, time=time, id= demName, ATT = TRUE)
m.out <- matchit(d.gone.neg ~ fitted(fit), method = "nearest", data = Blackwell, replace = TRUE)
summary(m.out)
if (time==1){
m.out1 <- matchit(d.gone.neg ~ fitted(fit), method = "nearest", data = Blackwell, replace = TRUE)
}

Warning message: In if (time == 1) { : the condition has length > 1 and only the first element will be used

Ahmed Arif
  • 189
  • 1
  • 2
  • 10