在rpart模型中应用权重会导致错误 [英] Apply weights in rpart model gives error

查看:197
本文介绍了在rpart模型中应用权重会导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 rpart 包来适应某些模型,如下所示:

 < code $ fitmodel = function(formula,data,w){

fit = rpart(公式,数据,权重= w)
}



调用自定义函数



  fit = fitmodel(y〜x1 + x2,data,w)

这会导致错误:


$ b

eval中的错误(expr,envir,enclos):object'w'not found



然后我决定使用

  fitmodel = function(formula,data,w){

data $ w = w
fit = rpart(公式,数据,权重= w)
}

这有效,但还有另外一个问题:

这会起作用



 fit = fitmodel(y〜x1 + x2,data,w)



< h3>这不起作用

  fit = fitmodel(y〜。,data,w)

E rval in eval(expr,envir,enclos):object'w'not found



在自定义函数中应用权重的正确方法是什么?谢谢!

解决方案

希望其他人给出更完整的答案。为什么 rpart 找不到 w 的原因是 rpart 在数据,权重等等的环境中搜索定义公式的环境。公式在最有可能是 GlobalEnv w的环境中创建是在其他功能中创建的。将公式的环境更改为 w parent.frame 修复的环境。 rpart 仍然可以找到数据,因为搜索路径将始终继续到 GlobalEnv 。我不确定为什么 sys.frame(sys.nframe())可以工作,因为环境不一样,但显然 w 仍然在搜索路径上

编辑: sys.frame(sys.nframe())似乎与将forumla的环境设置为调用函数 rpart 的环境相同( foo3 在这个例子中)。在这种情况下, rpart 会查找 w data ,等等在 foo3 中,然后 bar3 ,然后 GlobalEnv


  library(rpart)
数据(虹膜)

bar < - 函数(公式,数据){
w< - rpois(nrow(iris),1)
print(environment())
foo(formula,data,w)
}
$ b $ f foo < - 函数(公式,数据,w){
print(环境(公式))
fit < - rpart(公式,数据,权重= w)
返回(适合)
}


bar(I(Species ==versicolor)〜。,data = iris)
##< environment :0x1045b1a78>
##< environment:R_GlobalEnv>
## eval(expr,envir,enclos)中的错误(来自#2):找不到对象'w'


bar2 < - 函数(公式,数据) {
w < - rpois(nrow(iris),1)
print(environment())
foo2(formula,data,w)
}

foo2 < - function(formula,data,w){
print(environment(formula))
environment(formula)< - parent.frame()
print(environment(公式))
fit< - rpart(formula,data,weights = w)
return(fit)
}

bar2(I(Species == versicolor)〜。,data = iris)
##< environment:0x100bf5910>
##< environment:R_GlobalEnv>
##< environment:0x100bf5910>


bar3 < - 函数(公式,数据){
w < - rpois(nrow(iris),1)
print(environment())
foo3(formula,data,w)
}

foo3 < - function(formula,data,w){
print(environment(formula))$ b $环境(公式)< - environment()##似乎与sys.frame(sys.nframe())
print(environment(公式))
print(environment() )
fit< - rpart(formula,data,weights = w)
return(fit)
}

bar3(I(Species ==versicolor )〜。,data = iris)
##<环境:0x104e11bb8>
##< environment:R_GlobalEnv>
##<环境:0x104b4ff78>
##<环境:0x104b4ff78>


I'm using the rpart package to fit some models, like this:

fitmodel = function(formula, data, w) {

    fit = rpart(formula, data, weights = w)
}

Call the custom function

fit = fitmodel(y ~ x1 + x2, data, w)

This causes the error:

Error in eval(expr, envir, enclos) : object 'w' not found

Then i decided to use

fitmodel = function(formula, data, w) {

    data$w = w
    fit = rpart(formula, data, weights = w)
}

This works, but there's another problem:

This will work

fit = fitmodel(y ~ x1 + x2, data, w)

This does not work

fit = fitmodel(y ~ ., data, w)

Error in eval(expr, envir, enclos) : object 'w' not found

What's the correct way to apply weights inside a custom function? Thanks!

解决方案

Hopefully someone else gives a more complete answer. The reason why rpart can't find w is that rpart searches the environment that the formula is defined in for data, weights, etc. The formula is created in some environment most likely the GlobalEnv and the w is created within some other function. Changing the environment of the formula to the environment where w is created with parent.frame fixes that. rpart can still find the data since the search path will always continue to the GlobalEnv. I'm not sure why the sys.frame(sys.nframe()) works since the environments aren't the same but apparently w is still somewhere on the search path

edit: sys.frame(sys.nframe()) seems to be the same as setting the environment of the forumla to the environment of the function rpart is called in (foo3 in this example). In that case, rpart looks for w, data, etc. in foo3, then bar3 then the GlobalEnv.

library(rpart)
data(iris)

bar <- function(formula, data) {
   w <- rpois(nrow(iris), 1)
   print(environment())
   foo(formula, data, w)
}

foo <- function(formula, data, w) {
  print(environment(formula))
  fit <- rpart(formula, data, weights = w)
  return(fit)
}


bar(I(Species == "versicolor") ~ ., data = iris)
## <environment: 0x1045b1a78>
## <environment: R_GlobalEnv>
## Error in eval(expr, envir, enclos) (from #2) : object 'w' not found


bar2 <- function(formula, data) {
  w <- rpois(nrow(iris), 1)
  print(environment())
  foo2(formula, data, w)
}

foo2 <- function(formula, data, w) {
  print(environment(formula))
  environment(formula) <- parent.frame()
  print(environment(formula))
  fit <- rpart(formula, data, weights = w)
  return(fit)
}

bar2(I(Species == "versicolor") ~ ., data = iris)
## <environment: 0x100bf5910>
## <environment: R_GlobalEnv>
## <environment: 0x100bf5910>


bar3 <- function(formula, data) {
  w <- rpois(nrow(iris), 1)
  print(environment())
  foo3(formula, data, w)
}

foo3 <- function(formula, data, w) {
  print(environment(formula))
  environment(formula) <- environment() ## seems to be the same as sys.frame(sys.nframe())
  print(environment(formula))
  print(environment())
  fit <- rpart(formula, data, weights = w)
  return(fit)
}

bar3(I(Species == "versicolor") ~ ., data = iris)
## <environment: 0x104e11bb8>                                                                                                                                                                                                                 
## <environment: R_GlobalEnv>                                                                                                                                                                                                                 
## <environment: 0x104b4ff78>                                                                                                                                                                                                                 
## <environment: 0x104b4ff78>

这篇关于在rpart模型中应用权重会导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆