From jeff_hamann at hamanndonald.com  Mon Oct  6 11:17:38 2003
From: jeff_hamann at hamanndonald.com (Jeff D. Hamann)
Date: Mon Oct  6 19:17:06 2003
Subject: [Rd] getting names of p vector in nlm function...
Message-ID: <006901c38c2d$c161e0a0$0a00a8c0@rodan>

Dear R programming folks:

I'm trying to finish off a package for non-linear simultaneous system
estimation and I've been trying to figure out how to get the names of the
parameter vector variables when inside the function that nlm calls to return
the objective function value:

knls <- function( theta, eqns, data, fitmethod="OLS", instr=NULL, S=NULL )
  {

    ##     print( names( theta ) )              # returns NULL
    ## get the values of the parameters
    for( i in 1:length( theta ) )
      {
        val <- theta[i]
        storage.mode( val ) <-  "double"
        assign( names( theta )[i], val )          # gags here cause I can't
get the names...
      }

    ## resids = eval( lhs ) - eval( rhs )
    for( i in length( eqns ) )
      {
        lhs[[i]] <- eval( formula( eqns[[i]] )[2] )
        rhs[[i]] <- eval( formula( eqns[[i]] )[3] )
        residi[[i]] <- lhs[[i]] - rhs[[i]]
        r <- rbind( r, as.matrix( residi[[i]] ) )
      }

    ## blah, blah, blah....

    knls <- obj
}


print( "calling nlstest" )
demand2 <- q ~ d0 + d1 * p + d2 * d
supply2 <- q ~ s0 + s1 * p + s2 * f + s3 * a
system2 <- list( demand2, supply2 )
sv2 <- c(d0=3,d2=4,d1=4.234,s0=-2.123,s2=2.123,s3=4.234,s1=0.234)

### call the nlm function to get the estimates...
estnew <- nlm( knls, sv2, hessian=TRUE, print.level=1, eqns=system2,
data=kmenta, fitmethod="OLS" )

My question is why can't I simply call names on the vector (sv2) that is
passed into the nlm function to get the parameters? I was assuming that the
names, if available, are always present no matter where they get passed. Is
that not correct? Optim give the same results. Is it possible to "get"
nlm/optim to preserve the names without modifying the C code? I started
looking at the R and C code to examine where to change this and found the

SEXP do_nlm(SEXP call, SEXP op, SEXP args, SEXP rho)

function, but I'm not really sure how to modifiy yet to preserve the
variable names, if there are variable names when it gets to that function.
Plus I'd have to install lots of new and potentially conflicting tools on my
win32 machine and then debug to the code... so i think I'm going to have to
find another solution to my problem, which is a drag as the solution I
originally thought of (get the names from p vector) would be the best one so
far.



Thanks,
Jeff.


---
Jeff D. Hamann
Hamann, Donald and Associates, Inc.
PO Box 1421
Corvallis, Oregon USA 97339-1421
(office) 541-754-1428
(cell) 541-740-5988
jeff_hamann@hamanndonald.com
www.hamanndonald.com


[Rd] getting names of p vector in nlm function...
Jeff D. Hamann