问题
I have just spent a while trying to find a bug in my code which has turned out to be an unusual (at least to me) parameterisation for the R::dexp
function. For example:
cppFunction("
double my_dexp(double x, double lambda, double is_log) {
return R::dexp(x, lambda, is_log);
}
")
> my_dexp(4.5, 2.5, FALSE)
[1] 0.06611956
> dexp(4.5, 2.5, FALSE)
[1] 3.251824e-05
Looking here I can see that they use the definition:
double R::dexp(double x, double sl, int lg)
but I haven't been able to find out what sl
stands for. I'm not sure if this is documented anywhere - so hopefully this post stands as a warning to others who have used the function like me, and also if anyone can help as to what parameterisation has been used, and why.
回答1:
If you look at the function definition for dexp
,
R> dexp
function (x, rate = 1, log = FALSE)
.Call(C_dexp, x, 1/rate, log)
you'll see that dexp
calls the C function C_dexp
with parameter 1/rate
. This is what R::dexp
is mirroring. In Rcp, they always use the same parameterisation as R itself does at the C level which may be different than the R level.
That means
R> my_dexp(4.5, 1/2.5, FALSE) - dexp(4.5, 2.5, FALSE)
[1] 0
If you look at the Wikipedia page on the exponential function, you'll see the alternative parameterisation based on the reciprocal of the rate parameter, lambda. In this parameterisation, the parameter beta=1/lambda
takes the role of a survival parameter. So the expected duration of survival of the system is beta
units of time.
来源:https://stackoverflow.com/questions/32210788/parameterisation-of-rdexp