R: Using forecast() within a user-defined function - r

I'm writing a function which forecasts some user-inputted data, by fitting an AR model. Outside the function, the code may look like
dat <- c(1,1.1,1,1.2)
print(forecast(ar(dat)))
This runs just fine.
If this is now put inside a function, like:
func <- function(data_input)
{
temp <- forecast(ar(data_input))
print(temp)
}
func(dat)
I get this error:
Error in ts(x) : 'ts' object must have one or more observations
Please could someone explain what's going on here?

It works like this:
# library
library(forecast)
# data
dat <- c(1,1.1,1,1.2)
# function definition
func <- function(x){
(temp <- forecast(ar(x)))
}
# usage
func(dat)
However, I do not know why it does not work in your case.

I used the workaround suggested in Why can't I pass a dataset to a function?
This seems to work, so could the issue be
the definition of environments in the parse tree of S4 methods?

Related

Error in genereting_fuction(100) : could not find function "genereting_fuction

I created this function to generate the data with the characteristics I need:
genereting_fuction<-function(n){
X1=rnorm(n)+mean_shifts[1]
X4=rnorm(n)+mean_shifts[4]
X2=X1*p12+std_e2*rnorm(n)+mean_shifts[2]
X3=X1*p13+X4*p43+std_e3*rnorm(n)+mean_shifts[3]
X5=X2*p25+X3*p35+std_e5*rnorm(n)+mean_shifts[5]
sample=cbind(X1,X2,X3,X4,X5)
return(sample)
}
if I call it for a single item it works but when I call it in the applay function as follows:
dati<-lapply(1:100, genereting_fuction(100))
I get this error:
Error in genereting_fuction(100) :
could not find function "genereting_fuction"
Note that I prefer the replicate solution by #Jakub.Novotny for your purpose, but to understand what went wrong using lapply, this is why and how to solve it.
Using apply and a function, it assumes x the value of your apply to be provided always in the function.
To make it work you can do two things.
lapply(1:100, function(x) genereting_fuction(100))
include x in your function like genereting_fuction <- function(x, n) { # code here } and then you can use lapply(1:100, genereting_fuction, n = 100)

Using variable from another script (inter-file function closure)

I have script main.R, where I create inv_cov_mat variable. I later load metrics.R and use it to calculate function value (I use it as kind of inter-script function closure). I get error "object 'inv_cov_mat' not found". My code:
main.R:
knn <- function(...)
{
# some code
source("./source/metrics.R")
if (metric == "mahalanobis")
inv_cov_mat <- solve(cov(training_set))
# other code
# calculate distance in given metric between current vector and every row vector from training set matrix
distances <- apply(training_set, 1, metric, vec2=curr_vec) # error
metrics.R:
mahalanobis <- function(vec1, vec2)
{
diff <- vec1 - vec2
sqrt(t(diff) %*% inv_cov_mat %*% diff)
}
I've found simple, even if not elegant answer: use inv_cov_mat as global variable, not inside knn function. Then other scripts can see it.
It's not entirely clear what you want, but if I understand you correctly---you have a character string identifying the metric you want to use, and a function with the same name. So you should be able to use get to retrieve the function based on the name.
metric == "mahalanobis"
metric.fun = get(metric)
distances <- apply(training_set, 1, metric.fun, vec2=curr_vec)
That said, there are probably better ways to organize your code that would avoid this problem entirely, e.g. create a named list of functions for accessing metrics.
EDIT regarding the issue of inv_cov_mat, either pass it as an argument to your metric function or use get inside that function to access variables from the parent environment using the envir argument. Passing the variable as an argument to your metric function is definitely the better and cleaner approach.

Preserving data structure when returning values from function in R

I currently have a basic script written in R, which has two functions embedded within another:
FunctionA <- Function() {
results_from_B <- FunctionB()
results_from_C <- FunctionC()
}
Function B generates some data which is then analysed in Function C.
If I stop the code within function A, I can see the structure of results_from_C - this appears under 'values' and I can refer to different elements using the syntax results_from_C$column_name1.
I achieved this within Function C by specifying the returned values using:
return(list(column_name_1 = value1, column_name_2 = value2)
However, I cannot work out how I can return these same values (in the same structure) from Function A - everything I try returns a list which is formatted as 'Data' rather than 'Values' and cannot be indexed using the syntax results_from_A$column_name1.
Can anyone help me to understand what I need to do in order to extract results from Function C outside of Function A?
Thanks in advance
I don't understand what you mean by formatted as 'Data' rather than 'Values'.
There's nothing wrong with the setup you describe, I every now and then use functions inside functions, it's perfectly OK.
(Note that R is case sensitive, it's function not Function.)
FunctionA <- function() {
FunctionB <- function() 1:2*pi
FunctionC <- function(x)
list(column_name_1 = x[1], column_name_2 = x[2])
results_from_B <- FunctionB()
results_from_C <- FunctionC(results_from_B)
results_from_C
}
result <- FunctionA()
result
$column_name_1
[1] 3.141593
$column_name_2
[1] 6.283185
result$column_name_1
[1] 3.141593
Is this it? If not, please clarify your question.

R function with function argument

I need to write a function with a function argument, which will slightly modify the function and return the modified function.
what I have so far is
discriminant.functions <- function(priordist1,PC1)
{
g1 <- PC1*match.fun(priordist1)
return(g1)
}
but it doesn't work - i get the following error message when I call the function:
discriminant.functions(function(x1,x2) 36*x1*x2*(1-x1)*(1-x2),0.5)
Error in PC1 * match.fun(priordist1) :
non-numeric argument to binary operator
I am not very experienced with R and so I don't know if there are obvious ways to do this, it really seems like it should be very simple. Any help appreciated, thank you very much!
match.fun is used to check if the argument is a function, you need to call the function here. Either directly func(...) or using do.call like this:
## use ... for extra func arguments
discriminant.functions <-
function(func,PC1,...){
match.fun(func) ## check if func s a function
function(...) PC1* do.call(func,list(...))
}
I test it for * function:
mult2 <- discriminant.functions ("*",2)
mult2(5,4)
[1] 40

In R, getting the following error: "attempt to replicate an object of type 'closure'"

I am trying to write an R function that takes a data set and outputs the plot() function with the data set read in its environment. This means you don't have to use attach() anymore, which is good practice. Here's my example:
mydata <- data.frame(a = rnorm(100), b = rnorm(100,0,.2))
plot(mydata$a, mydata$b) # works just fine
scatter_plot <- function(ds) { # function I'm trying to create
ifelse(exists(deparse(quote(ds))),
function(x,y) plot(ds$x, ds$y),
sprintf("The dataset %s does not exist.", ds))
}
scatter_plot(mydata)(a, b) # not working
Here's the error I'm getting:
Error in rep(yes, length.out = length(ans)) :
attempt to replicate an object of type 'closure'
I tried several other versions, but they all give me the same error. What am I doing wrong?
EDIT: I realize the code is not too practical. My goal is to understand functional programming better. I wrote a similar macro in SAS, and I was just trying to write its counterpart in R, but I'm failing. I just picked this as an example. I think it's a pretty simple example and yet it's not working.
There are a few small issues. ifelse is a vectorized function, but you just need a simple if. In fact, you don't really need an else -- you could just throw an error immediately if the data set does not exist. Note that your error message is not using the name of the object, so it will create its own error.
You are passing a and b instead of "a" and "b". Instead of the ds$x syntax, you should use the ds[[x]] syntax when you are programming (fortunes::fortune(312)). If that's the way you want to call the function, then you'll have to deparse those arguments as well. Finally, I think you want deparse(substitute()) instead of deparse(quote())
scatter_plot <- function(ds) {
ds.name <- deparse(substitute(ds))
if (!exists(ds.name))
stop(sprintf("The dataset %s does not exist.", ds.name))
function(x, y) {
x <- deparse(substitute(x))
y <- deparse(substitute(y))
plot(ds[[x]], ds[[y]])
}
}
scatter_plot(mydata)(a, b)

Resources