Passing the weights argument to a regression function inside an R function

自作多情 提交于 2019-12-13 06:54:15

问题


I am trying to write an R function to run a weighted (optional) regressions, and I am having difficulties getting the weight variable to work. Here is a simplified version of the function.

HC <- function(data, FUN, formula, tau = 0.5, weights = NULL){
if(is.null(weights)){
est <- FUN(data = data, formula = formula, tau = tau)
intercept = est$coef[["(Intercept)"]]
zeroWorker <- exp(intercept)
}
 else {
est <- FUN(data = data, formula = formula, tau = tau, weights = weights)
intercept = est$coef[["(Intercept)"]]
zeroWorker <- exp(intercept)
}
return(zeroWorker)
}

The function works perfectly if I do not use the weights argument.

mod1 <- HC(data = mydata, formula = lin.model, tau = 0.2, 
       FUN = rq)

But, throws an error message when I use the weights argument.

mod2 <- HC(data = mydata, formula = lin.model, tau = 0.2, 
       FUN = rq, weights = weig)

I google the problem, and this post seems to be the closest to my problem, but I could still not get it to work. R : Pass argument to glm inside an R function. Any help will be appreciated. My problem can be replicated with:

library("quantreg")
data(engel)
mydata <- engel
mydata$weig <- with(mydata, log(sqrt(income))) # Create a fictive weigth variable
lin.model <- foodexp~income
mod1 <- HC(data = mydata, formula = lin.model, tau = 0.2, 
       FUN = rq) # This works perfectly
mod2 <- HC(data = mydata, formula = lin.model, tau = 0.2, 
       FUN = rq, weights = weig) # throws an error.

Error in HC(data = mydata, formula = lin.model, tau = 0.2, FUN = rq, weights = weig) : object 'weig' not found


回答1:


You have two problems. The error you're encountering is because you're trying to use the weigh variable without referencing it as coming from the mydata dataset. Try using mydata$weig. This will solve your first error, but you then get the actual one related to using the weights argument, which is:

Error in model.frame.default(formula = formula, data = data, weights = substitute(weights),  : 
invalid type (symbol) for variable '(weights)'

The solution is to add the variable specified in HC's weights argument to the dataframe before passing it to FUN:

HC <- function(data, FUN, formula, tau = 0.5, weights = NULL){
  data$.weights <- weights
  if(is.null(weights)){
    est <- FUN(data = data, formula = formula, tau = tau)
  } else {
    est <- FUN(data = data, formula = formula, tau = tau, weights = .weights)
  }
  intercept = est$coef[["(Intercept)"]]
  zeroWorker <- exp(intercept)
  return(zeroWorker)
}

Then everything works:

mod2 <- HC(data = mydata, formula = lin.model, tau = 0.2, FUN = rq, weights = mydata$weig)
mod2
# [1] 4.697659e+47


来源:https://stackoverflow.com/questions/27045340/passing-the-weights-argument-to-a-regression-function-inside-an-r-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!