title: `emmeans` for a gls model doesn't run inside `map`
body: <p>This question is inspired by <a href="https://stackoverflow.com/questions/48576160/cant-use-emmeans-inside-map?noredirect=1&amp;lq=1">can&#39;t use emmeans inside map</a>, and related to <a href="https://stackoverflow.com/questions/65282669/map-joint-tests-to-a-list-after-fitting-a-gls-model">Map `joint_tests` to a list after fitting a `gls` model</a> and <a href="https://stackoverflow.com/questions/65334449/group-by-and-keep-grouping-levels-as-nested-data-frames-name">`group_by` and keep grouping levels as nested data frame&#39;s name</a></p>
  <p>I want  to wrap several tests into one workflow.</p>
  <p>This works, for a <code>glm</code> model.</p>
  <pre><code>library(dplyr)
  library(purrr)
  library(emmeans)
  library(nlme)
  
  diamond_result &lt;- diamonds %&gt;%
    group_by(cut) %&gt;%
    nest() %&gt;% 
    ungroup %&gt;%
    dplyr::mutate(models=map(data,~glm(price ~ x + y + z + clarity + color,data=.x)),
           jt = map(models, ~emmeans::joint_tests(.x, data = .x$data)),
           means=map(models,~emmeans::emmeans(.x,&quot;color&quot;,data=.x$data)),
           p_cont = map(means, ~emmeans::contrast(.x, &quot;pairwise&quot;,infer = c(T,T))),
           across(models:p_cont, stats::setNames,  .$cut)) 
  &gt; diamond_result$jt
  $Ideal
   model term df1 df2 F.ratio p.value
   x            1 Inf 611.626 &lt;.0001 
   y            1 Inf   2.914 0.0878 
   z            1 Inf 100.457 &lt;.0001 
   clarity      7 Inf 800.852 &lt;.0001 
   color        6 Inf 256.796 &lt;.0001 
  
  
  $Premium
   model term df1 df2  F.ratio p.value
   x            1 Inf 2074.371 &lt;.0001 
  </code></pre>
  <p>But the same syntax doesn't work for a <code>gls</code> model so I stopped at <code>emmeans()</code> step. Eventually, I want <code>joint_tests</code>, <code>emmeans</code>, and <code>contrast</code> in the <code>mutate</code> step.</p>
  <pre><code>diamonds_emm2 &lt;-  diamonds %&gt;%
    group_by(cut) %&gt;% 
    nest() %&gt;%
    ungroup() %&gt;%
    dplyr::mutate(models=map(data,~gls(price ~ x + y + z + clarity,  
                                weights = varIdent(form = ~ 1|color),
                                data =.x)),
                  means=map(models,~emmeans::emmeans(.x,&quot;clarity&quot;,data=.x$data)),
                  across(models:p_cont, setNames,  .$cut))
      Error: Problem with `mutate()` input `means`.
      x undefined columns selected
      ℹ Input `means` is `map(models, ~emmeans::emmeans(.x, &quot;clarity&quot;, data = .x$data))`.
      Run `rlang::last_error()` to see where the error occurred.
  &lt;error/dplyr:::mutate_error&gt;
  Problem with `mutate()` input `means`.
  x undefined columns selected
  ℹ Input `means` is `map(models, ~emmeans::emmeans(.x, &quot;clarity&quot;, data = .x$data))`.
  Backtrace:
    1. `%&gt;%`(...)
   18. base::.handleSimpleError(...)
   19. dplyr:::h(simpleError(msg, call))
  
  &lt;error/dplyr:::mutate_error&gt;
  Problem with `mutate()` input `means`.
  x undefined columns selected
  ℹ Input `means` is `map(models, ~emmeans::emmeans(.x, &quot;clarity&quot;, data = .x$data))`.
  Backtrace:
       █
    1. ├─`%&gt;%`(...)
    2. ├─dplyr::mutate(...)
    3. ├─dplyr:::mutate.data.frame(...)
    4. │ └─dplyr:::mutate_cols(.data, ...)
    5. │   ├─base::withCallingHandlers(...)
    6. │   └─mask$eval_all_mutate(dots[[i]])
    7. ├─purrr::map(models, ~emmeans::emmeans(.x, &quot;clarity&quot;, data = .x$data))
    8. │ └─.f(.x[[i]], ...)
    9. │   └─emmeans::emmeans(.x, &quot;clarity&quot;, data = .x$data)
   10. │     ├─base::do.call(ref_grid, args)
   11. │     └─(function (object, at, cov.reduce = mean, cov.keep = get_emm_option(&quot;cov.keep&quot;), ...
   12. │       ├─emmeans::recover_data(object, data = as.data.frame(data), ...)
   13. │       └─emmeans:::recover_data.gls(...)
   14. │         └─emmeans:::recover_data.call(...)
   15. │           ├─tbl[, vars, drop = FALSE]
   16. │           └─base::`[.data.frame`(tbl, , vars, drop = FALSE)
   17. │             └─base::stop(&quot;undefined columns selected&quot;)
   18. └─base::.handleSimpleError(...)
   19.   └─dplyr:::h(simpleError(msg, call))
  </code></pre>
  <p>The code runs OK at this step.</p>
  <pre><code>diamonds_emm &lt;-  diamonds %&gt;%
    group_by(cut) %&gt;% nest() %&gt;%
    mutate(models=map(data,~gls(price ~ x + y + z + clarity,  
                                weights = varIdent(form = ~ 1|color),
                                data =.x))) 
  </code></pre>
  <p>How do I work around this problem? Thank you.</p>
  <p>Update: the <code>map2</code> function from Ronak's answer solved the problem at the <code>means</code> steps but it won't do pairwise contrasts. What did I miss?</p>
  <pre><code>diamonds %&gt;%
    group_by(cut) %&gt;% 
    nest() %&gt;%
    mutate(models=map(data,~gls(price ~ x + y + z + clarity,  
                                weights = varIdent(form = ~ 1|color),
                                data =.x)), 
           means = map2(data, models,~emmeans::emmeans(.y,&quot;clarity&quot;,data=.x)),
           p_cont = map2(means, ~emmeans::contrast(.y, &quot;pairwise&quot;,infer = c(T,T)))) %&gt;%
    ungroup %&gt;%
    mutate(across(models:p_cont, setNames,  .$cut)) -&gt; result
  Error: Problem with `mutate()` input `p_cont`.
  x object '.z' not found
  ℹ Input `p_cont` is `map(means, ~emmeans::contrast(.y, &quot;pairwise&quot;, infer = c(T, T)))`.
  ℹ The error occurred in group 1: cut = &quot;Fair&quot;.
  </code></pre>
  <p>Giving a new name to the input at the <code>p_cont</code> step, such as <code> ~emmeans::contrast(.z, &quot;pairwise&quot;, infer = c(T, T)))</code> didn't solve the problem.</p>