簡體   English   中英

有兩個未知數時,R中的uniroot

[英]uniroot in R when there are two unknowns

考慮兩個參數xa的函數f 第一予取的集成f相對於x ,這成為一個功能ga 其次,我想找到a的結果函數g的根。 我可以使用unirootintegrateR嗎? 如果是這樣,怎么辦? 如果沒有,有沒有辦法做到這一點? 謝謝。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM