r - 我应该如何在 R 中调用 model.frame?

标签 r scope modeling

我正在尝试在 R 中编写自己的建模函数,它需要一个公式、一些数据,也许还有一些额外的上下文,比如权重;调用 model.frame 后要提取必要的数字数据,它将执行拟合。我的第一遍看起来像:

my_modfunc <- function(formula,data,weights=NULL) {
    mf <- model.frame(formula,data=data,weights=weights)
    wt <- model.weights(mf)
    # do some fitting here...
}

# make fake data to test it
set.seed(1234)
data <- data.frame(x1=rnorm(50),x2=rnorm(50),y=rnorm(50),w=runif(50))

# call it:
my_modfunc(y ~ x1 + x2,data=data,weights=w)

这失败了,我收到错误: Error in model.frame.default(formula, data = data, weights = weights) : invalid type (closure) for variable '(weights)'
同样,如果我打电话
my_modfunc(y ~ x1 + x2,data=data,weights='w')

我犯了同样的错误。我怀疑环境,引用等存在一些问题。

剪切和粘贴 lm 的源代码,我可以将我的函数重写为
# based on lm
weird_modfunc <- function(formula,data,weights=NULL ) {
    cl <- match.call()  # what?
    mf <- match.call(expand.dots = FALSE)  # what??
    m <- match(c("formula", "data", "weights"), names(mf), 0L)
    mf <- mf[c(1L, m)]  # ??
    mf$drop.unused.levels <- TRUE # ??
    mf[[1L]] <- quote(stats::model.frame) ## ???
    mf <- eval(mf, parent.frame())
    wt <- as.vector(model.weights(mf))
    # do some fitting here...
}
# this runs without error:
weird_modfunc(y ~ x1 + x2,data=data,weights=w)
# this fails with the same error as above about variable lengths.
weird_modfunc(y ~ x1 + x2,data=data,weights='w')

问题是这包含多个我不知道如何解释、修改或维护的有点神秘的咒语。

调用model.frame的正确方法是什么? ?使我的函数同时接受 weights=w 的奖励积分和 weights='w'

最佳答案

欢迎来到非标准评估的乐趣。我建议您将您的功能基于 lm方法。它构造了对 model.frame 的调用。并对其进行评估。这是必要的,因为 model.frame进行非标准评估,即它接受/期望 weights 的符号范围。此外,它还确保有关公式环境的正确范围。

weird_modfunc <- function(formula,data,weights=NULL ) {
  #cl not needed, lm only adds this call to the return object
  mf <- match.call(expand.dots = FALSE)
  message("Call with ellipses not expanded: ")
  #note that there are no ellipses in the function arguments for now, 
  #but you might want to change that later
  print(mf)
  #turn weights into symbol if character is passed
  if (is.character(mf$weights)) mf$weights <- as.symbol(mf$weights)
  m <- match(c("formula", "data", "weights"), names(mf), 0L)
  message("Position of formula, data and weights in the call:")
  print(m)
  mf <- mf[c(1L, m)]
  message("New call that only contains what is needed:")
  print(mf)
  mf$drop.unused.levels <- TRUE 
  message("Call with argument added:")
  print(mf)
  mf[[1L]] <- quote(stats::model.frame) 
  message("Change call to a call to model.frame:")
  print(mf)
  mf <- eval(mf, parent.frame()) #evaluate call
  wt <- as.vector(model.weights(mf))
  # do some fitting here...
  message("Return value:")
  wt
}
# this runs without error:
weird_modfunc(y ~ x1 + x2,data=data,weights=w)
#Call with ellipses not expanded: 
#weird_modfunc(formula = y ~ x1 + x2, data = data, weights = w)
#Position of formula, data and weights in the call
#[1] 2 3 4
#New call that only contains what is needed:
#weird_modfunc(formula = y ~ x1 + x2, data = data, weights = w)
#Call with argument added:
#weird_modfunc(formula = y ~ x1 + x2, data = data, weights = w, 
#    drop.unused.levels = TRUE)
#Change call to a call to model.frame:
#stats::model.frame(formula = y ~ x1 + x2, data = data, weights = w, 
#    drop.unused.levels = TRUE)
#Return value:
# [1] 0.35299850 0.98095832 0.53888276 0.44403386 0.94936678 0.45248337 0.19062580 0.99160915 0.54845545 0.76881577 0.91342167 0.68211200 0.40725142
#[14] 0.40759230 0.14608279 0.19666771 0.19220934 0.40841440 0.34822131 0.83454285 0.19840001 0.86180531 0.39718531 0.15325377 0.33928338 0.36718044
#[27] 0.42737908 0.18633690 0.65801660 0.92041138 0.73389406 0.88231927 0.95334653 0.19490154 0.47261674 0.38605066 0.37416586 0.02785566 0.92935521
#[40] 0.41052928 0.95584022 0.27215284 0.51724649 0.97830984 0.36969649 0.31043044 0.03420963 0.66756585 0.92091638 0.04498960

#this runs without error too:
weird_modfunc(y ~ x1 + x2,data=data,weights='w')

这是一个更简单的版本,但可能存在问题(嗯,比非标准评估更多):
my_modfunc <- function(formula,data,weights=NULL) {
  weights <- substitute(weights)
  if (!is.symbol(weights)) weights <- as.symbol(weights)
  #substitute the symbol into the call:
  mf <- eval(substitute(model.frame(formula,data=data,weights=weights)))
  wt <- model.weights(mf)
  # do some fitting here...
  wt
}

my_modfunc(y ~ x1 + x2,data=data,weights=w)
#works
my_modfunc(y ~ x1 + x2,data=data,weights="w")
#works

关于r - 我应该如何在 R 中调用 model.frame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53827563/

相关文章:

Python:当我调用 "Sub"时,它不会到达

database - 图数据库建模 : multiple edges are better than single edges with properties?

c++ - 产生不同值的局部变量

javascript全局变量在下一次事件调用时返回到以前的状态

python - 如何根据组中的其他实例对实例进行分类?

c++ - 应用程序建模。什么时候使用哪个图表?

r - 将列名称传递给 Shiny 数据表中的目标选项

r - 密集与稀疏矩阵代数的速度

r - 使用自定义函数命名由 `nest` (tidyr) 创建的列中的项目

r - 你将如何在 R 中循环这个?