As the error message tells you, the problem is in integrating InverseF1(). I would recommend that the you breakup the integrand into two terms, the first term with `qnorm(...)' and the second part involving the `a*(abs(u-tau))^b*(u>tau)' term. It is the second term that causes trouble . But you can integrate this term analytically. So, do that and plug it into the expression for EX and varX.
This should work, I think. Ravi. ---------------------------------------------------------------------------- ------- Ravi Varadhan, Ph.D. Assistant Professor, The Center on Aging and Health Division of Geriatric Medicine and Gerontology Johns Hopkins University Ph: (410) 502-2619 Fax: (410) 614-9625 Email: rvarad...@jhmi.edu Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty_personal_pages/Varadhan.h tml ---------------------------------------------------------------------------- -------- -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of yh...@illinois.edu Sent: Sunday, July 19, 2009 9:39 PM To: Ravi Varadhan; Berend Hasselman; r-help@r-project.org Subject: Re: [R] Solving two nonlinear equations with two knowns I try to use integrate command to get theoretical mean and variance, and then solve for a and b. I have an error as follows, Iteration: 0 ||F(x0)||: 2.7096 Error in integrate(InverseF1, tau, 1, mu = mu2, sigma = sigma2, a = a, : non-finite function value Any suggestion? #R code: mu2=0.4 sigma2=4 tau=0.75 mean.diff=2 var.ratio=3 InverseF1<-function(u,mu,sigma,a,b,tau) { qnorm(u,mean=mu,sd=sigma)+a*(abs(u-tau))^b*(u>tau) } InverseF2<-function(u,mu,sigma) { qnorm(u,mean=mu,sd=sigma) } part1<-function(u,mu,sigma,a,b,tau) { (InverseF2(u,mu,sigma)+a*(abs(u-tau))^b*(u>tau))^2 } part2<-function(u,mu,sigma) { InverseF2(u,mu,sigma)^2 } parameter<- function(cons) { a<-cons[1] b<-cons[2] f <- rep(NA, 2) EX<-integrate(InverseF2,0,tau,mu=mu2,sigma=sigma2)$value+integrate(InverseF1 ,tau,1,mu=mu2,sigma=sigma2,a=a,b=b,tau=tau)$value EY<-integrate(InverseF2,0,1,mu=mu2,sigma=sigma2)$value VarX<-integrate(part2,0,tau,mu=mu2,sigma=sigma2)$value+integrate(part1,tau,1 ,mu=mu2,sigma=sigma2,a=a,b=b,tau=tau)$value-EX^2 VarY<-integrate(part2,0,1,mu=mu2,sigma=sigma2)$value-EY^2 f[1] <- abs(EX - EY) - mean.diff f[2] <- sqrt(VarX) - sqrt(var.ratio) * sqrt(VarY) f } library(BB) c0<-c(3,1) ans1 <- dfsane(par=c0, fn=parameter, method=3) ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.