[Rd] getting names of p vector in nlm function...
Prof Brian Ripley
ripley at stats.ox.ac.uk
Mon Oct 6 20:20:17 MEST 2003
The names never change, so having nlm/optim preserve them is just a
(considerable) waste of resources. You can however pass them as a
separate parameter via the ... argument of nlm/optim.
On Mon, 6 Oct 2003, Jeff D. Hamann wrote:
> 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 at hamanndonald.com
> www.hamanndonald.com
>
> ______________________________________________
> R-devel at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-devel
>
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-devel
mailing list