2016-06-12 31 views
1

我有面板數據,我正在應用傾向分數匹配。我已經使用CBPS包裝中的以下代碼估算了完整樣本的模型。如何迭代運行模型

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

我想每年單獨匹配一年。我正在使用if條件來達到這個目的,並且我運行了下面的代碼。

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

但是,會生成以下警告。

警告消息: 在如果(年== 2001){: 狀況已經長度> 1且僅第一個元素將被用來

如何可以執行期望的任務?

我用Blackwell數據集複製了這個問題。完整的代碼如下所示: - 數據集中有一個時間標識。如果時間等於1,我想匹配。儘管如此,我已經在完整示例上運行了該模型。

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) 
} 
+0

請檢查了這一點http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example –

+0

我提供了可再現的代碼@ Hack-R。我在單獨的答覆中提供了我的帖子。尋找你的迴應。 –

+0

太棒了!謝謝。我們只需要將它從答案部分移回原來的問題。我現在正在工作,但我會盡快提供幫助。 –

回答

0

我用Blackwell數據集複製了這個問題。完整的代碼如下所示: - 數據集中有一個時間標識。如果時間等於1,我想匹配。儘管如此,我已經在完整示例上運行了該模型。

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) 
} 

警告消息: 在如果(時間== 1){: 條件具有長度> 1且僅第一個元素將被用來

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) 
} 
+0

非常感謝。我希望我能夠將這些代碼複製到我的特定數據。 –