R 中的更新函数和变量访问
update function in R and variables access
我需要更新函数内的模型公式。这是一个例子:
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")))
编辑
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
。参数名称是 old
和 new
。在 update.default
中,名称是 model
和 formula.
,就像您现在使用的那样。
还使用变通方法将正确的变量名称输入到公式中
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
我需要更新函数内的模型公式。这是一个例子:
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")))
编辑
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
。参数名称是 old
和 new
。在 update.default
中,名称是 model
和 formula.
,就像您现在使用的那样。
还使用变通方法将正确的变量名称输入到公式中
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