Hi, Jerome and Phil,
Thank you for your solutions and I have studied carefully your codes but I have 
further questions (since I guess the simple lines of codes may not do the real 
job I am going to describe to you. Please forgive me for my shallowness!)

 I guess I over-simplified my question, basically I need such a function as the 
integrand for estimation of the expectation by Monte Carlo methods.

Please allow me to state the problem in more details:

I have to define a function for Monte Carlo computation of conditional 
expectation and solve for the argument for which the expectation equals a 
pre-specified value. Say, the integrand function is f(x,y,z), where x, z are 
deterministic, y probabilistic and follows a distribution F.
I will have to feed x=x0 to f, then I sample from F for y and evaluate 
f(x0,y,z), and use Monte Carlo method to get the expectation, which gives a 
function of z; now that the expectation is a function of z only, say, E(z); 
finally to solve for z such that E(z) = 0.5, for example.
The function f itself is very complicated and has high dimensional vectors as 
arguments except z, which is a real number. 

I am new in R but unexpectedly encountered this symbolic incapability of R as I 
almost finished programming all major computations in R. I have been skillful 
in Matlab and Mathematica (and it is very easy to do this in them) but as I am 
now in statistics I would like to continue in R unless it really is not able to 
do it (in that case I will have to recode in Mathematica).

Any of your further help is much appreciated!
Best regards,
-Chee

 
From: Jerome Asselin 
Sent: Friday, April 29, 2011 12:25 AM
To: Chee Chen 
Cc: R -Help 
Subject: Re: [R] How to define specially nested functions


On Thu, 2011-04-28 at 23:08 -0400, Chee Chen wrote:
> Dear All,
> I would like to define a function: f(x,y,z) with three arguments x,y,z, such 
> that: given values for x,y,  f(x,y,z) is still a function of z and that I am 
> still allowed to find the root in terms of z when x,y are given.
> For example: f(x,y,z) =  x+y + (x^2-z),  given x=1,y=3, f(1,3,z)= 1+3+1-z is 
> a function of z, and then I can use R to find the root z=5.
> 
> Thank you.
> -Chee

Interesting exercise.

I've got this function, which I think it's doing what you're asking.

f <- function(x,y,z)
{
fcall <- match.call()
fargs <- NULL
if(fcall$x == "x")
fargs <- c(fargs, "x")
if(fcall$y == "y")
fargs <- c(fargs, "y")
if(fcall$z == "z")
fargs <- c(fargs, "z")

ffunargs <- as.list(fargs)
names(ffunargs) <- fargs

argslist <- list(fcall)
ffun <- append(argslist, substitute( x+y + (x^2-z) ), after=0)[[1]]
as.function(append(ffunargs, ffun))
}

This yields.

> f(3, 2, z)
function (z = "z") 
3 + 2 + (3^2 - z)
<environment: 0x132fdb8>
> f(3, 2, z)(3)
[1] 11

I haven't figured out how to get rid of the default argument value shown
here as 'z = "z"'. That doesn't prevent it to work, but it's less
pretty.  If you find a better way, let me know.

HTH,
Jerome



        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to