##### R code for the initial likelihood calculations ##### in the AMI mortality case study (Lecture Notes, Day 1, Part 2) n <- 400 s <- 72 bernoulli.likelihood <- function( theta, s, n ) { log.likelihood <- s * log( theta ) + ( n - s ) * log( 1 - theta ) return( exp( log.likelihood ) ) } theta.grid.1 <- seq( 0, 1, length = 500 ) likelihood.grid.1 <- bernoulli.likelihood( theta.grid.1, s, n ) par( mfrow = c( 1, 2 ) ) plot( theta.grid.1, likelihood.grid.1, type = 'l', xlab = 'theta', ylab = 'Likelihood Function' ) theta.grid.2 <- seq( 0.12, 0.25, length = 500 ) likelihood.grid.2 <- bernoulli.likelihood( theta.grid.2, s, n ) plot( theta.grid.2, likelihood.grid.2, type = 'l', xlab = 'theta', ylab = 'Likelihood Function' ) par( mfrow = c( 1, 1 ) ) bernoulli.log.likelihood <- function( theta, s, n ) { log.likelihood <- s * log( theta ) + ( n - s ) * log( 1 - theta ) return( log.likelihood ) } log.likelihood.grid.1 <- bernoulli.log.likelihood( theta.grid.2, s, n ) plot( theta.grid.2, log.likelihood.grid.1, type = 'l', xlab = 'theta', ylab = 'Log Likelihood Function' )