----- Original Message ----- From: "Feng Chen" <[EMAIL PROTECTED]>
To: "Uwe Ligges" <[EMAIL PROTECTED]>
Sent: Thursday, February 24, 2005 9:52 PM
Subject: Re: [R] a question about function eval()



Dear Uwe,

Thanks for your advice. As you may have noticed, I am a newbie to R and not fully aware of the power of such mechanisms like eval(), call(), substitute(), ...

In fact, the likelihood function I want to maximize has a nonparametric component. I am trying to approximate it using special functions of known form but with a finite number, say n, of unknown parameters. I want the number n to be variable, so I use vector formals to pass into the (log)likelihood function the parameters of the approximating function. I tried to maximize the loglikelihood, but without success. Then I guess the function passed into mle() should not have vectors or lists as its arguments. Therefore, I try to generate functions with a variable number of arguments based on the loglikelihood function and use mle() to the new functions. That is why I have got all these troubles. Maybe there are more convenient alternatives I simply don't know.

By the way, If I use objects globally visible (such as constants) as the default values for coef.sex,...,knot.val.n. Then it does work. But now I am just curious whether it is possible to use objects existing in the parent function as the default values for the arguments of the child function (in this eval() case).

Any advice is appreciated,
Feng

----- Original Message ----- From: "Uwe Ligges" <[EMAIL PROTECTED]>
To: "Feng Chen" <[EMAIL PROTECTED]>
Cc: <[email protected]>
Sent: Thursday, February 24, 2005 7:59 PM
Subject: Re: [R] a question about function eval()



Feng Chen wrote:

Hi,

I have a question about the usage of eval(). Wonder if any experienced user can help me out of it.

I use eval() in the following function:
semireg.pwl <- function(coef.s=rnorm(1),coef.a=rnorm(1),knots.pos=knots.x,knots.ini.val=knots.val){
knotn <- length(knots.pos)
def.par.env <- sys.frame(1)
print(def.par.env)
print(environment(coef.s))
tg <- eval( (parse(text=
paste(
"function(coef.sex=coef.s,coef.age=coef.a,",


paste("knot.val.",1:knotn,"=knots.ini.val[",1:knotn,"]",sep="",collapse=","),
")",
"{\n print(sys.frame());print(coef.sex)\n y <- c(",


paste("knot.val.",1:knotn,sep="",collapse=","),
                           ")\n",


" -loglikelihood(coef.sex,coef.age,knots.pos,y)\n}", sep="",collapse="" ) ) ) ,envir=def.par.env)

Why do you want to use eval(parse(.....))? That's not sensible in this case. You can either alculate the stuff doirectly or define a function instead....


Uwe Ligges



  print(tg())
  print(coef.s)
  print(sys.frame(1))
  mle(tg)
}

But when I ran semireg.pwl(), I got correct value for tg(), but an message "Error in eval(expr, envir, enclos) : Object "coef.s" not found" for mle(tg). I just don't know how to make the environment variable visible for mle().

Thanks a lot!

Feng
[[alternative HTML version deleted]]

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


______________________________________________ [email protected] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to