Title: How can I loop through a list of predictors using the Synth() Package in R
Tags: r;loops;for-loop;lapply;synth
Question: 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.
Comment: Yes, that's correct. I'd like to find a way to automate this in a loop rather than doing it by hand.

Comment: So to make it simple, you want to make a combination of predictors?


Here is another answer: If you want to make a combination of predictor, you can make use of this syntax expand.grid().. so example I have 2 Vector containing, ID and Product Name, here I can make the combination of these two with these:
vec1 = c(1,4,7)
vec2 = c("ProdA", "ProdB", "ProdC")
expand.grid(vec1, vec2)
Var1  Var2
1    1 ProdA
2    4 ProdA
3    7 ProdA
4    1 ProdB
5    4 ProdB
6    7 ProdB
7    1 ProdC
8    4 ProdC
9    7 ProdC
combination = expand.grid(vec1, vec2)
combination$combined = paste0(combination$Var2, combination$Var1)
combination
Var1  Var2 combined
1    1 ProdA   ProdA1
2    4 ProdA   ProdA4
3    7 ProdA   ProdA7
4    1 ProdB   ProdB1
5    4 ProdB   ProdB4
6    7 ProdB   ProdB7
7    1 ProdC   ProdC1
8    4 ProdC   ProdC4
9    7 ProdC   ProdC7

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:04/16/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<r><loops><for-loop><lapply><synth>', 'Title': 'How can I loop through a list of predictors using the Synth() Package in R', 'ViewCount': 111, 'id': 61261842, 'user_AboutMe': None, 'user_AccountId': 10767199.0, 'user_DisplayName': 'Julia Lawson', 'user_Reputation': 1.0, 'user_Views': 3.0, 'user_id': 7921768.0}}