我在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"
任何建议都将不胜感激!