2

我在 StackOverflow 上查找了许多帖子,以寻找与我的类似的问题 - 即它无法在自定义函数内找到一个对象,否则可能会在函数之外找到该对象 - 但仍然无法找到一个好的解决方案.

也许像下面这样一个简单的例子可以帮助更好地说明这个问题:

library(robust)

data(stack.dat)
y <- stack.dat[, 1, drop = FALSE]
X <- stack.dat[, -1, drop = FALSE]

# Run robust regression with backward elimination
form     <- as.formula(paste(colnames(y), " ~ ", paste(colnames(X), collapse = ' + '), sep = ''))
r.fit    <- lmRob(form, data = data.frame(y, X), control = lmRob.control(mxr = 200))
r.fit2   <- step.lmRob(r.fit, direction = 'backward', trace = FALSE)

# Create a function of exact same workings...
robfit   <- function(m, n){
   form   <- as.formula(paste(colnames(m), " ~ ", paste(colnames(n), collapse = ' + '), sep = ''))
   r.fit  <- lmRob(form, data = data.frame(m, n), control = lmRob.control(mxr = 200))
   r.fit2 <- step.lmRob(r.fit, direction = 'backward', trace = FALSE)
}

# But calling it using the same data returns me a warning message
robfit(y, X) 
# The warning -> "Error in data.frame(m, n) : object 'm' not found"

任何建议将不胜感激!

4

0 回答 0