title: Accumulate or build a prediction where the prediction output is an input to the next item
body: <p>Some data:</p>
  <pre><code>mydiamonds &lt;- diamonds %&gt;% 
    group_by(cut, color) %&gt;% 
    mutate(cumprice = cumsum(price)) %&gt;% 
    mutate(lag_cumprice = lag(cumprice)) %&gt;% 
    na.omit(.)
  </code></pre>
  <p>A model:</p>
  <pre><code>nonsense_model &lt;- glm(cumprice ~ depth + lag_cumprice, family = &quot;poisson&quot;, data = mydiamonds)
  </code></pre>
  <p>To predict with this model, the output of the prediction is also an input, since it uses lagged data. So I cannot just use predict. Here's an attempted custom function:</p>
  <pre><code>acumPredict &lt;- function(dta, mod, initial_amount) {
    
    # for each row in the dataframe, predict/build
    for(r in 1:nrow(dta)) {
      total_exponent &lt;-
        mod$coefficients['(Intercept)']  +
        (mod$coefficients['depth'] * dta$depth[r]) + 
        (mod$coefficients['lag_cumprice'] * initial_amount)
      predictced_cumprice = exp(total_exponent)
      #&lt;update initial_value for the list item here with predictced_cumprice&gt;
      return(predictced_cumprice)
    }
  }
  </code></pre>
  <p>And, here is a list of dataframes, which is just mydiamonds split by the grouping, along with an item for the initial_value of cumulative_price:</p>
  <pre><code>mylist &lt;- mydiamonds %&gt;% 
    group_split %&gt;% 
    map(~ list(dta = ., initial_val = min(.$cumprice)))
  </code></pre>
  <p>Now I have all the pieces. For each dataframe within mylist, I would like to mutate a new field <code>predicted_cumprice</code> that uses the function <code>acumPredict</code>. acumPredict takes as inputs the initial_amount which should start with the list items value of <code>initial_val</code> and then, on each row, accumulate or build predictions where the output of the prediction for <code>cumprice</code> is the input variable <code>lag_cumprice</code> for the next rows prediction.</p>
  <p>I hope my post is clear, it's tricky to communicate it well.</p>
  <p>I can visualize what I need, I just cannot code it. Put in other words, I need to update the value of <code>initial_val</code> for the list items on each iteration/row during <code>mutate(predicted_cumprice = acumPredict(...))</code> (assuming this is the 'right' approach?).</p>
  <p>How can I do this?</p>