0

Using package nlme, I have model:

gls(temp.avg ~ year, data = df, method = 'ML')

This does exactly what I expect it to. However, when I create the model within a function with changing data argument for the gls function, my model no longer uses data 'df' but instead just prints "dat". Here is the function:

function(dat) { gls(temp.avg ~ year, data = dat, method = 'ML') }

Here is what it looks like when I view the model when created (first) outside of my wrapper function and then (second) when inside the function [Notice the "Data" line]:

Generalized least squares fit by maximum likelihood
  Model: temp.avg ~ year 
  Data: df 
  Log-likelihood: -3877.052

Coefficients:
   (Intercept)         (year) 
  15.135135363   -0.008796849 

Degrees of freedom: 1116 total; 1114 residual
Residual standard error: 7.807791

##########################

Generalized least squares fit by maximum likelihood
  Model: temp.avg ~ year
  Data: dat 
  Log-likelihood: -3877.052

Coefficients:
   (Intercept)         (year)  
  15.135135363   -0.008796849 

Degrees of freedom: 1116 total; 1114 residual
Residual standard error: 7.807791 

I'd really rather it not do that.

How can I get the function to carry over what I assign as dat vs "dat" itself?

theforestecologist
  • 4,667
  • 5
  • 54
  • 91
  • If would be helpful if you provided a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). That name is likely stored on the object somewhere and could be replaced. But `gls()` isn't a base function so you should be clear what non-standard packages you are using – MrFlick Mar 08 '16 at 14:57
  • Why do you care about this? – Roland Mar 08 '16 at 14:59
  • @Roland: b/c otherwise I won't be sure which data set the model was created from. – theforestecologist Mar 08 '16 at 15:06
  • Well, there are probably better ways to keep track of that, but I've added an answer. No warranties that the resulting model is fully functional. There could be dragons (although I don't expect them). – Roland Mar 08 '16 at 15:07

1 Answers1

1
library(nlme)
fun <- function(dat) { 
  res <- gls(follicles ~ sin(2*pi*Time) + cos(2*pi*Time), dat) 
  res$call$data <- substitute(dat)
  res
}
fun(Ovary)
#Generalized least squares fit by REML
#  Model: follicles ~ sin(2 * pi * Time) + cos(2 * pi * Time) 
#  Data: Ovary 
#  Log-restricted-likelihood: -898.434
#
#Coefficients:
#       (Intercept) sin(2 * pi * Time) cos(2 * pi * Time) 
#        12.2155822         -3.3396116         -0.8697358 
#
#Degrees of freedom: 308 total; 305 residual
#Residual standard error: 4.486121 
Roland
  • 127,288
  • 10
  • 191
  • 288