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

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)

Related

r function in function arguments + apply

I'm having troubles using several functions within the same one and calling the arguments generated. I'm using a more complicated function that can be simplified as followed:
func.essai <- function(x) {
g <- sample(seq(1,30), x)
i <- sample(x,1)
func.essai.2 <- function(y,i) {
z <- y+i
}
h <- sapply(g,func.essai.2(y,i))
}
sq <- seq(1,4)
lapply(sq, func.essai)
I'm using arguments that are generated at the beginning of func.essai (and that depend on x) as a fixed input for func.essai.2, here for i, and as a vector to go through on the sapply function, here for g. This code doesn't work as such -- it doesn't recognize y and/or i. How can I rewrite the code to do so?
I think the error you get is because of your use of sapply. This should work instead of your line containing sapply:
h <- sapply(g,func.essai.2, i)
See ?sapply, which tells you that you should provide additional arguments behind the function that you are applying.

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.

apply() in R with user defined function

I try to use the apply function using my own function. However, I keep getting this error: Error in match.fun(FUN) :
'calculate_3month_average_volume(volume_matrix, 90)' is not a function, character or symbol
Code below:
#Calculate 3 monthtly average volume (does not work, coding issue)
calculate_3month_average_volume <- function(stock, number_of_days){
return(SMA(stock, number_of_days))
}
avg_volume_matrix <- apply(X = volume_matrix, MARGIN = 2, FUN = calculate_3month_average_volume(volume_matrix,90))
avg_volume_matrix <- apply(volume_matrix,2,function(x){
SMA(x,90) #can also be return(SMA(x,90))
})
Should also do the trick as you don't have to call the function into your environment to begin with.
The issue you are having specifically is that within your apply loop when you state "FUN= calculate_3month_average_volume(volume_matrix,90)", you should match your arguments in the called function with respect to x, as apply(x=,MARGIN=,FUN=,...). If we were using a function that was called into the environment as you have, we would use:
avg_volume_matrix <- apply(volume_matrix,2,calculate_3month_average_volume(x,90))

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

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?

When passing a function as an argument to another function, how do I loop over that function?

I currently set up a function which takes another function as an argument. I am trying to create this function to loop over the function it took as an argument.
However, with the way I set it up, it seems like it only really runs the function once, as it should be dependent on a random seed but it keeps coming back with the exact same value.
The only way I found to solve this was to not have the function as an argument but to hard code it in to the function, but this is not really what I want to do.
Here is my code:
runModel <- function(model,runs){
b = c()
for (i in 1:runs){
a <- model$lambda.min
b <- append(b,a)
}
return (c(mean(b),sd(b)))
}
cvModel = cv.glmnet(predictors,outcome,family=c("binomial"),alpha=.9,nfolds=20)
runModel(cvModel,20)
I don't know what are your objects 'predictors' or 'outcome' but it looks like your passing the result of your function cv.glmnet rather than the expression of your function to your runModel function.
One way to run the function at each loop could be to rewrite your function to pass function name and arguments:
runModel <-function(fun,runs,predictors,outcome,family=c("binomial"),alpha=.9,nfolds=20){
b = c()
model <- fun(predictors,outcome,family,alpha,nfolds)
for (i in 1:runs){
a <- model$lambda.min
b <- append(b,a)
}
return (c(mean(b),sd(b)))
}
runModel(cv.glmnet,20, predictors,outcome,family=c("binomial"),alpha=.9,nfolds=20)

Resources