set.defaults.Rd
Set the default values of formal arguments of a function.
set.defaults(f, ..., defaults)
The repetitive argument lists of many of diversitree's likelihood functions are the motivation for this function.
For example, the likelihood function that make.bisse
produces
takes arguments condition.surv
, root
, and root.p
,
each with default values. If you dislike the defaults, you can change
them by passing in alternative values when computing likelihoods, or
when doing an ML search. However, this can get tedious if you are
using a function a lot, and your code will get cluttered with lots of
statements like condition.surv=FALSE
, some of which you may
forget. See the example below for how to avoid this.
## Due to a change in sample() behaviour in newer R it is necessary to
## use an older algorithm to replicate the previous examples
if (getRversion() >= "3.6.0") {
RNGkind(sample.kind = "Rounding")
}
#> Warning: non-uniform 'Rounding' sampler used
pars <- c(0.1, 0.2, 0.03, 0.03, 0.01, 0.01)
set.seed(4)
phy <- tree.bisse(pars, max.t=30, x0=0)
lik <- make.bisse(phy, phy$tip.state)
## default arguments:
args(lik)
#> function (pars, condition.surv = TRUE, root = ROOT.OBS, root.p = NULL,
#> intermediates = FALSE)
#> NULL
lik.no.cond <- set.defaults(lik, condition.surv=FALSE)
args(lik.no.cond)
#> function (pars, condition.surv = FALSE, root = ROOT.OBS, root.p = NULL,
#> intermediates = FALSE)
#> NULL
## Multiple arguments at once:
lik2 <- set.defaults(lik, root=ROOT.GIVEN, root.p=c(0, 1))
args(lik2)
#> function (pars, condition.surv = TRUE, root = ROOT.GIVEN, root.p = c(0,
#> 1), intermediates = FALSE)
#> NULL
## Equivalently (using alist, not list -- see ?alist)
defaults <- alist(root=ROOT.GIVEN, root.p=c(0, 1))
lik3 <- set.defaults(lik, defaults=defaults)
identical(lik2, lik3)
#> [1] TRUE