SCM

Forum: help

Monitor Forum | Start New Thread Start New Thread
RE: maxLik$estimate returns NULL [ Reply ]
By: uh lo on 2013-08-02 20:53
[forum:39771]
Hi Arne,
thank you so much for your efforts. Now it works! And of course I'll quote maxLik :-)
All the best,
Jacqueline

RE: maxLik$estimate returns NULL [ Reply ]
By: Arne Henningsen on 2013-08-01 22:42
[forum:39768]
Hi Jaqueline!

If you take a look at the object returned by maxLik(), you can see that the problem is that the log-likelihood value at the starting values is out of range:

R> a
Maximum Likelihood estimation
Newton-Raphson maximisation, 0 iterations
Return code 100: Initial value out of range.

because

R> loglik(theta)
[1] NaN

because

> w2(theta)

returns some values that are minus infinity, because

> m0(theta)

returns some values that are exactly one (due to a limited precision and unavoidable rounding errors on digital computers) and log(1-1) = log(0) is not defined (minus infinity).

You can improve the precision of your calculations by simplifying your equations. Your code does the following calculations:

w2 = log(1-m0(theta))

m0 = 1-exp(-exp(theta[1] + theta[2]*Z + theta[3]*Z^2 ))

When merging them, you get:

w2 = log(1-(1-exp(-exp(theta[1] + theta[2]*Z + theta[3]*Z^2 ))))
= log(1 - 1 + exp(-exp(theta[1] + theta[2]*Z + theta[3]*Z^2 )))
= log(exp(-exp(theta[1] + theta[2]*Z + theta[3]*Z^2 )))
= -exp(theta[1] + theta[2]*Z + theta[3]*Z^2 )

Hence, you can replace the lines:

w2 <- function(theta) (log(1-m0(theta)))
m0 <- function(theta) (1-exp(-exp(theta[1] + theta[2]*Z + theta[3]*Z^2 )))

by the line:

w2 <- function(theta) (-exp(theta[1] + theta[2]*Z + theta[3]*Z^2 ))

Then, I get:
R> a
Maximum Likelihood estimation
Newton-Raphson maximisation, 5 iterations
Return code 1: gradient close to zero
Log-Likelihood: -0.3955582 (3 free parameter(s))
Estimate(s): -0.2286405 1.056328 0.1684955


Please do not forget to cite the maxLik package in your publications.

Best regards,
Arne

RE: maxLik$estimate returns NULL [ Reply ]
By: uh lo on 2013-07-29 21:54
[forum:39758]
I really am embarassed - sorry. Z has to be capitalized.. Next try! I can run this code without error messages. I hope, the code will work for you. Many thanks for your help!

main <- function(){
n <- 100
theta <- c(0.2, 1, 0)
Z <<- rnorm(n, 0, 2)
probs <- m0(theta)
delta <<- rbinom(n, 1, probs)
loglik <- function(theta) (1/n * sum(delta*w1(theta) + (1-delta)*w2(theta)))
w1 <- function(theta) (log(m0(theta)))
w2 <- function(theta) (log(1-m0(theta)))
m0 <- function(theta) (1-exp(-exp(theta[1] + theta[2]*Z + theta[3]*Z^2 )))
a <- maxLik(loglik, start=theta, print.level=0)
print( a$estimate )
}

RE: maxLik$estimate returns NULL [ Reply ]
By: Arne Henningsen on 2013-07-29 21:09
[forum:39757]
Now I get the following error message:

Error in m0(theta) : object 'Z' not found

Please make sure that your example is reproducible before you post the code here. Thanks!

RE: maxLik$estimate returns NULL [ Reply ]
By: uh lo on 2013-07-29 19:21
[forum:39756]
Thanks for your response! n should be 100 - sorry for that!
The problem is that m returns 1, even though it never actually is 1. Therefore w2 is infinite and that ruins everything and maxLik returns null. Unfortunately I still do not know what to do. The code looks like:

z <- rnorm(100, 2)
delta <- rbinom(100, 1, 0.5)
theta <- c(0.2, 1, 0)
loglik <- function(theta) (1/100 * sum(delta*w1(theta) + (1-delta)*w2(theta)))
w1 <- function(theta) (log(m0(theta)))
w2 <- function(theta) (log(1-m0(theta)))
m0 <- function(theta) (1-exp(-exp(theta[1] + theta[2]*Z + theta[3]*Z^2 )))
#m0 <- function(theta) (exp(theta[1] + theta[2]*z + theta[3]*z^2 ) / (1 + exp(theta[1] + theta[2]*z + theta[3]*z^2))) #this one works fine!
a <- maxLik(loglik, start=theta, print.level=0)
print( a$estimate )

RE: maxLik$estimate returns NULL [ Reply ]
By: Arne Henningsen on 2013-07-28 20:43
[forum:39748]
Hi Jacqueline:

I get the error message:

> main()
Error in fnOrig(theta, ...) : object 'n' not found

because the variable "n" in your "loglik" function is not defined.

Best regards,
Arne



maxLik$estimate returns NULL [ Reply ]
By: uh lo on 2013-07-20 13:02
[forum:39740]
Hello,
I've got a problem using maxLik with a special loglik-function. It only returns NULL whereas in other examples it works fine.. Does anyone know why and would be so kind as to help me? :-) Thanks a lot!
Here is my code:

main <- function(){
z <- rnorm(100, 2)
delta <- rbinom(100, 1, 0.5)
theta <- c(0.2, 1, 0)
loglik <- function(theta) (1/n * sum(delta*w1(theta) + (1-delta)*w2(theta)))
w1 <- function(theta) (log(m0(theta)))
w2 <- function(theta) (log(1-m0(theta)))
m0 <- function(theta) (1-exp(-exp(theta[1] + theta[2]*Z + theta[3]*Z^2 )))
#m0 <- function(theta) (exp(theta[1] + theta[2]*z + theta[3]*z^2 ) / (1 + exp(theta[1] + theta[2]*z + theta[3]*z^2))) #this one works fine!
a <- maxLik(loglik, start=theta, print.level=0)
print( a$estimate )
}

Thanks to:
Vienna University of Economics and Business Powered By FusionForge