R version 2.9.0 (2009-04-17) Copyright (C) 2009 The R Foundation for Statistical Computing ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > # augrain1.r > # Example 9.5 -- maximize likelihood/concentrated likelihood > # > # data > both <- matrix(scan("augrain.dat"),45,2,byrow=TRUE) > y <- both[,2] > y [1] 4.63 4.18 6.69 1.77 3.66 1.37 3.65 1.95 1.88 5.86 6.45 2.53 5.07 2.94 1.37 [16] 4.05 0.92 2.20 4.95 4.15 3.82 2.85 3.36 4.68 5.34 3.99 7.67 3.84 4.74 3.94 [31] 4.20 4.71 2.33 4.28 4.20 5.89 5.43 3.95 2.98 1.19 2.85 3.88 6.97 2.89 2.60 > # define log-likelihood function > unlike <- function (theta) { # loglikelihood for 2 param gamma + n <- length(y) + sumy <- sum(y) + sumly <- sum(log(y)) + unlike <- n*theta[1]*log(theta[2]) + n*lgamma(theta[1]) - + (theta[1]-1)*sumly + sumy/theta[2] } > # > # concentrated/profile log-likelihood function > lcalph <- function (alpha) { # profile llike + n <- length(y) + sumy <- sum(y) + sumly <- sum(log(y)) + lcalph <- - n*alpha*log(sumy/(n*alpha)) - n*lgamma(alpha) + + (alpha-1)*sumly - n*alpha } > # > # define gradient function > grunlike <- function (theta) { # gradient of unlike + n <- length(y) + sumy <- sum(y) + sumly <- sum(log(y)) + dldalph <- n*log(theta[2]) + n*digamma(theta[1]) - sumly + dldbeta <- n*theta[1]/theta[2] - sumy/(theta[2]**2) + grunlike <- c(dldalph,dldbeta) } > # > # starting values from method of moments > n <- length(y) > sy <- sum(y) > vy <- var(y) > b0 <- n*vy/sy > a0 <- sy*sy/(n*n*vy) > v <- unlike(c(a0,b0)) > print("method of moments estimates for starting values") [1] "method of moments estimates for starting values" > print(c(a0,b0,v)) [1] 5.8357604 0.6582023 84.7092968 > # maximize profile like > that1 <- optimize(f=lcalph, c(a0/5,a0*5), maximum=TRUE ) > print("maximize concentrated loglikelihood") [1] "maximize concentrated loglikelihood" > that1 $maximum [1] 5.1414 $objective [1] -84.50925 > print("value at maximum") [1] "value at maximum" > ta <-that1$maximum > ta [1] 5.1414 > f <- lcalph(ta) > f [1] -84.50925 > # check gradient > tb <- sy/(n*ta) > print(c(ta,tb)) [1] 5.1414002 0.7470944 > grad <- grunlike(c(ta,tb)) > grad [1] 2.676173e-06 5.684342e-14 > # check original loglikelihood function > f <- unlike( c(ta,tb) ) > f [1] 84.50925 > # done > rm(list=ls()) > q()