[R] Unable to pass Object Arguments to UniRoot()

Bazman76 h_a_patience at hotmail.com
Mon Aug 10 12:26:35 CEST 2015


Hi there,

I'd like to be able to pass an entire object to uniroot() as one of the
arguments. Unfortunately to recreate my precise error would be rather
involved. So I present a simplified version below:

To create a structured object let the output from the lm() function use this
simple code: 

    year <- c(2000 ,   2001  ,  2002  ,  2003 ,   2004)
    rate <- c(9.34 ,   8.50  ,  7.62  ,  6.93  ,  6.60)
    fit <- lm(rate ~ year)


    When I try to pass this to uniroot:

    OptLam<-uniroot(f=LamOpt, interval=c(0.0001,120),
tol=0.0000000000000001, MLEObj=fit )

I get the following error

Error in f(lower, ...) : 
  unused argument(s) (MLEObj = list(coefficients = c(1419.2079999999,
-0.704999999999952), residuals =

This seems to be specific to uniroot I have tested my own functions and they
happily accept objects like fit. The function LamOpt is not supposed to work
with a object created by lm() I've just used lm() as a simple example so
don't worry about the results that LamOpt produces, if I can pass one object
like fit to uniroot then I assume I can pass any object.

It seems that uniroot must be expecting an object of a certain size, would
it therefore be possible to use something analogous to pointers in c/c++ and
pass the object by reference so only a pointer to the object is passed
rather than the object itself?



    LamOpt<-function(lambda,MLEobj){

    x <- c(3,6,9,12,15,18,21,24,30,36,48,60,72,84,96,108,120)

    #MLEObj<-kemfit
    ## Cross-sectional model (Nelson and Siegel)
    #lambda <- 0.0609
    h2 <- function(x){(1-exp(-lambda*x))/(lambda*x)}
    h3 <- function(x){((1-exp(-lambda*x))/(lambda*x)) - exp(-lambda*x)}
    X=cbind(rep(1,m), h2(x), h3(x)) #obs

    Z <- matrix(as.list(X), ncol = 3)

    MLEObj$par$Z=Z

    kf_iter = MARSSkf(MLEobj)

    return(-kf_iter$logLik)
    }

Thanks

Baz
   




--
View this message in context: http://r.789695.n4.nabble.com/Unable-to-pass-Object-Arguments-to-UniRoot-tp4710938.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list