title: How to automatically set-up and add functions to a model in R?
body: <p>I am setting up a model, and I am trying to reduce the amount of writing I have to do. </p>
  
  <p>Concretely, I am using the coala R-package to do coalescent simulations, and I am trying to easily implement a stepping-stone migration model.</p>
  
  <p>A reproducible example: 4 linearly distributed populations exchange migrants according to stepping-stone pattern (only the adjacent populations).</p>
  
  <pre><code>model &lt;- coal_model(sample_size = c(5, 5, 5, 5),
                      loci_number = 1,
                      loci_length = 10,
                      ploidy = 1) +
  feat_mutation(rate = mut_rate, # e.g. 0.1
                model = "HKY",
                base_frequencies = c(0.25,0.25,0.25,0.25),
                tstv_ratio = 4) +
  feat_migration(mig_rate, 1, 2) + # mig_rate can be e.g. 0.5
  feat_migration(mig_rate, 2, 1) +
  feat_migration(mig_rate, 2, 3) +
  feat_migration(mig_rate, 3, 2) +
  feat_migration(mig_rate, 3, 4) +
  feat_migration(mig_rate, 4, 3) +
  sumstat_dna(name = "dna", transformation = identity)
  </code></pre>
  
  <p>This example works, but the downside is that I have to write many 'feat_migration' lines, although there is a clear pattern that could be automated. It is fine for a small number of populations, but I want to do a large simulation with about 70 populations. Does someone has a good idea how to automate this? The documentation has not helped me so far.</p>
  
  <p>I tried two things that didn't work:</p>
  
  <pre><code>feat_migration(mig_rate, c(1,2,2,3,3,4), c(2,1,3,2,4,3))
  </code></pre>
  
  <p>and something like this:</p>
  
  <pre><code>migration_model &lt;- function(){
    for(i in 1:n_pops){
      feat_migration(mig_rate, i, i+1) +
      feat_migration(mig_rate, i+1, i))
  }
  </code></pre>
  
  <p>In the latter case, I don't really know how I can correctly create and parse all functions correctly into my model.</p>
  
  <p>Good ideas are very welcome! :)</p>