1

我正在尝试编写一些函数来简化多个模型的调整,但发现它很痛苦,因为 R 无法找到正确的data,当它深入评估树时。尽管努力将公式环境存储在模型中,但我想确实没有办法明确地指向原始数据对象。这对于使用 拟合生存曲线变得更加困难survfit,因为其中没有terms存储任何对象。

我真的需要每次都重新输入数据/公式作为参数吗?

例子:

# model-fitting wrapper function
fn <- function(fn_formula, fn_data) {
    lm(formula = fn_formula, data = fn_data)
}
# specify exemplary data and formula
data <- data.frame(
    y = rnorm(100),
    x1 = rnorm(100),
    x2 = rnorm(100))
formula <- y ~ x1

# try to create and update the fit with different parameters
fn_fit <- fn(formula, data)
update(fn_fit, ~ x2)
# Error in is.data.frame(data) : object 'fn_data' not found
terms(fn_fit) %>% attr('.Environment')
# <environment: R_GlobalEnv>
terms(fn_fit$model) %>% attr('.Environment')
# <environment: R_GlobalEnv>
getCall(fn_fit)
# lm(formula = fn_formula, data = fn_data)
4

2 回答 2

3

存储数据的变量应该在同名的范围lm()update()。不确定你真正想要完成什么,如果你想要一个创建可以在全局环境中使用的签名的函数,你可以做这样的事情

fn <- function(fn_formula, fn_data) {
  do.call("lm", list(fn_formula, data=substitute(fn_data)))
}
fn_fit <- fn(formula, data)
update(fn_fit, ~ x2)

否则,如果您真的想在本地函数范围内捕获该变量,您可以创建一个助手来在正确的环境中进行有趣的更新。

fn <- function(fn_formula, fn_data) {
  environment(fn_formula) <- environment()
  lm(formula = fn_formula, data = fn_data)
}

fn_update <- function(object, ...) {
  mc<-match.call(definition = update)
  mc[[1]] <- quote(update)
  eval(mc, envir=environment(terms(object)))
}

fn_fit <- fn(formula, data)
fn_update(fn_fit, ~x2)
于 2017-06-26T22:15:32.937 回答
1

When you passed formula, the only items stored in the ['model'] sublist were those that were needed.

> names(fn_fit$model)
[1] "y"  "x1"

But there's nothing named either 'data' or 'fn_data' in that object. MrFlick second suggestion is more resilient to modifications in the calling tree of frames:

> fn <- function(fn_formula, fn_data) {
+   do.call("lm", list(fn_formula, data=substitute(fn_data)))
+ }
> fn_fit <- fn(formula, data); rm(data)  # mess with the calling environment
> update(fn_fit, ~ x2)
Error in terms.formula(formula, data = data) : 
  'data' argument is of the wrong type

That error occurred because the R interpreter only found the function named data; if instead you deploy the second option you get:

> data <- data.frame(
+     y = rnorm(100),
+     x1 = rnorm(100),
+     x2 = rnorm(100))

> fn <- function(fn_formula, fn_data) {
+   environment(fn_formula) <- environment()
+   lm(formula = fn_formula, data = fn_data)
+ }
> 
> fn_update <- function(object, ...) {
+   mc<-match.call(definition = update)
+   mc[[1]] <- quote(update)
+   eval(mc, envir=environment(terms(object)))
+ }

> 
> fn_fit <- fn(formula, data) ; rm(data)
> fn_update(fn_fit, ~x2)

Call:
lm(formula = y ~ x2, data = fn_data)

Coefficients:
(Intercept)           x2  
    0.01117     -0.13004  
于 2017-06-27T04:28:28.927 回答