title: Loop over many outcome variables - Synth() package in R
body: <p>I am using the Synth() package (see <a href="ftp://cran.r-project.org/pub/R/web/packages/Synth/Synth.pdf" rel="nofollow noreferrer">ftp://cran.r-project.org/pub/R/web/packages/Synth/Synth.pdf</a>) in R.</p>
  
  <p>This is a subset of my data frame:</p>
  
  <pre><code>all_data_uk &lt;- 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))
  </code></pre>
  
  <p>In my reproducible example I have three different outcome variables "top10_income_share", "top5_income_share", "top1_income_share" (in my real problem I have way more) that I want to run the analysis with. "gdp_growth" and "capital_quinn" are my control variables. </p>
  
  <p>For one outcome variable, here "top10_income_share", I have the following code (which works fine):</p>
  
  <pre><code># Define treated and control units
  control_units_top10 &lt;- c(1,2)
  treated_unit &lt;- 16
  
  # Run dataprep() which returns a list of matrices
  dataprep.out_top10 &lt;- 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 &lt;- 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 &lt;- dataprep.out_top10$Y1plot - (dataprep.out_top10$Y0plot %*% synth.out_top10$solution.w)
  </code></pre>
  
  <p>I would like to loop over these commands and do the same analysis for all three outcome variables. My problem is, that each time I have to adjust <code>treatment.identifier</code>, <code>special.predictors</code> and <code>dependent</code>. Furthermore I would like to store the outputs (dataprep.out_top10, dataprep.out_top5...; synth.out_top10, synth.out_top5... etc.) for all three outcome variables.</p>
  
  <p>I found a similar question (<a href="https://stackoverflow.com/questions/37027787/save-every-r-for-loop-iteration-in-a-new-list">Save every R for loop iteration in a new list</a>), however they had the same outcome and control variables in each loop and only wanted to loop over the control units and I did not succeed in applying their solution to my problem.</p>
  
  <p>In the following is what I came up with so far:</p>
  
  <pre><code>control_units_top10 &lt;- c(1,2)
  control_units_top5 &lt;- c(1,2,3)
  control_units_top1 &lt;- c(1,3)
  treated_unit &lt;- 16
  
  for(top in c("top10", "top5", "top1"))   
  {
    paste0("dataprep.out_", top) &lt;- 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) &lt;- synth(data.prep.obj = dataprep.out, optimxmethod = "BFGS")
  
    paste0("gaps_", top) &lt;- paste0("dataprep.out_", top)$Y1plot - (paste0("dataprep.out_", top)$Y0plot %*% paste0("synth.out_", top)$solution.w)
  }
  </code></pre>
  
  <p>I get the error: <code>Error in paste0("synth.out_", top) &lt;- synth(data.prep.obj = dataprep.out, : target of assignment expands to non-language object</code>, so I guess my paste0() approach does not work, but I could not find any other solution on how to "index" the outcome variables and my new objects.</p>
  
  <p>I am new to R and stockoverflow and would be very happy about any tipps on how to set up the loop.</p>
  
  <p>Thank you in advance!</p>