[R] Problem with na.action within own function

ivan
Thu, 08 Aug 2013 08:19:09 -0000

Dear R Community,

I am trying to build a very simple function which uses lm and coeftest to
return a coefficient matrix with heteroskedasticity robust standard errors.
The  function is the following:

reg=function(formula,data,na.action){
  res=lm(formula=formula,data=data,na.action=na.action)
  hc3=coeftest(res, vcov = vcovHC(res, type = "HC3"))
  residuals=resid(res)
  return(list("coef.hc3"=hc3,"R2"=summary(res)$r.squared,
    "R2.adj"=summary(res)$adj.r.squared,
"residuals"=residuals))
}

The function works perfect as long as the data contains no missing values.
I.e.

test1=seq(1,30,1)
test2=seq(1,30,1)
testdata=data.frame(test1,test2)
reg(formula=test1~test2, data=testdata, na.action=na.exclude)

However, as soon as I have a missing value, it does not work any more (the
error message is: Error in estfun(x)/X : non-conformable arrays):

test1=seq(1,30,1)
test2=seq(1,30,1)
test2[5]=NA
testdata=data.frame(test1,test2)
reg(formula=test1~test2, data=testdata, na.action=na.exclude)

My feeling is that it has something to do with na.exclude being a function
itself and hence R having a problem with inputing a function into another
function. Does anyone have an idea?

Thanks a lot!!!
Regards

	[[alternative HTML version deleted]]