[R] Generalizing call to function
Berend Hasselman
bhh at xs4all.nl
Wed Sep 7 18:54:21 CEST 2011
. wrote:
>
> Hello guys,
>
> I would like to ask for help to understand what is going on in
> "func2". My plan is to generalize "func1", so that are expected same
> results in "func2" as in "func1". Executing "func1" returns...
>
> 0.25 with absolute error < 8.4e-05
>
> But for "func2" I get...
>
> Error in dpois(1, 0.1, 23.3065168689948, 0.000429064542600244,
> 3.82988398013855, :
> unused argument(s) (0.000429064542600244, 3.82988398013855,
> 0.00261104515224461, 1.37999516465199, 0.0072464022020844,
> 0.673787740945863, 0.0148414691931943, 0.383193602946711,
> 0.0260964690514175, 0.236612585866545, 0.0422631787036055,
> 0.152456705113438, 0.0655923922306948)
>
> Thanks in advance.
>
> func1 <- function(y, a, rate) {
> f1 <- function(n, y, a, rate) {
> lambda <- a * n
> dexp(n, rate) * dpois(y, lambda)
> }
> integrate(f1, 0, Inf, y, a, rate)
> }
>
> func1(1, 0.1, 0.1)
>
>
> func2 <- function(y, a, rate, samp) {
> f1 <- function(n, y, a, rate, samp) {
>
> SampDist <- function(y, a, n, samp) {
> lambda <- a * n
> dcom <- paste("d", samp, sep="")
> dots <- as.list(c(y, lambda))
> do.call(dcom, dots)
> }
>
> dexp(n, rate) * SampDist(y, a, n, samp)
> }
> integrate(f1, 0, Inf, y, a, rate, samp)
> }
>
> func2(1, 0.1, 0.1, "pois")
>
You need to replace the line with "dots <- as.list(c(y, lambda))" with this
dots <- list(y, lambda)
You could also replace the two lines
dots <- as.list(c(y, lambda))
do.call(dcom, dots)
with the single line
do.call(dcom, list(y, lambda))
See the description of the argument "args" of do.call.
You should now get the same answer as for func1.
Deciding if this is correct is up to you.
Berend
--
View this message in context: http://r.789695.n4.nabble.com/Generalizing-call-to-function-tp3793329p3796637.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list