I am using the 'Synth' package in R (see ftp://cran.r-project.org/pub/R/web/packages/Synth/Synth.pdf) and I'd like to figure out how to run all possible combinations of my predictors. I have been using the very helpful previous 'Synth' loop questions here (looping over outcome variables) and here (saving loops in a list), but neither fully resolved my question and I'm still feeling stuck.
I'm going to use the previous toy dataset from looping over outcome variables for simplicity:
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))
Using 'Synth' dataprep, here is the output:
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")
I would like to create a loop such that each iteration of the predictors (1) "gdp_growth", (2) "capital_quinn", and (3) "gdp_growth" AND "capital_quinn" are run and stored in a list so I can compare MSPE from optimization over v and w weights ('loss.v', 'loss.w'). In other words:
predictors = c("gdp_growth")
predictors = c("capital_quinn")
predictors = c("gdp_growth", "capital_quinn")
In reality, I have five predictors, so I am in need of a more efficient way in which to run combinations of predictors.