我有面板数据,我正在应用倾向得分匹配。我使用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)
}
但是,会生成以下警告。
警告讯息: 在if(year == 2001){: 条件的长度> 1,只使用第一个元素
我如何执行所需的任务?
我已经用Blackwell数据集复制了这个问题。完整代码如下: - 数据集中有时间ID。如果时间等于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 :(得分: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)
}
答案 1 :(得分:0)
我已经用Blackwell数据集复制了这个问题。完整的代码如下: - 数据集中有时间ID。如果时间等于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)
}
警告讯息: 在if(time == 1){: 条件的长度> 1,只使用第一个元素