I am using the optimx::optimx() function to estimate some model parameters. A co-worker runs the optimizer using the MS R Open 4.0.2 version of R while I am using the 4.0.2 version downloaded from CRAN. Package version numbers are the same, but I am only getting believable parameter estimates when using MS R Open 4.0.2.
My questions is. Is there a explainable reason that these two distributions of the same version of R are performing differently?
Below is some code running the optimx() function taken from https://www.ime.unicamp.br/~cnaber/optim%202.pdf
Running this, I get two very different maximum likelihood estimates.
# Y is the binary response data.
# X is the covariate data, each row is the response data
# for a single subject. If an intercept is desired, there
# should be a column of 1’s in X
# V is the prior variance (10 by default)
# when V = Inf, this is maximum likelihood estimation.
posterior.mode <- function(Y, X,V=10)
{
# sample size
n <- length(Y)
# number of betas
d <- ncol(X)
# the log posterior as a function of the vector beta for subject i data
log.like_one <- function(i, beta)
{
# p_{i}, given X_{i} and beta
Phi.Xb <- pnorm( sum(X[i,] * beta) )
return( log(1 - Phi.Xb) + Y[i]*log( Phi.Xb/(1 - Phi.Xb) ) )
}
# log likelihood of the entire sample
loglike <- function(beta)
{
L <- 0
for(ii in 1:n) L <- L + log.like_one(ii,beta)
return(L)
}
# *negative* log posterior of the entire sample
log.posterior <- function(beta) -loglike(beta) + (1/(2*V))*sum(beta^2)
# return the beta which optimizes the log posterior.
# initial values are arbitrarily chosen to be all 0’s.
return( optim( rep(0,d), log.posterior)$par )
}
# The covariates are standard normal
X <- cbind( rep(1, 20), rnorm(20), rnorm(20) )
Y <- rep(0,20)
Beta <- c(.43, -1.7, 2.8)
for(i in 1:20)
{
# p_i
pp <- pnorm( sum(Beta * X[i,]) )
if( runif(1) < pp ) Y[i] <- 1
}
# fit the model with prior variance equal to 10
posterior.mode(Y,X)
# very small prior variance
posterior.mode(Y,X,.1)
# maximum likelihood estimate
posterior.mode(Y,X,Inf)