将权重参数传递给R函数内的回归函数

时间:2014-11-20 17:03:23

标签: r function if-statement regression

我正在尝试编写一个R函数来运行加权(可选)回归,并且我很难让权重变量起作用。 这是该函数的简化版本。

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)
}

如果我不使用权重参数,该函数可以正常工作。

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

但是,当我使用权重参数时会抛出一条错误消息。

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

我谷歌问题,这篇文章似乎是最接近我的问题,但我仍然无法让它工作。 R : Pass argument to glm inside an R function。 任何帮助将不胜感激。 我的问题可以复制:

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.

HC中的错误(data = mydata,formula = lin.model,tau = 0.2,FUN = rq,weights = weig):   对象'weig'未找到

1 个答案:

答案 0 :(得分:0)

你有两个问题。您遇到的错误是因为您尝试使用weigh变量而未将其引用来自mydata数据集。尝试使用mydata$weig。这将解决您的第一个错误,但您获得与使用weights参数相关的实际错误,即:

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

解决方案是将HC的{​​{1}}参数中指定的变量添加到数据框,然后再将其传递给weights

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)
}