r - Why the parameter I am trying to estimate is "not found"? -


i trying optimise likelihood function of r_j , r_m using optim estimate al_j, au_j, b_j , sigma_j. did.

a = read.table("d:/ff.txt",header=t) attach(a)       r_j         r_m 1  2e-03 0.026567295 2  3e-03 0.009798475 3  5e-02 0.008497274 4 -1e-02 0.012464578 5 -9e-04 0.002896023 6  9e-02 0.000879473 7  1e-02 0.003194435 8  6e-04 0.010281122 

the parameters al_j, au_j, b_j , sigma_j need estimated.

llik=function(r_j,r_m)  if(r_j< 0)  {  sum[log(1/(2*pi*(sigma_j^2)))-(1/(2*(sigma_j^2))*(r_j+al_j-b_j*r_m))^2]  }else if(r_j>0)  {  sum[log(1/(2*pi*(sigma_j^2)))-(1/(2*(sigma_j^2))*(r_j+au_j-b_j*r_m))^2]  }else if(r_j==0)  {  sum(log(pnorm(au_j,mean=b_j*r_m,sd=sigma_j)-pnorm(al_j,mean=b_j*r_m,sd=sigma_j)))  }  start.par=c(al_j=0,au_j=0,sigma_j=0.01,b_j=1)  out1=optim(llik,par=start.par,method="nelder-mead")  error in pnorm(au_j, mean = b_j * r_m, sd = sigma_j) :    object 'au_j' not found 

it difficult tell start on this.

as @mac said, code difficult read. contains errors.

for example, if try sum[c(1,2)] error: should use sum(c(1,2)). in case, seem taking sum in wrong place. cannot use if , else if on vectors, , need use ifelse. have nothing stop standard deviation going negative. there more.

the following code runs without errors or warnings. still have decide whether want.

a <- data.frame( r_j = c(0.002,0.003,0.05,-0.01,-0.0009,0.09,0.01,0.0006),                  r_m = c(0.026567295,0.009798475,0.008497274,0.012464578,                          0.002896023,0.000879473,0.003194435,0.010281122) )  llik = function(x)     {      al_j=x[1]; au_j=x[2]; sigma_j=x[3];  b_j=x[4]     sum(         ifelse(a$r_j< 0, log(1/(2*pi*(sigma_j^2)))-                            (1/(2*(sigma_j^2))*(a$r_j+al_j-b_j*a$r_m))^2,           ifelse(a$r_j>0 , log(1/(2*pi*(sigma_j^2)))-                            (1/(2*(sigma_j^2))*(a$r_j+au_j-b_j*a$r_m))^2,                           log(pnorm(au_j,mean=b_j*a$r_m,sd=sqrt(sigma_j^2))-                            pnorm(au_j,mean=b_j*a$r_m,sd=sqrt(sigma_j^2)))))         )    }   start.par = c(0, 0, 0.01, 1)  out1 = optim(llik, par=start.par, method="nelder-mead")  

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -