2

I looked up numerous posts on StackOverflow for question similar to mine one - i.e. it fails to find an object inside a self-defined function that otherwise could be found stepping outside of the function - but still not being able to find a good fix for it.

Maybe a simple example like the one below can help better illustrate the issue:

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"

Any advice would be much appreciated!

C.Yuen
  • 21
  • 2

0 Answers0