Create a custom function in R - r

I need help with creating a custom function in R.
I don't know how to create a function Y, where the 3 inputs of the function must be: n as in integer, mu (dimension m) as a vector, and sigma as a matrix (dimension mxm).
Does anyone know how to do that or have any idea of how can I reach it?
Thank you all :)
Note: I know how to create functions where the inputs are integers, but I have no idea how to proceed in this case

MyFun <- function(x,y,z)
{
# write a custom function
x*y*z
}
output1 <- MyFun(1,2,3)
output1

Related

extracting functionality info of a user defined function

mysum <- function(x,y){
#this is my sum function and it is used for..
#john's updated version, x in int, y in double.
k <- sum(x,y)
return(k)
}
say I have a mysum function and wanted to see the functionality and some basic info, how do one extract the function's info?
Instead of looking into the script directly, would like to have something similar like ?mysum or mysum.info that can print out that two line of comment/info from my console. Thanks.

R: optim() when function has inputs in different formats

I have data
dat1 <- data.frame(a=1:3, b=rnorm(3))
dat2 <- data.frame(a=c(rep(1,3),rep(2,5),rep(3,4)), c=runif(12,1,50))
and a function that takes both data frames as inputs
foo <- function(dat1,dat2,par){
if(par< 25){return(dat1$b*par)}
if(par>=25){return(sum(dat2$c>par))}
}
which might work if it was embedded in a loop over different values of a.
However, I would like to find the value of par that minimizes the output of foo across all values of a. The optim() funtion should be able to do just this, but my problem is that I need to pass it two dataframes of different dimensions. I suspect some form of list could help but wouldn't know how.
from the help documentation on optim,
fn - A function to be minimized (or maximized), with first argument the vector of parameters over which minimization is to take place. It should return a scalar result.
Your function is not returning a scalar when par < 25. Since you are not changing the data.frames during optimization process, you do not have to pass in them again. Below is an example usage of optim in your case:
foo <- function(par) {
if(par < 25) {
return(sum(dat1$b*par))
} else {
return(sum(dat2$c>par))
}
}
optim(0, foo, method="Brent", lower=-1e6, upper=1e6)

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.

Print.myclass function in R

I am trying to develop my first package in R and I am facing some issues with "myclass" generic functions that i will try to describe.
Assume a data.frame X with n <- nrow(X) rows and K <- ncol(X) columns.
My main package function (too big to put it in this post) lets say
fun1 <- function(X){
# do staff...
out <- list(index= character vector, A= A, B= B,... etc)
return(out)
class(out) <- "myclass"
}
returns as an output a list. Then I have to use the output for the generic print method in a print.myclass function. However, in my print function I want to use the data frame X used in my main function without asking the user to provide it in an argument (i.e, print(out,X)) and without having it in my output list out (visible to the user at least). Is there any way to do that? Thanks in advance!

Use optimize() for a function which returns several values

Easier to ask by example. If I have a function
fn <- function(x) {
...
return(c(a,b,c))
}
and I wish to maximize (or minimize) with respect to a, but also get the values of b and c at the optimal value.
Of course I can use fn2 <- function(x) fn(x)[1] to determine the optimal value, then call the function again, but I wonder if there is a smarter way of doing this.
optim needs the return value to be a scalar. The documentation says so
fn: A function to be minimized (or maximized), with first
argument the vector of parameters over which minimization is
to take place. It should return a scalar result.
You could write the values of interest to a global variable inside your function though. This isn't necessarily best practice but it could work.
f <- function(x){
.vals <<- c(x, x+1)
x^2
}
optim(1, f)
then after we can look at what is stored in .vals
> .vals
[1] 9.765625e-05 1.000098e+00

Resources