【问题标题】:update function in R and variables accessR中的更新函数和变量访问
【发布时间】:2015-02-08 22:14:49
【问题描述】:

我需要更新函数内的模型公式。这是一个例子:

A <- runif(n = 200)             #  generate some data
B <- runif(n = 200)
P <- 1/(1+exp(.5-A))            #  generate event probability
outcome <- runif(n = 200) < P   #  generate outcome

my.function <- function(model, data.to.add) {   # this is the function for updating the formula
  new.model <- update(object = model, formula. = ~ . + data.to.add)
  return (new.model)
}

test <- my.function(model = glm(outcome ~ B, family = binomial(link="logit")), data.to.add = A) 

不幸的是,执行此代码会引发如下错误:

Error in eval(expr, envir, enclos) : object 'data.to.add' not found 

似乎my.function 无法将变量data.to.add 的值提供给update 函数。我可以做些什么来为另一个函数中的update 函数提供正确的变量范围?

编辑:好的,如果要传递给要更新的函数的变量在全局环境中,那么您的解决方案很好,现在如果我必须在函数内定义变量,由于范围有限,我再次遇到错误变量:

A <- runif(n = 200)             #  generate some data
P <- 1/(1+exp(.5-A))            #  generate event probability
outcome <- runif(n = 200) < P   #  generate outcome

nested.update<-function(model) {
  B<-runif(n = 200)
  my.function <- function(model, data.to.add) {   # this is the function for updating the formula
    data.to.add <- paste('. ~ . +', deparse(substitute(data.to.add)), sep = "")
    new.model <- update(object = model, formula. = data.to.add)
    return (new.model)
  }
  return(my.function(model = model, data.to.add = B))
}

nested.update(model = glm(outcome ~ A, family = binomial(link="logit")))

【问题讨论】:

    标签: r scoping


    【解决方案1】:

    编辑

    my.function <- function(model, data.to.add) {   # this is the function for updating the formula
      data.to.add <- sprintf('. ~ . + %s', deparse(substitute(data.to.add)))
      new.model <- update(object = model, formula. = data.to.add)
      return (new.model)
    }
    
    my.function(lm(mpg ~ wt, data = mtcars), disp)
    
    # Call:
    #   lm(formula = mpg ~ wt + disp, data = mtcars)
    # 
    # Coefficients:
    #   (Intercept)           wt         disp  
    #      34.96055     -3.35083     -0.01772 
    
    my.function(lm(mpg ~ wt, data = mtcars), hp)
    
    # Call:
    #   lm(formula = mpg ~ wt + hp, data = mtcars)
    # 
    # Coefficients:
    #   (Intercept)           wt           hp  
    #      37.22727     -3.87783     -0.03177 
    

    不好的答案:

    R 正在调度 update.formula 而不是默认的 update.default,因为您将公式传递给 update。参数名称为oldnew。在update.default 中,名称是modelformula.,就像您现在使用的一样。

    还使用变通方法将正确的变量名放入公式中

    my.function <- function(model, data.to.add) {   # this is the function for updating the formula
      data.to.add <- sprintf('. ~ . + %s', deparse(substitute(data.to.add)))
      new.model <- update(old = model, new = data.to.add)
      return (new.model)
    }
    
    my.function(y ~ a, b)
    # y ~ a + b
    my.function(y ~ a, c)
    # y ~ a + c
    

    【讨论】:

    • 我仍然得到一个错误:Error in getCall(object) : argument "object" is missing, with no default,可能是因为model 的参数不是formula 对象而是一个完整的模型对象(如我之前的代码示例)。我需要传递完整的模型而不是简单的公式,因为我必须动态地改变它
    • 啊,我明白了,我的错。见编辑。相同的 cmets 和原则适用 @NicolaDinapoli
    • 我在问题中添加了一个更新,以解决有关在函数内部而不是在全局环境中声明的变量的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    • 2018-06-08
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    相关资源
    最近更新 更多