> # finder.r October 2008, 2009 > # > # Example 8.3 -- maximize likelihood/root of likelihood equation > # > x <- c(1,1,1,1,1,1,2,2,2,3) # data > # define log-likelihood function > llike <- function (theta) { # loglikelihood for log-series model + n <- length(x) + sumx <- sum(x) + llike <- sumx*log(theta) - n*log(-log(1-theta)) } > # > # note that llike only used data x -- not general > # > # derivative log-likelihood function > dllike <- function (theta,u) { # derivative of log-series llike + n <- length(u) + sumu <- sum(u) + dllike <- sumu/theta + n/((1-theta)*log(1-theta)) } > # > # note generality of dllike -- data as argument to function > # > # another way to define & use function > dllikex <- function(theta) dllike(theta,x) > # > # still another approach > llikex <- function (x) { # loglikelihood for log-series model + n <- length(x) + sumx <- sum(x) + function(theta) sumx*log(theta) - n*log(-log(1-theta)) } > # > # First approach ** maximize llike > # > # with one parameter, use optimize > # function limits > that1 <- optimize(f=llike, c(.02,.98), maximum=TRUE ) > print("maximize loglikelihood") [1] "maximize loglikelihood" > that1 $maximum [1] 0.5335877 $objective [1] -6.71288 > print("value at maximum") [1] "value at maximum" > t <-that1$maximum > t [1] 0.5335877 > f <- llike(t) > f [1] -6.71288 > # > # Second approach ** find root of likelihood equation > # > # with one parameter, use uniroot > # function limits pass data > that2 <- uniroot(f=dllike, lower=.02, upper=.98, u=x ) > print("root of likelihood equation") [1] "root of likelihood equation" > that2 $root [1] 0.5335918 $f.root [1] -8.666671e-05 $iter [1] 7 $estim.prec [1] 6.103516e-05 > print("value at root") [1] "value at root" > t <- that2$root > t [1] 0.5335918 > # > # Third approach ** find root of likelihood equation > # > # again with uniroot but with specific function > # function limits v no x passed > that3 <- uniroot(f=dllikex, lower=.02, upper=.98 ) > print("root of likelihood equation") [1] "root of likelihood equation" > that3 $root [1] 0.5335918 $f.root [1] -8.666671e-05 $iter [1] 7 $estim.prec [1] 6.103516e-05 > print("value at root") [1] "value at root" > t <- that3$root > t [1] 0.5335918 > f <- llike(t) > f [1] -6.71288 > # > # Fourth approach ** maximize llike > # > # with one parameter, use optimize > # function limits > xm1 <- c(0,0,0,0,0,0,1,1,1,2) > that4 <- optimize(f=llikex(xm1+1), c(.02,.98), maximum=TRUE ) > print("maximize loglikelihood") [1] "maximize loglikelihood" > that4 $maximum [1] 0.5335877 $objective [1] -6.71288 > print("value at maximum") [1] "value at maximum" > t <-that1$maximum > t [1] 0.5335877 > ff <- llikex(xm1+1) # creates function > ff function (theta) sumx * log(theta) - n * log(-log(1 - theta)) > ff(t) # evaluate function at t [1] -6.71288 > # done > rm(list=ls()) > q()