1 Star 0 Fork 1

titzp/Causal-Inference-Using-Quasi-Experimental-Methods

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
4. Synthetic Control Method 2.54 KB
一键复制 编辑 原始数据 按行查看 历史
# install and load package
install.packages("Synth")
library(Synth)
# read the dataset "basque"
data("basque")
#EDA
dim(basque) #774*17
basque[1:10,]
# set up different arguments
# read the dataset
dataprep.out <- dataprep(foo = basque,
predictors = c(“school.illit”, “school.prim”, “school.med”,
“school.high”, “school.post.high”, “invest”),
predictors.op = “mean”, # the operator
time.predictors.prior = 1964:1969, #the entire time frame from the #beginning to the end
special.predictors = list(
list(“gdpcap”, 1960:1969, “mean”),
list(“sec.agriculture”, seq(1961,1969,2),”mean”),
list(“sec.energy”,seq(1961,1969,2),”mean”),
list(“sec.industry”, seq(1961,1969,2),”mean”),
list(“sec.construction”, seq(1961,1969,2),”mean”),
list(“sec.services.venta”, seq(1961,1969,2),”mean”),
list(“sec.services.nonventa”,seq(1961,1969,2),”mean”),
list(“popdens”, 1969, “mean”)),
dependent = “gdpcap”, # dv
unit.variable = “regionno”,#identifying unit numbers
unit.names.variable = “regionname”,#identifying unit names
time.variable = “year”,#time-periods
treatment.identifier = 17,#the treated case
controls.identifier = c(2:16, 18),#the control cases; all others #except number 17
time.optimize.ssr = 1960:1969,#the time-period over which to optimize
time.plot = 1955:1997)#the entire time period before/after the treatment
# run synth()
synth.out = synth(data.prep.obj = dataprep.out, method = “BFGS”)
# To calculate the difference between the real Baseque region and the synthetic control as follow
gaps = dataprep.out$Y1plot — (dataprep.out$Y0plot
%*% synth.out$solution.w)
synth.tables = synth.tab(dataprep.res = dataprep.out, synth.res = synth.out)
names(synth.tables)
gaps[1:3,1]
synth.tables$tab.pred[1:13,]
#relative importance of each donor to the synthetic control case
synth.tables$tab.w[8:14, ]
# plot the synthetic and the real cases before and after the treatment
path.plot(synth.res=synth.out,dataprep.res = dataprep.out,
Ylab="real per-capita gdp (1986 USD, thousand)",Xlab="year",
Ylim = c(0,12),Legend = c("Basque country",
"synthetic Basque country"),
Legend.position = "bottomright")
gaps.plot(synth.res = synth.out, dataprep.res = dataprep.out,
Ylab = “gap in real per-capita GDP (1986 USD, thousand)”, Xlab= “year”,
Ylim = c(-1.5,1.5), Main = NA)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/titzp/Causal-Inference-Using-Quasi-Experimental-Methods.git
git@gitee.com:titzp/Causal-Inference-Using-Quasi-Experimental-Methods.git
titzp
Causal-Inference-Using-Quasi-Experimental-Methods
Causal-Inference-Using-Quasi-Experimental-Methods
master

搜索帮助