当有两个未知数时 R 中的 uniroot
uniroot in R when there are two unknowns
考虑两个参数 x
和 a
的函数 f
。首先,我将 f
与 x
进行积分,它成为 a
的函数 g
。其次,我想找到 a
的结果函数 g
的根。我可以在 R
中使用 uniroot
和 integrate
来做到这一点吗?如果是这样,如何?如果没有,有没有办法做到这一点?谢谢。
b <- 2
truncfn <- function(x) pmin(b, pmax(x, -b))
# thetashape and thetascale are constants
# x and a are arguments
f <- function(x, thetashape, thetascale, a){
term1 <- -1/thetascale
term2 <- (1-thetashape)/thetascale
term3 <- x/(thetascale-thetashape*x)
term1 + term2*term3 - a
}
# First, integrate f with respect to x
g <- integrate(truncfn(f), lower=0, upper=Inf)
# Second, find root of g
uniroot(g, ...)
你可以定义一个函数(我叫它truncfn2
),在f
的调用结果上调用truncfn
,然后g
整合truncfn2
。最后 uniroot
搜索 g
的根:
b <- 2
truncfn <- function(x) pmin(b, pmax(x, -b))
# thetashape and thetascale are constants
# x and a are arguments
f <- function(x, thetashape, thetascale, a){
term1 <- -1/thetascale
term2 <- (1-thetashape)/thetascale
term3 <- x/(thetascale-thetashape*x)
term1 + term2*term3 - a
}
truncfn2 <- function(x, thetashape, thetascale, a) truncfn(f(x, thetashape, thetascale, a))
g <- function(a) integrate(truncfn2, thetascale=1, thetashape=0.6, a=a, lower=0, upper=10)$value
uniroot(g, lower=-10, upper=10)
# $root
# [1] -1.867932
#
# $f.root
# [1] 1.134733e-07
#
# $iter
# [1] 7
#
# $init.it
# [1] NA
#
# $estim.prec
# [1] 6.103516e-05
考虑两个参数 x
和 a
的函数 f
。首先,我将 f
与 x
进行积分,它成为 a
的函数 g
。其次,我想找到 a
的结果函数 g
的根。我可以在 R
中使用 uniroot
和 integrate
来做到这一点吗?如果是这样,如何?如果没有,有没有办法做到这一点?谢谢。
b <- 2
truncfn <- function(x) pmin(b, pmax(x, -b))
# thetashape and thetascale are constants
# x and a are arguments
f <- function(x, thetashape, thetascale, a){
term1 <- -1/thetascale
term2 <- (1-thetashape)/thetascale
term3 <- x/(thetascale-thetashape*x)
term1 + term2*term3 - a
}
# First, integrate f with respect to x
g <- integrate(truncfn(f), lower=0, upper=Inf)
# Second, find root of g
uniroot(g, ...)
你可以定义一个函数(我叫它truncfn2
),在f
的调用结果上调用truncfn
,然后g
整合truncfn2
。最后 uniroot
搜索 g
的根:
b <- 2
truncfn <- function(x) pmin(b, pmax(x, -b))
# thetashape and thetascale are constants
# x and a are arguments
f <- function(x, thetashape, thetascale, a){
term1 <- -1/thetascale
term2 <- (1-thetashape)/thetascale
term3 <- x/(thetascale-thetashape*x)
term1 + term2*term3 - a
}
truncfn2 <- function(x, thetashape, thetascale, a) truncfn(f(x, thetashape, thetascale, a))
g <- function(a) integrate(truncfn2, thetascale=1, thetashape=0.6, a=a, lower=0, upper=10)$value
uniroot(g, lower=-10, upper=10)
# $root
# [1] -1.867932
#
# $f.root
# [1] 1.134733e-07
#
# $iter
# [1] 7
#
# $init.it
# [1] NA
#
# $estim.prec
# [1] 6.103516e-05