[R] Optim question

From: Doran, Harold <HDoran_at_air.org>
Date: Thu 24 Aug 2006 - 22:54:24 EST

This is a very basic question, but I am a bit confused with optim. I want to get the MLEs using optim which could replace the newton-raphson code I have below which also gives the MLEs. The function takes as input a vector x denoting whether a respondent answered an item correctly (x=1) or not (x=0). It also takes as input a vector b_vector, and these are parameters of test items (Rasch estimates in this case)

For example, here is how my current function operates.

> rasch.max(c(1,1,0,0), c(-1,.5,0,1))

theta is about 0.14 , se 1.063972

I'm not quite sure how to accomplish the same thing using optim. Can anyone offer a suggestion?

rasch.max <- function(x, b_vector){

   p <- numeric(length(b_vector))
   theta <- log(sum(x)/(length(x)/sum(x))) # This is a starting value for theta

   rasch <- function(theta,b) 1/ (1 + exp(b-theta))    old <- 0
   updated <- 5
   while(abs(old-updated) > .001){

      old <- updated
      for(k in seq(along=b_vector)) p[k] <- rasch(theta,b_vector[k])
      first_deriv  <- sum(x) - sum(p)
      second_deriv <- sum((1-p)*-p)
      change       <- (first_deriv/second_deriv)
      theta        <- theta - change # This is the updated theta
      updated      <- change

cat('theta is about', round(theta,2), ', se', 1/sqrt(-second_deriv), '\n')


> version

platform       i386-pc-mingw32           
arch           i386                      
os             mingw32                   
system         i386, mingw32             
major          2                         
minor          3.0                       
year           2006                      
month          04                        
day            24                        
svn rev        37909                     
language       R                         
version.string Version 2.3.0 (2006-04-24)

        [[alternative HTML version deleted]]

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu Aug 24 23:05:39 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 25 Aug 2006 - 02:23:14 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.