Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
513 views
in Technique[技术] by (71.8m points)

numerical integration - Integrating over a root in R

I built a model that unfortunately does not provide analytical solutions for some of the variables so I need to solve it numerically for each value. How can I integrate over a function that needs to find the root for each point in the interval? That is, while integrating over x, I need to use each x to find a root value as can be seen in the expression below.

crrafun <- function(w){ alpha/(1-alpha)*(theta-w)-w/(1-gam)+rhovu*w^gam}
integrand4 <- function(x){uniroot(crrafun,c(rhovu,xhat2_new,theta=x)$root+xi-rhovu2_new)*dlnorm(x,mu,sigma)}
integ4 <- integrate(integrand4,xhat2_new,20)$value

This gives me the error "f() values at end points not of opposite sign" but when I run uniroot separately on a sequence of values within the interval of x's (including the highest and lowest value) there is never any problem so the error message is probably misleading.

I tried writing the crrafun directly into the integrand but this did not help. I also tried substituting uniroot.all for uniroot. Is there any alternative to integrate or uniroot that might help me in this case? I know I could probably write my own integrate function, summing the root values over some sequence. I would prefer not to however, since it seems very ad-hoc and would only give a rough idea of the size of the integral.

question from:https://stackoverflow.com/questions/65643765/integrating-over-a-root-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...