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? 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 at hamanndonald.com www.hamanndonald.com
Spencer Graves
2003-Sep-30 19:01 UTC
[R] Preserving names from "p" to "f" in "nlm" (was: can't get names from vector in nlm calls)
Perhaps someone else can help you figure out how to preserve the names from argument "p" to function "f" in calls to "nlm", but I'm now beyond the limits of my knowledge. I'm sorry, but I can't help further with this. Spencer Graves ######################## Jeff D. Hamann wrote: 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. Jeff. ----- Original Message ----- From: "Spencer Graves" <spencer.graves at pdf.com> To: "Spencer Graves" <spencer.graves at pdf.com> Cc: "Jeff D. Hamann" <jeff_hamann at hamanndonald.com> Sent: Tuesday, September 30, 2003 5:44 AM Subject: Re: [R] can't get names from vector in nlm calls> I've written functions to ask first if the input vector had >names. If it didn't, I checked the length and then assigned the names I >expected it to have. Then I checked the names. That worked for me. > > I believe it would be possible to get nlm/optim to preserve the >names, as you ask. However, it would not be easy, and it might slow >down the code. To check this, I typed "nlm" and "optim" at a command >prompt and read the code. When I did that, I found that most of the >work in both is done via a call to ".Internal", which passes control to >a compiled function. R is open source, which means you could get the >source code and modify it to do what you want. However, that would be >difficult, at least for me. > > hope this helps. spencer graves > >Jeff D. Hamann wrote: > > >optim give the same results. is it possible to "get" nlm/optim to > >preserve> >the names? > > > > > >----- Original Message ----- > >From: "Spencer Graves" <spencer.graves at pdf.com> > >To: "Jeff D. Hamann" <jeff_hamann at hamanndonald.com> > >Sent: Monday, September 29, 2003 8:18 PM > >Subject: Re: [R] can't get names from vector in nlm calls > > > > > > > > > >> I have not used nlm, but it may not preserve the names of the > >>starting values in "p". > >> > >> hope this helps. spencer graves > >> > >>Jeff D. Hamann wrote: > >> > >> > >> > >>>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? > >>> > >>>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 at hamanndonald.com > >>>www.hamanndonald.com > >>> > >>>______________________________________________ > >>>R-help at stat.math.ethz.ch mailing list > >>>https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >