I am using the package robust.arima in R, which works fine when I call it in a script. However, I want to organize my files and therefore call robust arima in a function. Here all of a sudden the variable is not found. Let me give an example
# Works fine
ts_list <- rnorm(100)
arima.rob(ts_list~1)
# Breaks down
get_rob_estimate <- function(x){
out <- arima.rob(x~1)
return(out)
ts_list <- rnorm(100)
get_rob_estimate(ts_list)
Error in eval(formula[[2]]) : object 'x' not found
Does anyone know what's going on? I think the problem looks similar to R : Pass argument to glm inside an R function , but I still can't seem to figure it out and I am curious how R processes these functions?
Edit
Okay for the basic option I understand it now, but I don't get why it works. What if I have
check_func <- function(ind_ts){
out <- substitute(arima.rob(ind_ts~1))
return(eval(out))
}
analyze_ts <- function(){
df <- mvrnorm(100, mu=c(0,0,0), Sigma=diag(c(1,1,1)))
p <- list()
for (i in ncol(df)){
sel <- df[,i]
check_func(sel)
p <- append(p, sel)
}
return(p)
}
analyze_ts()
I then get the error
Error in eval(formula[[2]]) : object 'sel' not found
How does it work? What is going on here? I just want my list to go as a list in my function, shouldn't be so hard right? Does not matter how many functions it goes through?