Hi all,
I wrote the following code for the problem I posted before. Can some one
recommend a better way to do this?
f1 <- function(lambda,z,p1){
lambda*(p1*exp(-3*z-9/2)+(0.2-p1)*exp(4*z-8))-(1-lambda)*0.8}
f2 <- function(p1,cl, cu){
0.8*(pnorm(cl)+(1-pnorm(cu)))/(0.8*(pnorm(cl)+(1-pnorm(cu)))+p1*(pnorm(cl+3)+(1-pnorm(cu+3)))+(0.2-p1)*(pnorm(cl-4)+(1-pnorm(cu-4))))}
lam <- seq(0.01,0.9, by=0.001)
x1 <- numeric(length(lam))
x2 <- numeric(length(lam))
for (i in 1:length(lam)){
cl <- uniroot(f1, lower =-10, upper = 0,
tol = 1e-10,p1=0.15,lambda=lam[i])$root
cu <- uniroot(f1, lower =0, upper = 10,
tol = 1e-10,p1=0.15,lambda=lam[i])$root
x1[i]<- f2(p1=0.15, cl=cl, cu=cu)
}
x1<=0.05
x1[259]
## x1[259] is the lambda value for f2()=0.05.
Thank you very much!!
Hannah
2011/2/21 li li <hannah.hlx@gmail.com>
> Hi all,
> I have the following two function f1 and f2.
>
> f1 <- function(lambda,z,p1){
> lambda*(p1*exp(-3*z-9/2)+(0.2-p1)*exp(4*z-8))-(1-lambda)*0.8}
>
> f2 <- function(p1,cl, cu){
>
>
0.8*(pnorm(cl)+(1-pnorm(cu)))/(0.8*(pnorm(cl)+(1-pnorm(cu)))+p1*(pnorm(cl+3)+(1-pnorm(cu+3)))+(0.2-p1)*(pnorm(cl-4)+(1-pnorm(cu-4))))}-0.05
>
> First fix p1 to be 0.15.
> (i) choose a lambda value, say lamda=0.6,
> (ii) there should be two solutions for the equation f1(z, p1=0.15,
> lambda=1)=0, say cl and cu,
> (iii) evaluate f2 at cl and cu found in (ii) and p1=0.15,
>
> How do we find the lambda value such that f2=0?
>
> Thank you very much!
> Hannah
>
>
>
>
>
>
[[alternative HTML version deleted]]