代码拉取完成,页面将自动刷新
同步操作将从 张剑/Causal-Inference-Using-Quasi-Experimental-Methods 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
# 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)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。