Log-likelihood optimization, transfer in different data sets

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 
+4
source share
2 answers

optim allows optim to pass additional parameters to the function that you optimize. In this case, you just need to add data_x=your_new_data_set to the optimization parameters.

 optim(c(0,1), loglike, data_x = your_new_data_set) 

This is what the option ... allows for optim . Check out ?optim for more details.

+5
source

Use the argument ... to optimize:

 y <- 1:100 optimization<-optim(c(0,1), loglike, data_x=y) 
+2
source

Source: https://habr.com/ru/post/1448026/


All Articles