Functions and GLMs in R

44 views Asked by At

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!

0

There are 0 answers