RE: [S] Integration difficulties

Steven Paul Millard (probstat@nwrain.com)
Wed, 11 Mar 1998 10:43:34 -0800


Hello,

Nicole DePriest Demers wrote about difficulty using the integrate()
function with a function that takes an argument called "k". The
problem with integrate(), nlmin(), nlminb(), and a few other functions
like these is that they have their own arguments as well as the
ellipsis argument (...), and S-PLUS does partial matching on argument
names.

For the integrate() function, the ellipsis argument passes argument
values from the call to integrate() to the function f that is being
integrated. Unfortunately, Nicole's function moment() takes an
argument called "k", but integrate() has an argument called "keep.xy".
When you call integrate() and set k=2, integrate thinks you are trying
to set the value of keep.xy, not the value of k in the call to the
function moment(). You can see this in the value of ans$call:

> ans$call
integrate(f = moment, lower = - Inf, upper = -1, keep.xy = 2,
theta = 0.1)

For this reason, I always use unusual argument names for functions that
I know I will be using with integrate(), nlmin(), nlminb(), etc. For
example, Nicole could rename the argument k to k.weird, and then set
k.weird=2 within the call to integrate:

> moment_function(x,theta,k.weird=1) x^k.weird*p.mix(x,theta)
> p.mix_function(x,theta) .5*dnorm(x,theta) + .5*dnorm(x,-theta)
> ans_integrate(moment,-Inf,-1,theta=0.1,k.weird=2)
> ans$call
integrate(f = moment, lower = - Inf, upper = -1, theta = 0.1,
k.weird = 2)

Sincerely,

--Steve M.

_____________
| *** | Steven P. Millard, Ph.D.
| * |
| * * * | P robability, TEL: 206-528-4877
| * * * | S tatistics & FAX: 206-528-4802
| * | I nformation E-mail: SMillard@ProbStatInfo.com
| * | Web: www.ProbStatInfo.com
| *** | 7723 44th Avenue NE
|___________| Seattle, WA 98115-5117 USA

-----Original Message-----
From: Nicole DePriest Demers [SMTP:ntd@stat.ohio-state.edu]
Sent: Tuesday, March 10, 1998 3:11 PM
To: s-news@wubios.wustl.edu
Subject: [S] Integration difficulties

I am trying to integrate a function and a few problems have arisen.

#Function to be integrated is

moment_function(x,theta,k=1) x^k*p.mix(x,theta)

#where p.mix is a mixture of two normal distributions

p.mix_function(x,theta) .5*dnorm(x,theta) + .5*dnorm(x,-theta)

Note what happens in the following cases:

1) > ans_integrate(moment,-Inf,-1,theta=0.1,k=2)
> ans$integral
[1] -0.2443884
> ans$call
integrate(f = moment, lower = - Inf, upper = -1, keep.xy = 2,
theta = 0.1)

#The integrate function ignored my k=2 argument in the call to
#integrate. The result is for k=1.

2) #I removed the default value for k
> moment_function(x,theta,k) x^k*p.mix(x,theta)
> ans_integrate(moment,-Inf,-1,theta=0.1,k=2)
Error in qsub(f, bound, sgn, 0, 1, aux = optargs(l..: singularity
encountered
Dumped

3) #I switched the order of k and theta.
> moment_function(x,k,theta=.1) x^k*p.mix(x,theta)
> ans_integrate(moment,-Inf,-1,k=2)
Error in qsub(f, bound, sgn, 0, 1, aux = optargs(l..: singularity
encountered
Dumped

4) #I supply defaults for k and theta and send a different value for
#theta when I call integrate.
> moment_function(x,k=2,theta=.1) x^k*p.mix(x,theta)
> ans_integrate(moment,-Inf,-1,theta=0.2)
> ans$call
integrate(f = moment, lower = - Inf, upper = -1, theta = 0.2)

#For some reason, I need a default value for k in my integrand function
but not for theta. When k is supplied as an argument to integrate it
is
ignored but theta is not. I want to vary both k and theta within the
integrate command. All suggestions/explanations will be appreciated.

Nicole Demers

____________________________________________________________________
__________
Nicole Demers
Statistics Department
The Ohio State University
1958 Neil Ave., #404
Columbus, OH 43210, USA
ntd@stat.ohio-state.edu

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To
unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news