As a general stragtey, always try to evaluate your function at least once
(using the starting values) before you pass it to optim. If
logit.fn(par=c(0, 0, 0), x = X, y = Y)
does not return a scalar, then the function is incorrect. Take another close
look at the logit.fn and gary's lectur slides on the logit.
To debug:
Set
x=X
y=Y
par= c(0, 0, 0)
and run the function line by line.
Hope this helps.
Jens
From: gov2001-l-bounces at
lists.fas.harvard.edu
[mailto:gov2001-l-bounces at
lists.fas.harvard.edu] On Behalf Of Marcy
McCullaugh
Sent: Tuesday, March 18, 2008 1:51 PM
To: gov2001-l at
lists.fas.harvard.edu
Subject: Re: [gov2001-l] Optim problem
Hi Jon,
Thanks for responding.? My likelihood function is:
logit.fn <- function(par, x, y){
??? beta<- par[1:ncol(x)]
??? out<- -sum(log(1+exp((1-2*y)*x%*
%beta)))
??? return(out)
??? }
I fixed the data as follows:
space <- as.data.frame(space)
x1 <- space$Temperature
x2 <- space$Pressure
X <- cbind(1, x1, x2)
Y <- space$Incident
The error message I'm getting is: "Error in optim(c(0, 0, 0), logit.fn, x =
X, y = Y, method = "BFGS", control = list(fnscale = -1),? :
??????? initial value in 'vmmin' is not finite
In addition: Warning message:
* not meaningful for factors in: Ops.factor(2, y)"
I know I'm making a stupid mistake but can't tell what it is.? I thought my
optim command was wrong but maybe the problem is in my function.?
Thanks,
Marcy
On Tue, Mar 18, 2008 at 6:49 AM, Jon Bischof <jbischof at fas.harvard.edu>
wrote:
Hey Marcy,
Nothing looks wrong with your optim function, but the relevant error
is probably in your likelihood function. Without more information (or
some idea of what the error is), it's hard to help.
Jon
2008/3/18, Marcy McCullaugh <mmccull at fas.harvard.edu>:
Hi,
I'm having trouble getting optim to work in Part B. ?My code is as
follows:
opt.logit <- optim(c(0,0,0), logit.fn, x=X, y=Y, method="BFGS",
control=list(fnscale=-1), hessian=T)
My logit function takes par, x and y; X is a matrix with 1, x1 and x2, and
Y
is a vector. ?I can't see what I'm doing
wrong so any help would be most
appreciated!
Thanks,
Marcy
--
Marcy E. McCullaugh
Ph.D. student
Department of Political Science
University of California, Berkeley
210 Barrows Hall
Berkeley, CA 94720
Exchange Scholar 2007-08
?Department of Government and
Davis Center for Russian and Eurasian Studies
Harvard University
1730 Cambridge Street, 3rd Floor
Cambridge, MA 02138
_______________________________________________
?gov2001-l mailing list
?gov2001-l at
lists.fas.harvard.edu
?http://lists.fas.harvard.edu/mailman/listinfo/gov2001-l
--
Jon Bischof
Graduate Student
Department of Government
Harvard University
_______________________________________________
gov2001-l mailing list
gov2001-l at
lists.fas.harvard.edu
http://lists.fas.harvard.edu/mailman/listinfo/gov2001-l
--
Marcy E. McCullaugh
Ph.D. student
Department of Political Science
University of California, Berkeley
210 Barrows Hall
Berkeley, CA 94720
Exchange Scholar 2007-08
Department of Government and
Davis Center for Russian and Eurasian Studies
Harvard University
1730 Cambridge Street, 3rd Floor
Cambridge, MA 02138