[英]uniroot in R when there are two unknowns
考慮兩個參數x
和a
的函數f
。 第一予取的集成f
相對於x
,這成為一個功能g
的a
。 其次,我想找到a
的結果函數g
的根。 我可以使用uniroot
並integrate
到R
嗎? 如果是這樣,怎么辦? 如果沒有,有沒有辦法做到這一點? 謝謝。
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
),該truncfn
在對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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.