0

I'm a relatively new R user, and I'm trying to create a function to be used in a bootstrap of standard errors in a GLM in R. If I run the GLM by itself like this...

glm(glm.formula,
    family = tweedie(var.power=1.5,link.power=0),
    data=coll.train,
    subset=1:records,
    weights=EXPOSURE_COLL)

...it runs just fine. Currently the "1:records" is just selecting the entire data set. In the future I would like to replace it with a random sampling of the records.

However, if I take this same code and put it inside a function...

boot.fn=function(data,index){
      return(coef(glm(glm.formula,
                      family = tweedie(var.power=1.5,link.power=0),
                      data=data,
                      subset=index,
                      weights=EXPOSURE_COLL
                      )))  
}

...and call the function as follows...

boot.fn(coll.train,1:records)

...I get the following error message:

Error in eval(expr, envir, enclos) : object 'index' not found 

This seems fairly straightforward, so I'm a little puzzled as to what the issue is. The most frustrating thing about this is that I took the bones of this code from a textbook, so it should work. I'm not sure what I did to mess it up.

Any help anyone can provide is greatly appreciated!

www
  • 38,575
  • 12
  • 48
  • 84
user42719
  • 41
  • 1
  • 3
  • This will be a scoping problem. You dont show it but you will be passing the formula somehow, which will cause an issue.. So wrap with `return(eval(substitute(coef(... plus the rest of your code` – user20650 Feb 08 '16 at 21:28
  • 1
    That was it - thanks! – user42719 Feb 09 '16 at 14:31

0 Answers0