title: mgcv::gamm() and MuMIn::dredge() errors
body: <p>I've been trying to fit multiple GAMs using the package <code>mgcv</code> within a function, and crudely select the most appropriate model through model selection procedures. But my function runs the first model then doesn't seem to recognise the input data <code>dat</code> again.</p>
  
  <p>I get the error</p>
  
  <blockquote>
    <p>Error in is.data.frame(data) : object 'dat' not found. </p>
  </blockquote>
  
  <p>I think this is a scoping problem and I've looked <a href="https://stackoverflow.com/questions/20298754/r-how-to-solve-error-in-is-data-framexobject-not-found">here</a>, and <a href="https://stackoverflow.com/questions/28148533/is-data-framedata-object-not-found-in-function-context">here</a> for help but cannot figure it out.</p>
  
  <p>Code and data are as follows (hopefully reproducible): 
  <a href="https://github.com/cwaldock1/Help/blob/master/test_gam.csv" rel="nofollow noreferrer">https://github.com/cwaldock1/Help/blob/master/test_gam.csv</a></p>
  
  <pre><code>library(mgcv)
  
  # Function to fit multiple models 
  best.mod &lt;- function(dat) {
  
  # Set up control structure
  ctrl &lt;- list(niterEM = 0, msVerbose = TRUE, optimMethod="L-BFGS-B")
  
  # AR(1)
  m1 &lt;- get.models(dredge(gamm(Temp ~ s(Month, bs = "cc") + s(Date, bs = 'cr') + Year,
           data = dat, correlation = corARMA(form = ~ 1|Year, p = 1),
           control = ctrl)), subset=1)[[1]]
  
  # AR(2)
  m2 &lt;- get.models(dredge(gamm(Temp ~ s(Month, bs = "cc") + s(Date, bs = 'cr') + Year,
           data = dat, correlation = corARMA(form = ~ 1|Year, p = 2),
           control = ctrl)), subset=1)[[1]]
  
  # AR(3)
  m3 &lt;- get.models(dredge(gamm(Temp ~ s(Month, bs = "cc") + s(Date, bs = 'cr') + Year,
           data = dat, correlation = corARMA(form = ~ 1|Year, p = 3),
           control = ctrl)), subset = 1)[[1]]
  
  
  ### Select best model to work with based on unselective AIC criteria 
  if(AIC(m2$lme) &gt; AIC(m1$lme)){mod = m1}else{mod = m2} 
  if(AIC(mod$lme) &gt; AIC(m3$lme)){mod = m3}else{mod = mod}
  
  return(mod$gam)
  }
  
  mod2 &lt;- best.mod(dat = test_gam)
  </code></pre>
  
  <p>Any help would be greatly appreciated. </p>
  
  <p>Thanks,
  Conor</p>