开发者

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

I am trying to optimise my likelihood function of R_j and R_m using optim to estimate al_j, au_j, b_j and sigma_j. This is what I did.

a = read.table("D:/ff.txt",header=T)
attach(a)   
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 and sigma_j need to be 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 is difficult to tell where to start on this.

As @mac said, your code is difficult to read. It also contains errors.

For example, if you try sum[c(1,2)] you will get an error: you should use sum(c(1,2)). In any case, you seem to be taking the sum in the wrong place. You cannot use if and else if on vectors, and need to use ifelse. You have nothing to stop the standard deviation going negative. There is more.

The following code runs without errors or warnings. You will still have to decide whether it does what you 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") 


Let's start with the error message:

Error in pnorm(au_j, mean = b_j * R_m, sd = sigma_j) : 
  object 'au_j' not found

So R is telling you that when it got to the pnorm call, it couldn't find anything called 'au_j' to use in that call. Your next step should be to look at your function, llik, and try to identify how you expect the variable 'au_j' to be defined within that function.

At this point, the answer should be fairly clear (maybe!). Nowhere in llik is the variable 'au_j' assigned a value. So it won't be 'created' inside the function. R's scoping rules will then cause it to look outside the function in the global environment for something called 'au_j'.

And you might say that here is where things should work, since you assigned 'au_j' a value within start.par. But that's a list, and R can't find the named object 'au_j' inside a list like that.

So the solution here is most likely to rework your function llik so that it takes as arguments everything that it will use, so you're going to add everything in start.par to the arguments of llik. Something like:

llik <- function(par=c(al_j,au_j,sigma_j,b_j),R_j,R_m){...}

and then within llik you'll refer to al_j using par[1] and so forth. Then the optim call should look something like:

optim(start.par,llik,R_j=a$R_j,R_m=a$R_m)

Since you've attached your data, in a, you probably don't have explicitly pass the arguments R_j and R_m in the optim call, but it's probably good practice to do so.

I think I've reconstructed what you're trying to accomplish here (modulo the math, which I haven't even glanced at), but I confess that your code is a bit hard to parse. I would suggest spending some time with the examples in ?optim to make sure you understand how that function is called.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜