How to wrap `VGAM::cumulative` into a helper function (`object not found` issue)?

32 Views Asked by At

When I execute the following code for testing proportional odds assumption in the ordered logit model it works fine:

data = transform(VGAM::pneumo, let=log(exposure.time))
formula = cbind(normal, mild, severe) ~ let

fit_po = VGAM::vglm(formula, family=VGAM::cumulative(parallel=TRUE, link='logitlink'), data=data)
fit_h0 = VGAM::vglm(formula, family=VGAM::cumulative(parallel=FALSE, link='logitlink'), data=data)
VGAM::lrtest(fit_po, fit_h0)

However, when I try to wrap it into a helper function log_lr_po_assumption:

log_lr_po_assumption = function(formula, data, method='logitlink') {
    fit_po = VGAM::vglm(formula, family=VGAM::cumulative(parallel=TRUE, link=method), data=data)
    fit_h0 = VGAM::vglm(formula, family=VGAM::cumulative(parallel=FALSE, link=method), data=data)
    VGAM::lrtest(fit_po, fit_h0)
}
log_lr_po_assumption(formula, data)

it fails with:

Error in get(fun.name) : object 'method' not found

Why is it so? How can I create a convenience wrapper which will work?

1

There are 1 best solutions below

0
On

One way to solve it is to force eager evaluation using do.call (with the default quote=FALSE):

log_lr_po_assumption = function(formula, data, method='logitlink') {
    po_family = do.call(VGAM::cumulative, list(parallel=TRUE, link=method))
    np_family = do.call(VGAM::cumulative, list(parallel=FALSE, link=method))

    fit_po = VGAM::vglm(formula, family=po_family, data=data)
    fit_h0 = VGAM::vglm(formula, family=np_family, data=data)
    VGAM::lrtest(fit_po, fit_h0)
}

Though I believe that an easier solution should exist...