I am using "optim" package to maximize the log-likelihood function. and I would like to visualize the optimization path till convergence with animation such as this graph: link below.
https://florarblog.files.wordpress.com/2018/08/optimization_path2.gif?w=740&h=507&zoom=2)
Here is the log-likelihood function and optim:
x = runif(500)
x = model.matrix( ~ x)
y = rbinom(500,1,0.5)
theta= c(-0.1,2)
logll <- function(theta)
{
p <- exp(x%*%theta)
p[p >=1] <- 1- 1e-5 ; p[p<=0] <- 1e-5
LL <- sum(y*log(p) + (1-y)*log(1 - p))
return(-LL)
}
optim(theta,logll)
l.grid <- 200
grid.b <- as.matrix(expand.grid(b0=seq(-3,0.5,length.out=l.grid), b1=seq(-6,6,length.out=l.grid)))
grid.f <-apply(grid.b,1,logll)
grid.b <- as.data.frame(grid.b)
contourplot(grid.f~b0+b1,data=grid.b,cuts=15)
Thanks in advance for your help