# finder.r # Example 8.3 -- maximize likelihood/root of likelihood equation # # data x <- c(1,1,1,1,1,1,2,2,2,3) # 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)) } # 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)) } # another way to define & use function dllikex <- function(theta) dllike(theta,x) # # maximize llike that1 <- optimize(f=llike, c(.02,.98), maximum=TRUE ) print("maximize loglikelihood") that1 print("value at maximum") t <-that1$maximum t f <- llike(t) f # find root of likelihood equation that2 <- uniroot(f=dllike, lower=.02, upper=.98, u=x ) print("root of likelihood equation") that2 print("value at root") t <- that2$root t f <- llike(t) f # find root of likelihood equation v nothing passed that3 <- uniroot(f=dllikex, lower=.02, upper=.98 ) print("root of likelihood equation") that3 print("value at root") t <- that3$root t f <- llike(t) f # done rm(list=ls()) q()