Hello everyone!

I am a newbie to R coding and coding in general. I only have some experience with a bit C coding.

I somehow can't find a solution for my problem.
I want to calculate the mean of a distribution function, it is F(x)= 0, for x<0,
F(x)= x^2, for 0<=x<=1, and else (x>1) F(x)=1.
The mean of it is defined by an integral over the single intervals with F'(x)*x as integrand for each interval of x.

My idea to solve it, was to implement the function using a if/else loop and store the arguments of the function in a vector.
To calculate the mean, I'd then derive the vector's single elements, store it in a new vector and multiply it with x.
Then i'd integrate this.

Problem is the output: although i defined the x- dependent parts of the function using 'function(x)' , the output of the vector will tell me, that the x- dependent part of the function body is "recognized" as language and not as variable.
So the symbolic derivation of it will give me... something that is mathematically not right in the context.
Also, how to multiply and call the vectorelements later?
Could i use sapply then?
Please don't judge me, i really tried it for days and i ran out of ideas.

my latest idea to implement it, was:

``````library(Deriv)
arg_df <-  c(function(x) {x^2} , 1, 0)
a_f<- c(function(x){ Deriv(arg_df[[1]], "x")}, Deriv(arg_df[[2]], "x") , Deriv(arg_df[[3]], "x"))
b<- c(function(x) {a_f[[1]]*x}, function(x) {a_f[[2]]*x}, function(x) {a_f[[3]]*x})
b
``````

The 'original' code looks something like this (with different chunks, where i tried different ideas..)

``````
if (!requireNamespace("numDeriv", quietly = TRUE)) {
install.packages("numDeriv")
}
library(numDeriv)
if (!requireNamespace("Deriv", quietly = TRUE)) {
install.packages("Deriv")
}
library(Deriv)

arg_f <- function(x) c(x^2 , 1, 0)
df<- Deriv(arg_f, "x")
print(df)

x<- runif(1, min=-3, max=3)
arg_f <- c(x^2 , 1, 0)
f <- function(x) {
if (x >= 0 & x <= 1) {
result<- arg_f[1]
# return(arg_f[1])
} else if (x > 1) {
result<- arg_f[2]
# return( arg_f[2] )
} else {
result<- arg_f[3]
# return(arg_f[3])
}
cat(result, "\n")
return(result)
}
f(x)

arg_f <- function(x) c(x^2 , 1, 0)
print(arg_f)
a_f <- function(x) {
df <- function(x) Deriv(arg_f, "x")
derivatives <- df(x)
result <- list(derivatives = derivatives, x = x)
print(result)
return(result)
}

arg_f <- function(x) c(x^2 , 1, 0)
print(arg_f)

a_f<-function(x) {
df<- Deriv(arg_f, "x")
return(df)
}

print(a_f(x))
d<- a_f(x)
print(d)
str(d)
list(d)
#just need to index a_f(x), don't know why r is not accepting it??

d<- function(x){
a_f[[1]]
}
print(d)
#i<- c(1,2,3), maybe indexing it with a index vector??
intgr <- function(x,i) sapply(x, function(x) x * function(x) a_f[i])
#intgr<- function(x,a_f) (x*a_f(x))
result <- function(x,a_f) intgr(x, a_f)
result(x,a_f)

mean_f <- function(x) {
a<- integrate(intgr, lower = -Inf, upper = 0 )

b<- integrate( intgr , lower = 1, upper = Inf)

c<- integrate()

}

return(a+b+c)

``````

I forgot: is there a more "noble way" to implement what I want to do?
I cant imagine that a simple operation like deriving a function with the arguments stored in a vector should be a very long and complicated task in R language.. I just can't figure it out and would be so happy, if someone can explain it to me!

I'm not sure this will help you because your approach to calculating values of F() is so different than what I would do that I suspect I'm misunderstanding your goal. Nevertheless, here are two methods for taking a vector of values and calculating F(x), returning a vector. In the first method, everything is hard coded. In the second method, a list of arguments is passed to the function so you can adjust the values returned for the three ranges x<0, 0<=x<=1, x>1. You have to use a list because a vector can only have one kind of element but the arguments are a function and two numeric values.

``````#Hard coded arguments
DistFunc <- function(x) ifelse(x >= 0 & x <= 1, x^2, ifelse(x > 1, 1, 0))
Values <- c(-0.5, 0.7, 3)
DistFunc(Values)
#> [1] 0.00 0.49 1.00

#arguments stored in a list
DistFunc2 <- function(x, ARGS) ifelse(x >= 0 & x <= 1, ARGS[[1]](x),
ifelse(x > 1, ARGS[[2]], ARGS[[3]]))
ARGSvalues <- list(function(z) z^2, 1, 0)
DistFunc2(Values, ARGSvalues)
#> [1] 0.00 0.49 1.00
``````

Created on 2023-12-20 with reprex v2.0.2
Does that help?

Hi,
Thanks so much for the answer!
I'm interested in your approach! How would you do it?

How can I- without passing a numerical value to the function, "keep" the x in the argument of the function?
So that the output of F(x) here is a vector c(x^2, 1, 0)?. The reason why i want this kind of output is, that i want to do some symbolic maths with the function.
So the x^2 in that vector should be something that i can perform symbolic derivations, etc with it.
But its not working and i wonder how it'll work?

...so basically in the chunk that i gave, the part where i defined F(x) with the if-else loop can be ignored.

I looked at the Deriv package and I saw that the Deriv function can take in a function and return the function that is its derivative. That led me to this approach. Notice that ARGSvalues is now a list of functions. For each input value, I pick the piece-wise function that would apply, then I calculate the derivative of each of those (again returning a function), evaluate the derivatives at each input value, then multiply the values by the evaluated derivatives.
I hope I am not leading you astray!

``````DistFunc <- function(x, ARGS) ifelse(x >= 0 & x <= 1, ARGS[1],
ifelse(x > 1, ARGS[2], ARGS[3]))
ARGSvalues <- c(function (x) x^2, function(x) 1, function(x) 0)

Values <- c(-0.5, 0.7, 3, 0.33)

LocalFuncs <- sapply(Values, DistFunc, ARGS = ARGSvalues)
LocalFuncs
#> [[1]]
#> function(x) 0
#>
#> [[2]]
#> function (x) x^2
#>
#> [[3]]
#> function(x) 1
#>
#> [[4]]
#> function (x) x^2

library(Deriv)
#> Warning: package 'Deriv' was built under R version 4.3.2

LocalDerivs <- sapply(LocalFuncs, Deriv)
LocalDerivs #a list of derivative functions
#> [[1]]
#> function (x)
#> 0
#>
#> [[2]]
#> function (x)
#> 2 * x
#>
#> [[3]]
#> function (x)
#> 0
#>
#> [[4]]
#> function (x)
#> 2 * x

FinalFunc <- function(Val, Func) Func(Val)

#Evaluate the derivative at each value
EvalDerivs <- mapply(FinalFunc, Values, LocalDerivs)
EvalDerivs
#> [1] 0.00 1.40 0.00 0.66

x_times_Fprime <- Values * EvalDerivs   #x * F'(x)
x_times_Fprime
#> [1] 0.0000 0.9800 0.0000 0.2178
#check result
c(-0.5 * 0, 0.7 * 2*0.7, 3 * 0, 0.33 * 2*0.33)
#> [1] 0.0000 0.9800 0.0000 0.2178
``````

Created on 2023-12-20 with reprex v2.0.2

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.