title: using lm in list column to predict new values using purrr
body: <p>I am trying to add a column of predictions to a dataframe that has a list column that contains an lm model.  I adopted some of the code from <a href="https://stackoverflow.com/a/42799097/4734091">this post</a>.</p>
  
  <p>I have made a toy example here:</p>
  
  <pre><code>library(dplyr)
  library(purrr)
  library(tidyr)
  library(broom)
  
  set.seed(1234)
  
  exampleTable &lt;- data.frame(
    ind = c(rep(1:5, 5)),
    dep = rnorm(25),
    groups = rep(LETTERS[1:5], each = 5)
  ) %&gt;%
  group_by(groups) %&gt;%
  nest(.key=the_data) %&gt;%
  mutate(model = the_data %&gt;% map(~lm(dep ~ ind, data = .))) %&gt;%
  mutate(Pred = map2(model, the_data, predict))
  
  exampleTable &lt;- exampleTable %&gt;%
    mutate(ind=row_number())
  </code></pre>
  
  <p>that gives me a tibble that looks like this:</p>
  
  <pre><code># A tibble: 5 × 6
    groups         the_data    model      Pred   ind 
    &lt;fctr&gt;           &lt;list&gt;   &lt;list&gt;    &lt;list&gt; &lt;int&gt; 
  1      A &lt;tibble [5 × 2]&gt; &lt;S3: lm&gt; &lt;dbl [5]&gt;     1 
  2      B &lt;tibble [5 × 2]&gt; &lt;S3: lm&gt; &lt;dbl [5]&gt;     2 
  3      C &lt;tibble [5 × 2]&gt; &lt;S3: lm&gt; &lt;dbl [5]&gt;     3 
  4      D &lt;tibble [5 × 2]&gt; &lt;S3: lm&gt; &lt;dbl [5]&gt;     4 
  5      E &lt;tibble [5 × 2]&gt; &lt;S3: lm&gt; &lt;dbl [5]&gt;     5 
  </code></pre>
  
  <p>to get a predicted value using the lm model for a specific group I can use this:</p>
  
  <pre><code>predict(exampleTable[1,]$model[[1]], slice(exampleTable, 1) %&gt;% select(ind))
  </code></pre>
  
  <p>which produces this result:</p>
  
  <pre><code>&gt; predict(exampleTable[1,]$model[[1]], slice(exampleTable, 1) %&gt;% select(ind))
           1 
  -0.4822045
  </code></pre>
  
  <p>I would like to have one new prediction for each group.  I tried using purrr to get what I wanted:</p>
  
  <pre><code>exampleTable %&gt;%
    mutate(Prediction = map2(model, ind, predict))
  </code></pre>
  
  <p>but that gives the following error:</p>
  
  <pre><code>Error in mutate_impl(.data, dots) : object 'ind' not found
  </code></pre>
  
  <p>I was able to get the result I wanted with the following monstrosity:</p>
  
  <pre><code>exampleTable$Prediction &lt;- NA
  
  for(loop in seq_along(exampleTable$groups)){
    lmod &lt;- exampleTable[loop, ]$model[[1]]
    obs &lt;- filter(exampleTable, row_number()==loop) %&gt;%
      select(ind)
    exampleTable[loop, ] $Prediction &lt;- as.numeric(predict(lmod, obs))
  }
  </code></pre>
  
  <p>that gives me a tibble that looks like this:</p>
  
  <pre><code># A tibble: 5 × 6
    groups         the_data    model      Pred   ind Prediction
    &lt;fctr&gt;           &lt;list&gt;   &lt;list&gt;    &lt;list&gt; &lt;int&gt;      &lt;dbl&gt;
  1      A &lt;tibble [5 × 2]&gt; &lt;S3: lm&gt; &lt;dbl [5]&gt;     1 -0.4822045
  2      B &lt;tibble [5 × 2]&gt; &lt;S3: lm&gt; &lt;dbl [5]&gt;     2 -0.1357712
  3      C &lt;tibble [5 × 2]&gt; &lt;S3: lm&gt; &lt;dbl [5]&gt;     3 -0.2455760
  4      D &lt;tibble [5 × 2]&gt; &lt;S3: lm&gt; &lt;dbl [5]&gt;     4  0.4818425
  5      E &lt;tibble [5 × 2]&gt; &lt;S3: lm&gt; &lt;dbl [5]&gt;     5 -0.3473236
  </code></pre>
  
  <p>There must be a way to do this in a 'tidy' way, but I just cant crack it.</p>