I am trying to do a log-likelihood optimization of a normal distribution. The log likelihood function works and recognizes the data set that is being transmitted, but the optimization does not recognize that the data set exists? If we set data_x=rnorm(100,0,1) , this code will return the correct answer, but I will need to go through different data sets.
x <- rnorm ( 100, 0, 1) loglike <- function( pars, data_x=x) { mu <- pars[1] sigma2 <- pars[2]^2 numobs <- length( data_x ) sumsq <- sum( ( data_x-mu )^2 ) val.log.like <- -numobs / 2 * log( sigma2 ) - ( 1 / (2*sigma2) ) * sumsq return( val.log.like ) } optimization <- optim( c( 0, 1), loglike) answer <- matrix( optimization$par, 2, 1) answer
source share