在R中集成一个函数作为参数的函数
Integrate a function that has a function as a parameter in R
所以我刚回答了一个问题,一回答(我认为是正确的),提问者就把它删除了。所以又来了:
我是 R 的新手,需要帮助才能使用此功能。我需要创建一个函数,可以找到任何函数的 mgf 日志和指定 t 的 return 值。我做了很多研究,我发现了很多东西告诉我使用 Vectorize() 并确保我正确定义了我的参数,但我似乎仍然无法让它工作。如果有人能帮助我,我会很高兴!
我需要编写一个函数,returns 是 mgf
自然对数的数值向量
# I'm using the expression 2*x as an example
# You can use any integrand as long as it is a function of x
logmgf <- function(integrand, upper, lower, t) {
expression <- function(x, t) {
integrand * exp(x * t)
}
integrate <- integrate(expression(x, t), upper, lower)
logmgf <- log(Vectorize(integrate[1]))
return(logmgf)
}
logmgf(2 * x, upper = Inf, lower = 0, t = 0)
2 小时前问过
熊雷
让我们尝试一些在统计上或数学上更合理的东西,例如非标准化的正态分布,即表达式:exp(-x^2)
您正在尝试创建一个新表达式(实际上是一个 R "call"),它将被解析为该表达式乘以 exp(x*t) 的乘积,因此您需要 a) 传递函数一个真正的 R 语言对象和 b) 使用不会破坏它的函数来处理它。 quote
函数将构造一个 substitute
可以在 "language level" 处操作的表达式。不幸的是,function
-函数将以一种不符合您的符号意图的方式评估 "body" 参数,因此您需要使用 body<-
(函数),它需要一个表达式在右边- 赋值运算符的手边。我将留下一些调试 print(.)
调用,这些调用是我用来了解我在早期工作中出错的地方:
logmgf <- function(integrand, upper, lower, t) {
expr <- substitute( integrand *exp(x*t), list(integrand=integrand) )
print(expr)
func <- function(x ){} # builds an empty function in x
body(func)<- expr # could have also set an environment
# but in this case using envir=parent.frame() is not appropriate
print(func)
integral <- integrate( func, upper=upper,
# notice need to name the parameters
lower=lower
# else they would be positionally matched
# (and therefore reversed in this case)
)$value
# the integrate fn returns a loist and the numeric part is in $value
logmgf <- log(integral)
}
res <- logmgf(quote(exp(-x^2)), upper = Inf, lower = -Inf, t = 0)
> res
[1] 0.5723649
MGF 从 -Inf 集成到 Inf(或者对于仅在具有定义值的 x 上具有受限域的函数)。
我想检查我是否会从已知参数中得到正确答案,所以我为正态分布添加了正确的归一化常数:
mgf <- function(integrand, upper, lower, t) {
expr <- substitute( integrand *exp(x*t), list(integrand=integrand) )
func <- function(x ){}; body(func)<- expr
integral <- integrate( func, upper=upper, lower=lower)$value
}
res <- mgf(quote((1/sqrt(2*pi))*exp(-x^2/2)), upper = Inf, lower = -Inf, t = 0)
res
#[1] 1
所以我刚回答了一个问题,一回答(我认为是正确的),提问者就把它删除了。所以又来了:
我是 R 的新手,需要帮助才能使用此功能。我需要创建一个函数,可以找到任何函数的 mgf 日志和指定 t 的 return 值。我做了很多研究,我发现了很多东西告诉我使用 Vectorize() 并确保我正确定义了我的参数,但我似乎仍然无法让它工作。如果有人能帮助我,我会很高兴!
我需要编写一个函数,returns 是 mgf
自然对数的数值向量# I'm using the expression 2*x as an example
# You can use any integrand as long as it is a function of x
logmgf <- function(integrand, upper, lower, t) {
expression <- function(x, t) {
integrand * exp(x * t)
}
integrate <- integrate(expression(x, t), upper, lower)
logmgf <- log(Vectorize(integrate[1]))
return(logmgf)
}
logmgf(2 * x, upper = Inf, lower = 0, t = 0)
2 小时前问过 熊雷
让我们尝试一些在统计上或数学上更合理的东西,例如非标准化的正态分布,即表达式:exp(-x^2)
您正在尝试创建一个新表达式(实际上是一个 R "call"),它将被解析为该表达式乘以 exp(x*t) 的乘积,因此您需要 a) 传递函数一个真正的 R 语言对象和 b) 使用不会破坏它的函数来处理它。 quote
函数将构造一个 substitute
可以在 "language level" 处操作的表达式。不幸的是,function
-函数将以一种不符合您的符号意图的方式评估 "body" 参数,因此您需要使用 body<-
(函数),它需要一个表达式在右边- 赋值运算符的手边。我将留下一些调试 print(.)
调用,这些调用是我用来了解我在早期工作中出错的地方:
logmgf <- function(integrand, upper, lower, t) {
expr <- substitute( integrand *exp(x*t), list(integrand=integrand) )
print(expr)
func <- function(x ){} # builds an empty function in x
body(func)<- expr # could have also set an environment
# but in this case using envir=parent.frame() is not appropriate
print(func)
integral <- integrate( func, upper=upper,
# notice need to name the parameters
lower=lower
# else they would be positionally matched
# (and therefore reversed in this case)
)$value
# the integrate fn returns a loist and the numeric part is in $value
logmgf <- log(integral)
}
res <- logmgf(quote(exp(-x^2)), upper = Inf, lower = -Inf, t = 0)
> res
[1] 0.5723649
MGF 从 -Inf 集成到 Inf(或者对于仅在具有定义值的 x 上具有受限域的函数)。
我想检查我是否会从已知参数中得到正确答案,所以我为正态分布添加了正确的归一化常数:
mgf <- function(integrand, upper, lower, t) {
expr <- substitute( integrand *exp(x*t), list(integrand=integrand) )
func <- function(x ){}; body(func)<- expr
integral <- integrate( func, upper=upper, lower=lower)$value
}
res <- mgf(quote((1/sqrt(2*pi))*exp(-x^2/2)), upper = Inf, lower = -Inf, t = 0)
res
#[1] 1