我在R。
中使用Synth()包(请参阅ftp://cran.r-project.org/pub/R/web/packages/Synth/Synth.pdf)这是我的数据框的一个子集:
all_data_uk <- structure(list(countryno = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 16, 16,
16), country = c("Australia", "Australia", "Australia", "Canada",
"Canada", "Canada", "Denmark", "Denmark", "Denmark", "United Kingdom",
"United Kingdom", "United Kingdom"), year = c(1971, 1972, 1973,
1971, 1972, 1973, 1971, 1972, 1973, 1971, 1972, 1973), top10_income_share = c(0.2657,
0.2627, 0.2546, 0.37833, 0.37807, 0.37271, 0.323069660453, 0.322700285165,
0.320162826601, 0.2929, 0.289, 0.2831), top5_income_share = c(0.1655,
0.1654, 0.1593, 0.24075, 0.24106, 0.23917, 0.211599113574, 0.21160700537,
0.209096813051, 0.1881, 0.1848, 0.1818), top1_income_share = c(0.0557,
0.0573, 0.054, 0.08866, 0.08916, 0.08982, 0.082392548404, 0.0824267594074,
0.07776546085945, 0.0702, 0.0694, 0.0699), gdp_growth = structure(c(4.00330835508684,
3.91178191457604, 2.59931282534502, 4.11765761702448, 5.44585557970514,
6.96420291945871, 3.00503299618597, 3.92934382503836, 4.09292523611968,
3.48436803631409, 4.30194591910262, 6.50872079327365), label = "(annual %)", class = c("labelled",
"numeric")), capital_quinn = structure(c(50, 37.5, 37.5, 87.5, 87.5, 75, 75, 75, 75, 50, 50, 50), label = "(financial openness - capital account)", class = c("labelled",
"numeric"))), class = "data.frame", .Names = c("countryno", "country",
"year", "top10_income_share", "top5_income_share", "top1_income_share",
"gdp_growth", "capital_quinn"), row.names = c(NA, -12L))
在我可重复的例子中,我有三个不同的结果变量&#34; top10_income_share&#34;,&#34; top5_income_share&#34;,&#34; top1_income_share&#34; (在我真正的问题中我有更多的方法)我想用它来运行分析。 &#34; gdp_growth&#34;和&#34; capital_quinn&#34;是我的控制变量。
对于一个结果变量,这里&#34; top10_income_share&#34;,我有以下代码(工作正常):
# Define treated and control units
control_units_top10 <- c(1,2)
treated_unit <- 16
# Run dataprep() which returns a list of matrices
dataprep.out_top10 <- dataprep(
foo = all_data_uk,
predictors = c("gdp_growth", "capital_quinn"),
predictors.op = "mean",
time.predictors.prior = 1971:1972,
special.predictors = list(
list("top10_income_share", 1971, "mean"),
list("top10_income_share", 1972, "mean")),
dependent = "top10_income_share",
unit.variable = "countryno",
unit.names.variable = "country",
time.variable = "year",
treatment.identifier = treated_unit,
controls.identifier = control_units_top10,
time.optimize.ssr = 1971:1972,
time.plot = 1971:1973)
# Run synth() command
synth.out_top10 <- synth(data.prep.obj = dataprep.out_top10, optimxmethod = "BFGS")
# Annual discrepancies in the top 10 income share trend between unit 4 (United Kingdom) and its synthetic counterpart:
gaps_top10 <- dataprep.out_top10$Y1plot - (dataprep.out_top10$Y0plot %*% synth.out_top10$solution.w)
我想循环这些命令并对所有三个结果变量进行相同的分析。我的问题是,每次我必须调整treatment.identifier
,special.predictors
和dependent
。此外,我想存储所有三个结果变量的输出(dataprep.out_top10,dataprep.out_top5 ...; synth.out_top10,synth.out_top5 ......等)。
我发现了一个类似的问题(Save every R for loop iteration in a new list),但是他们在每个循环中都有相同的结果和控制变量,并且只想循环控制单元而我没有成功地将他们的解决方案应用于我的问题。
以下是我到目前为止所提出的内容:
control_units_top10 <- c(1,2)
control_units_top5 <- c(1,2,3)
control_units_top1 <- c(1,3)
treated_unit <- 16
for(top in c("top10", "top5", "top1"))
{
paste0("dataprep.out_", top) <- dataprep(
foo = all_data_uk,
predictors = c("gdp_growth", "capital_quinn"),
predictors.op = "mean",
time.predictors.prior = 1971:1972,
special.predictors = list(
list(paste0(top, "_income_share"), 1971, "mean"),
list(paste0(top, "_income_share"), 1972, "mean")),
dependent = paste0(top, "_income_share"),
unit.variable = "countryno",
unit.names.variable = "country",
time.variable = "year",
treatment.identifier = treated_unit,
controls.identifier = get(paste0("control_units_", top)),
time.optimize.ssr = 1971:1972,
time.plot = 1971:1973)
paste0("synth.out_", top) <- synth(data.prep.obj = dataprep.out, optimxmethod = "BFGS")
paste0("gaps_", top) <- paste0("dataprep.out_", top)$Y1plot - (paste0("dataprep.out_", top)$Y0plot %*% paste0("synth.out_", top)$solution.w)
}
我收到错误:Error in paste0("synth.out_", top) <- synth(data.prep.obj = dataprep.out, : target of assignment expands to non-language object
,所以我想我的paste0()方法不起作用,但我找不到任何其他关于如何&#34; index&#34;的解决方案。结果变量和我的新对象。
我是R和stockoverflow的新手,对于如何设置循环的任何窍门都会感到非常高兴。
提前谢谢!