[R] g parameter for deltaMethod() as a function
Fox, John
jfox at mcmaster.ca
Mon Jan 30 20:03:31 CET 2017
Dear Marc,
A "wrapper function" *calls* deltaMethod(). Your function try_g() is intended to be *called by* try_g(). Actually, you just pass to deltaMethod() a character string that would evaluate to the function call, and deltaMethod() doesn't know what to do with that.
The description of the g (second) argument in ?deltaMethod seems perfectly clear to me; beyond what I quoted, in "Details" you'll find, "The argument g must be a quoted character string that gives the function [of the coefficients] of interest. For example, if you set m2 <- lm(Y ~ X1 + X2 + X1:X2), then deltaMethod(m2,"X1/X2") applies the delta method to the ratio of the coefficient estimates for X1 and X2. The argument g can consist of constants and names associated with the elements of the vector of coefficient estimates. Etc."
Try writing a function that calls deltaMethod() and does what you want.
John
> -----Original Message-----
> From: Marc Girondot [mailto:marc_grt at yahoo.fr]
> Sent: Monday, January 30, 2017 1:31 PM
> To: Fox, John <jfox at mcmaster.ca>
> Cc: r-help at r-project.org
> Subject: Re: [R] g parameter for deltaMethod() as a function
>
> Le 30/01/2017 à 19:04, Fox, John a écrit :
>
>
> Hi everyone,
>
> I try to use the Default S3 method DeltaMethod() from car
> package, but I
> have some problems when I try to use a function as the "g"
> parameter. I
> don't know if it is possible anyway. I hope that you could
> tell me:
>
> I don't see how that would work. From ?deltaMethod: "g [the second
> argument]: A quoted string that is the function of the parameter
> estimates to be evaluated; see the details below."
>
> A possible solution would be to write a wrapper function that
> prepares a proper call to deltaMethod().
>
> Hi John,
>
> This is exactly what I try to do: a wrapper (I forget that name in
> English !).
>
> I have made some progress to do a wrapper function:
>
> try_g <- function(...) {
> par <- list(...)
> return(par$t1/par$t2)
> }
>
> try_g(t1=1, t2=2)
> deltaMethod(coef(m1), "try_g(t1, t2)", vcov.=vcov(m1))
>
> The wrapper function try_g is accepted now, but I get an error because
> deltaMethod() tried to do symbolic derivative:
>
> > deltaMethod(coef(m1), "try_g(t1, t2)", vcov.=vcov(m1))
> Error in D(g, names(para)[i]) :
> La fonction 'try_g' n'est pas dans la table des dérivées (translation:
> The function 'try_g' is not in the table of derivative functions).
>
> I was hopping that numeric approximation of derivative (example
> numDeriv::grad() or deriv() ) could be used, but it is not the case.
>
> Thanks
>
> Marc
>
More information about the R-help
mailing list