saltthehash
saltthehash

Reputation: 316

R function optim(): object not found

So I've been working on a script to calculate log likelihood based on specifically 4 parameters and put them into a particular log-lik function. That script is fine. The issue is optimizing it - whenever I try, it says that the object (for the parameter in question) can not be found. For the sake of simplicity, I will just use a much simpler script that is giving me an identical error when I use the optim() function. The funny part is that I actually pulled this script directly out of a "MLE in R" tutorial. I didn't even rewrite it, I literally copied/pasted it from the PDF. Here is the likelihood function from the tutorial:

normal.lik1<-function(theta,y){
mu<-theta[1]
sigma2<-theta[2]
n<-nrow(y)
logl<- -.5*n*log(2*pi) -.5*n*log(sigma2) - (1/(2*sigma2))*sum((y-mu)**2)
return(-logl)
}

This function works perfectly fine on it's own. However, when I try to optimize it, I get an error saying that the object is not found, the object being the parameter I'm trying to optimize. This next line is also copy/pasted from the tutorial:

optim(c(0,1),normal.lik1,y=y,method="BFGS")

When I run this line, R gives me the following error:

Error in nrow(y) : object 'y' not found

That immediately made me realize that it's talking about how y is an object specifically within the function, not a global one, but at the same time optim() is supposed to be evaluating that very function! So, I don't know why it is acting like y doesn't exist.

EDIT:

Now I understand how it works. But I am still having an issue with my overall goal. To put it more into perspective, here is the code I am working with now:

w.loglik<-function(w){ 
  # w is just a vector with 4 values: two means (w(1) and w(2) below) and the position
  # of two decision bounds (w(3) and w(4))

  #create data matrix
  data<-matrix(c(140,36,34,40,89,91,4,66,85,5,90,70,20,59,8,163),nrow=4,ncol=4,byrow=TRUE)  

  # get means
  means<-matrix(0,4,2,byrow=TRUE)
  means[2,1]<-w[1]
  means[3,2]<-w[2]
  means[4,1]<-w[1]
  means[4,2]<-w[2]

  # get covariance matrices (fix variances to 1)
  covmat<-list()
  covmat[[1]]<-diag(2)
  covmat[[2]]<-diag(2)
  covmat[[3]]<-diag(2)
  covmat[[4]]<-diag(2)

  # get decision bound parameters
  b<-diag(2)
  c<-matrix(c(w[3],w[4]),2,1)

  L<-matrixloglik(data,means,covmat,b,c)
  return(L)
}

matrixloglik is just a function that outputs a log likelihood (it runs fine). How can I run optim() so that I optimize the vector w?

Upvotes: 1

Views: 3027

Answers (1)

Metrics
Metrics

Reputation: 15458

y is your data and you haven't specified that in your code. Hence the error Error in nrow(y) : object 'y' not found

If you go the example 5 of same pdf file and use the data y as defined below, you will have an output:

X<-cbind(1,runif(100))
theta.true<-c(2,3,1)
y<-X%*%theta.true[1:2] + rnorm(100)

> optim(c(0,1),normal.lik1,y=y,method="BFGS")
$par
[1] 3.507266 1.980783

$value
[1] 176.0685

$counts
function gradient 
      49       23 

$convergence
[1] 0

$message
NULL

Warning messages:
1: In log(sigma2) : NaNs produced
2: In log(sigma2) : NaNs produced
3: In log(sigma2) : NaNs produced

Note: Please see the "An Introduction to R" to understand the basics in function.

Update: as per comments: You can try doing and see what happens:

y<-X%*%theta.true++ rnorm(100)
Error in X %*% theta.true : non-conformable arguments

Upvotes: 4

Related Questions