title: Use multiple predictors in linear model with purrr map2 function
body: <p>My question is similar to <a href="https://stackoverflow.com/questions/44709870/using-lm-in-list-column-to-predict-new-values-using-purrr">this one</a>, but now I am trying to use a model with multiple predictors and I can't figure out how to get the newdata into the predict function.</p>
  
  <pre><code>library(dplyr)
  library(lubridate)
  library(purrr)
  library(tidyr)
  library(broom)
  
  set.seed(1234)
  </code></pre>
  
  <p>First I create a seq of weeks</p>
  
  <pre><code>wks = seq(as.Date("2010-01-01"), Sys.Date(), by="1 week")
  </code></pre>
  
  <p>Then I grab the current year</p>
  
  <pre><code>cur_year &lt;- year(Sys.Date())
  </code></pre>
  
  <p>Here I create a data frame with dummy data </p>
  
  <pre><code>my_data &lt;- data.frame(
    week_ending = wks
  ) %&gt;% 
    mutate(
      ref_period = week(week_ending),
      yr = year(week_ending),
      PCT.EXCELLENT = round(runif(length(wks), 0, 100),0),
      PCT.GOOD = round(runif(length(wks), 0, 100),0),
      PCT.FAIR = round(runif(length(wks), 0, 100),0),
      PCT.POOR = round(runif(length(wks), 0, 100),0),
      PCT.VERY.POOR = round(runif(length(wks), 0, 100),0),
      pct_trend = round(runif(length(wks), 75, 125),0)
    )
  </code></pre>
  
  <p>Next I create a nested dataframe that has the data for each week of the year as one group.</p>
  
  <pre><code>cond_model &lt;- my_data %&gt;% 
    filter(yr != cur_year) %&gt;% 
    group_by(ref_period) %&gt;% 
    nest(.key=cond_data) 
  </code></pre>
  
  <p>Here I join this year's data back into the previous years' data by week of the year.</p>
  
  <pre><code>cond_model &lt;- left_join(
    cond_model,
    my_data %&gt;% 
      filter(yr==cur_year) %&gt;% 
      select(week_ending,
             ref_period,
             PCT.EXCELLENT,
             PCT.FAIR,
             PCT.GOOD,
             PCT.POOR,
             PCT.VERY.POOR),
    by = c("ref_period")
  ) 
  </code></pre>
  
  <p>And this adds the linear model to the data frame for each week of the year</p>
  
  <pre><code>cond_model &lt;- 
    cond_model %&gt;% 
    mutate(model = map(cond_data,
                       ~lm(pct_trend ~ PCT.EXCELLENT + PCT.GOOD + PCT.FAIR + PCT.POOR + PCT.VERY.POOR, data = .x)))
  </code></pre>
  
  <p>now I would like to use the model for each week to predict using this year's data.  I tried the following:</p>
  
  <pre><code>cond_model &lt;- 
    cond_model %&gt;% 
    mutate(
      pred_pct_trend = map2_dbl(model, PCT.EXCELLENT + PCT.GOOD + PCT.FAIR + PCT.POOR + PCT.VERY.POOR,
                                ~predict(.x, newdata = data.frame(.y)))
    )
  </code></pre>
  
  <p>That gives the following error:</p>
  
  <pre><code>Error in mutate_impl(.data, dots) : object 'PCT.EXCELLENT' not found
  </code></pre>
  
  <p>I then tried nesting my predictors in my data frame...</p>
  
  <p>create a data frame with just this year's data and nest the predictors</p>
  
  <pre><code>cur_cond &lt;- my_data %&gt;% 
    filter(yr==cur_year) %&gt;% 
    select(week_ending, PCT.EXCELLENT,
           PCT.GOOD, PCT.FAIR, PCT.POOR, PCT.VERY.POOR) %&gt;% 
    group_by(week_ending) %&gt;% 
    nest(.key=new_data) %&gt;% 
    mutate(new_data=map(new_data, ~data.frame(.x)))
  </code></pre>
  
  <p>join this into my main data frame</p>
  
  <pre><code>cond_model &lt;- left_join(cond_model, cur_cond)
  </code></pre>
  
  <p>Now I try the prediction again:</p>
  
  <pre><code>cond_model &lt;- 
    cond_model %&gt;% 
    mutate(
      pred_pct_trend = map2_dbl(model, new_data,
                                ~predict(.x, newdata = data.frame(.y)))
    )
  </code></pre>
  
  <p>I get the same error as before:</p>
  
  <pre><code>Error in mutate_impl(.data, dots) : object 'PCT.EXCELLENT' not found
  </code></pre>
  
  <p>I think that the answer could involve performing a flatten() on the predictors, but I can't figure out where that goes in my workflow.</p>
  
  <pre><code>cond_model$new_data[1]
  </code></pre>
  
  <p>vs.</p>
  
  <pre><code>flatten_df(cond_model$new_data[1])
  </code></pre>
  
  <p>and at this point I have run out of ideas.</p>