将权重参数传递给R函数内的回归函数 [英] Passing the weights argument to a regression function inside an R function
问题描述
我试图编写一个R函数来运行加权(可选)回归,并且我很难让权重变量正常工作。
这里是该函数的简化版本。
HC < - function(data,FUN,formula,tau (数据=数据,公式=公式,tau = tau)
截距= est $ coef [[(Intercept)]]
zeroWorker< - exp(截距)
}
else {
est< - FUN(data = data,formula =公式,tau = tau,权重=权重)
截距= est $ coef [[(截距)]]
zeroWorker< - exp(截距)
}
return(zeroWorker)
}
如果我不使用权值参数
mod1 < - HC(data = mydata,formula = lin.model,tau = 0.2,
FUN = rq)
但是,当我使用权重参数时抛出错误消息。 b = b
$ b
mod2 <-HC(data = mydata,formula = lin.model,tau = 0.2,
FUN = rq,weight = weig )
我谷歌的问题,这篇文章似乎是最接近我的问题,但我仍然无法得到它的工作。 R:将参数传递给R函数中的glm 。
任何帮助将不胜感激。
我的问题可以复制:
library(quantreg)
data(engel)
mydata < - engel
mydata $ weig < - with(mydata,log(sqrt(income)))#创建虚拟重量变量
lin.model< - foodexp〜income $ (数据= mydata,公式= lin.model,tau = 0.2,
FUN = rq)#这完美地适用于
mod2 <-HC(数据= mydata,公式= lin.model,tau = 0.2,
FUN = rq,weights = weig)#引发错误。
HC中的错误(data = mydata,formula = lin.model,tau = 0.2,FUN = rq ,weight = weig):
object'weig'not found
你有两个问题。您遇到的错误是因为您尝试使用权重
变量而不引用它来自 mydata
数据集。尝试使用 mydata $ weig
。这将解决你的第一个错误,但是你会得到一个与使用权重
参数有关的实际的错误:
< (公式=数据=数据,权重=替代(权重),:
变量的无效类型(符号)(权重) '
解决方法是添加 HC $ c在将数据传递给
FUN
:
之前,在数据框中使用$ c>的权重
HC < - 函数(数据,FUN,公式,tau = 0.5,权重= NULL){
data $ .weights < - 权重$如果(is.null(权重)){
est < - FUN(data = data,formula = formula,tau = tau)
} else {
est < - FUN(data = data,formula = formula,tau = tau,weights = .weights)
}
intercept = est $ coef [[(截距)]]
zeroWorker< exp(截获)
return(zeroWorker)
}
(数据= mydata,公式= lin.model,tau = 0.2,FUN = rq,其中k = 1, weight = mydata $ weig)
mod2
#[1] 4.697659e + 47
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
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
这篇关于将权重参数传递给R函数内的回归函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!