Skip to content

Commit

Permalink
Merge pull request #45 from richfitz/cran-fixes
Browse files Browse the repository at this point in the history
Bump version
  • Loading branch information
richfitz authored Oct 2, 2024
2 parents 5f5c131 + 9f9dda1 commit 1687f05
Show file tree
Hide file tree
Showing 15 changed files with 158 additions and 160 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: diversitree
Version: 0.10-0
Version: 0.10-1
Title: Comparative 'Phylogenetic' Analyses of Diversification
Authors@R: c(person("Richard G.", "FitzJohn", role = c("aut", "cre"),
email = "[email protected]"),
Expand Down
28 changes: 14 additions & 14 deletions man/find.mle.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,17 @@ find.mle(func, x.init, method, ...)
Different \code{method} arguments take different arguments passed
through \code{...} to control their behaviour:

\code{method="optim"}: Uses \R's \code{\link{optim}} function for the
\code{method="optim"}: Uses \R's \code{optim} function for the
optimisation. This allows access to a variety of general purpose
optimisation algorithms. The method \emph{within} \code{optim} can be
chosen via the argument \code{optim.method}, which is set to
"L-BFGS-B" by default (box constrained quasi-Newton optimisation).
This should be suitable for most uses. See the \code{method} argument
of \code{\link{optim}} for other possibilities. If \code{"L-BFGS-B"}
of \code{optim} for other possibilities. If \code{"L-BFGS-B"}
is used, then upper and lower bounds may be specified by the arguments
\code{lower} and \code{upper}. The argument \code{control} can be
used to specify other control parameters for the algorithms - see
\code{\link{optim}} for details. Most of the \code{optim} algorithms
\code{optim} for details. Most of the \code{optim} algorithms
require finite values be returned at every evaluated point. This is
often not possible (extreme values of parameters or particular
combinations may have zero likelihood and therefore -Inf
Expand All @@ -99,13 +99,13 @@ find.mle(func, x.init, method, ...)
approximation of derivatives and seems to find the global optimum more
reliably (though often less precisely). Additional arguments are
\code{control} to control aspects of the search (see
\code{\link{subplex}} for details). The argument \code{fail.value}
\code{subplex} for details). The argument \code{fail.value}
can be used as in \code{method="optim"}, but by default \code{-Inf}
will be used on failure to evaluate, which is generally appropriate.
\code{method="nlminb"}: Uses the function \code{\link{nlminb}} for
\code{method="nlminb"}: Uses the function \code{nlminb} for
optimisation, so that optimising a Mk2/Mkn likelihood function behaves
as similarly as possible to \code{ape}'s \code{\link{ace}} function.
as similarly as possible to \code{ape}'s \code{ace} function.
As for \code{method="optim"}, lower and upper bounds on parameters may
be specified via \code{lower} and \code{upper}. \code{fail.value} can
be used to control behaviour on evaluation failure, but like
Expand All @@ -114,24 +114,24 @@ find.mle(func, x.init, method, ...)
- see \code{link{nlminb} for details}. This function is not generally
recommended for use.

\code{method="nlm"}: Uses the function \code{\link{nlm}} for
\code{method="nlm"}: Uses the function \code{nlm} for
optimisation, so that optimising a birth-death likelihood function
behaves as similarly as possible to \code{ape}'s
\code{\link{birthdeath}} function. Takes the same additional
\code{birthdeath} function. Takes the same additional
arguments as \code{method="nlminb"} (except that \code{fail.value}
behaves as for \code{method="optim"}). Like \code{method="nlminb"},
this is not recommended for general use.
\code{code} and \code{logLik} methods exist for \code{fit.mle} objects
so that parameters and log-likelihoods may be extracted. This also
allows use with \code{\link{AIC}}.
allows use with \code{AIC}.
Simple model comparison by way of likelihood ratio tests can be
performed with \code{\link{anova}}. See Examples for usage.
performed with \code{anova}. See Examples for usage.
}
\section{Model comparison}{
The \code{\link{anova}} function carries out likelihood ratio tests.
The \code{anova} function carries out likelihood ratio tests.
There are a few possible configurations.
First, the first fit provided could be the focal fit, and all other
Expand All @@ -146,7 +146,7 @@ find.mle(func, x.init, method, ...)
strictly decreasing in parameters (D nested in C, C nested in B, ...).
In both cases, nestedness is checked. First, the "class" of the
fitted object must match. Second, the \code{\link{argnames}} of the
fitted object must match. Second, the \code{argnames} of the
likelihood function of a sub model must all appear in the
\code{argnames} of the parent model. There are some cases where this
second condition may not be satisfied and yet the comparison is valid
Expand Down Expand Up @@ -202,14 +202,14 @@ coef(fit) # Named vector of six parameters
logLik(fit) # -659.93
AIC(fit) # 1331.86
## find.mle works with constrained models (see \link{constrain}). Here
## find.mle works with constrained models (see constrain). Here
## the two speciation rates are constrained to be the same as each
## other.
lik.l <- constrain(lik, lambda0 ~ lambda1)
fit.l <- find.mle(lik.l, pars[-2])
logLik(fit.l) # 663.41
## Compare the models with \link{anova} - this shows that the more
## Compare the models with anova - this shows that the more
## complicated model with two separate speciation rates fits
## significantly better than the simpler model with equal rates
## (p=0.008).
Expand Down
2 changes: 1 addition & 1 deletion man/make.mkn.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ fit.mkn[1:2]
## These are the same (except for the naming of arguments)
all.equal(fit.mkn[-7], fit.mk2[-7], check.attr=FALSE, tolerance=1e-7)
## Equivalence to ape's \link{ace} function:
## Equivalence to ape's ace function:
model <- matrix(c(0, 2, 1, 0), 2)
fit.ape <- ace(phy$tip.state, phy, "discrete", model=model, ip=p)

Expand Down
2 changes: 1 addition & 1 deletion src/GslOdeR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ SEXP GslOdeR::target(double t, SEXP y) {

void GslOdeR::derivs(double t, const double y[], double dydt[]) {
// It is possible that we could allocate the space at construction
// (though I think that allocVector could lead to eventual garbage
// (though I think that Rf_allocVector could lead to eventual garbage
// collection as we can't protect it). Alternatively,
// Rcpp::NumericVector followed by Rcpp::wrap might be less ugly.
// That would require a change in target() to return
Expand Down
8 changes: 4 additions & 4 deletions src/TimeMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ TimeMachine::TimeMachine(std::vector<std::string> names,

void TimeMachine::set(std::vector<double> pars) {
if (pars.size() != np_in)
error("Expected %d parameters, recieved %d", (int)np_in, (int)pars.size());
Rf_error("Expected %d parameters, recieved %d", (int)np_in, (int)pars.size());
// Only go through the extra effort below if the parameters differ.
if ( pars == p_in )
return;
Expand Down Expand Up @@ -178,9 +178,9 @@ TimeMachineFunction::TimeMachineFunction(std::string name_,
f = &tm_fun_spline;
np = 2;
if ( spline == NULL )
error("Should not be able to get here!");
Rf_error("Should not be able to get here!");
} else {
error("Unknown function type %s", func_name.c_str());
Rf_error("Unknown function type %s", func_name.c_str());
}

p_in.resize(np);
Expand All @@ -201,7 +201,7 @@ double TimeMachineFunction::check_ok(double x) {
if ( truncate )
x = 0;
else if ( nonnegative )
error("Value of %s (%s) must be nonnegative",
Rf_error("Value of %s (%s) must be nonnegative",
variable_name.c_str(), func_name.c_str());
}
return x;
Expand Down
7 changes: 2 additions & 5 deletions src/asr-joint.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#include <R.h>
#include <Rinternals.h>

/* Ripped from the R code; I can probably use this to speed things up
later? - If I drop the permutations or save them in the main
section this will be a bit faster... */
int ProbSampleOne_tmp(int n, double *p, int *perm) {
double rU, tot = 0;
int i, j;
Expand Down Expand Up @@ -43,7 +40,7 @@ SEXP r_sample(SEXP r_root_p) {
ret = ProbSampleOne_tmp(k, pr, perm);
PutRNGstate();

return ScalarInteger(ret);
return Rf_ScalarInteger(ret);
}


Expand All @@ -66,7 +63,7 @@ SEXP r_do_asr_joint(SEXP r_k, SEXP r_order, SEXP r_parent,
int idx, i, j, l;

GetRNGstate();
PROTECT(ret = allocVector(INTSXP, len));
PROTECT(ret = Rf_allocVector(INTSXP, len));
states = INTEGER(ret);

/* Sample root */
Expand Down
64 changes: 32 additions & 32 deletions src/continuous.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ SEXP r_make_dt_obj_cont(SEXP cache, SEXP r_ic, SEXP r_br) {
dt_obj_cont *obj;
SEXP extPtr;

obj = (dt_obj_cont *)Calloc(1, dt_obj_cont);
obj = (dt_obj_cont *)R_Calloc(1, dt_obj_cont);
obj->neq = neq;
obj->n_out = LENGTH(getListElement(cache, "len"));
obj->np = np;
Expand All @@ -29,9 +29,9 @@ SEXP r_make_dt_obj_cont(SEXP cache, SEXP r_ic, SEXP r_br) {
obj->br = br;

/* Set up storage */
obj->init = (double *)Calloc(obj->n_out * neq, double);
obj->base = (double *)Calloc(obj->n_out * neq, double);
obj->lq = (double *)Calloc(obj->n_out, double);
obj->init = (double *)R_Calloc(obj->n_out * neq, double);
obj->base = (double *)R_Calloc(obj->n_out * neq, double);
obj->lq = (double *)R_Calloc(obj->n_out, double);

/* Set up tips and internal branches */
dt_cont_setup_tips(obj, cache);
Expand All @@ -49,22 +49,22 @@ SEXP r_make_dt_obj_cont(SEXP cache, SEXP r_ic, SEXP r_br) {
static void dt_obj_cont_finalize(SEXP extPtr) {
dt_obj_cont *obj = (dt_obj_cont*)R_ExternalPtrAddr(extPtr);

Free(obj->init);
Free(obj->base);
Free(obj->lq);
R_Free(obj->init);
R_Free(obj->base);
R_Free(obj->lq);

/* tips */
Free(obj->tip_y);
Free(obj->tip_len);
Free(obj->tip_target);
R_Free(obj->tip_y);
R_Free(obj->tip_len);
R_Free(obj->tip_target);

/* internals */
Free(obj->order);
Free(obj->children);
Free(obj->len);
Free(obj->depth);
R_Free(obj->order);
R_Free(obj->children);
R_Free(obj->len);
R_Free(obj->depth);

Free(obj);
R_Free(obj);
}

void dt_cont_setup_tips(dt_obj_cont *obj, SEXP cache) {
Expand All @@ -78,10 +78,10 @@ void dt_cont_setup_tips(dt_obj_cont *obj, SEXP cache) {
tip_target = INTEGER(tip_target_r);

n_tip = obj->n_tip = LENGTH(tip_target_r);
if ( nrows(tip_y) != neq || ncols(tip_y) != n_tip )
error("Incorrect tip state dimensions");
if ( Rf_nrows(tip_y) != neq || Rf_ncols(tip_y) != n_tip )
Rf_error("Incorrect tip state dimensions");

obj->tip_target = (int *)Calloc(n_tip, int);
obj->tip_target = (int *)R_Calloc(n_tip, int);
memcpy(obj->tip_target, tip_target, n_tip*sizeof(int));

for ( i = 0; i < n_tip; i++ ) {
Expand All @@ -104,10 +104,10 @@ void dt_cont_setup_internal(dt_obj_cont *obj, SEXP cache) {
n_out = obj->n_out;
n_int = obj->n_int = LENGTH(order) - 1;

obj->order = (int *)Calloc(n_int, int);
obj->children = (int *)Calloc(n_out*2, int);
obj->len = (double *)Calloc(n_out, double);
obj->depth = (double *)Calloc(n_out, double);
obj->order = (int *)R_Calloc(n_int, int);
obj->children = (int *)R_Calloc(n_out*2, int);
obj->len = (double *)R_Calloc(n_out, double);
obj->depth = (double *)R_Calloc(n_out, double);

memcpy(obj->order, INTEGER(order), n_int*sizeof(int));
memcpy(obj->children, INTEGER(children), 2*n_out*sizeof(int));
Expand All @@ -130,7 +130,7 @@ SEXP r_all_branches_cont(SEXP extPtr, SEXP r_pars) {
SEXP ret, ret_vals;

if ( obj == NULL )
error("Corrupt pointer (are you using multicore?)");
Rf_error("Corrupt pointer (are you using multicore?)");

ic = obj->ic;
br = obj->br;
Expand All @@ -148,7 +148,7 @@ SEXP r_all_branches_cont(SEXP extPtr, SEXP r_pars) {
lq = obj->lq;

if ( LENGTH(r_pars) != obj->np )
error("Incorrect length parameters. Expected %d, got %d",
Rf_error("Incorrect length parameters. Expected %d, got %d",
obj->np, LENGTH(r_pars));

for ( i = 0; i < n_tip; i++ ) {
Expand Down Expand Up @@ -176,9 +176,9 @@ SEXP r_all_branches_cont(SEXP extPtr, SEXP r_pars) {
for ( i = 0; i < obj->n_out; i++ )
tot += lq[i];

PROTECT(ret = allocVector(VECSXP, 2));
PROTECT(ret_vals = allocVector(REALSXP, neq));
SET_VECTOR_ELT(ret, 0, ScalarReal(tot));
PROTECT(ret = Rf_allocVector(VECSXP, 2));
PROTECT(ret_vals = Rf_allocVector(REALSXP, neq));
SET_VECTOR_ELT(ret, 0, Rf_ScalarReal(tot));
SET_VECTOR_ELT(ret, 1, ret_vals);
memcpy(REAL(ret_vals), init + obj->root * neq, neq*sizeof(double));
UNPROTECT(2);
Expand All @@ -193,11 +193,11 @@ SEXP r_get_vals_cont(SEXP extPtr) {
int n_out = obj->n_out, neq = obj->neq;
int i, idx;

PROTECT(ret = allocVector(VECSXP, 3));
PROTECT(r_init = allocMatrix(REALSXP, neq, n_out));
PROTECT(r_base = allocMatrix(REALSXP, neq, n_out));
PROTECT(ret = Rf_allocVector(VECSXP, 3));
PROTECT(r_init = Rf_allocMatrix(REALSXP, neq, n_out));
PROTECT(r_base = Rf_allocMatrix(REALSXP, neq, n_out));

PROTECT(lq = allocVector(REALSXP, n_out));
PROTECT(lq = Rf_allocVector(REALSXP, n_out));
SET_VECTOR_ELT(ret, 0, r_init);
SET_VECTOR_ELT(ret, 1, r_base);
SET_VECTOR_ELT(ret, 2, lq);
Expand Down Expand Up @@ -325,7 +325,7 @@ SEXP r_dt_cont_reset_tips(SEXP extPtr, SEXP tip_y) {
double *y = REAL(tip_y);
int i, idx, neq = obj->neq, n_tip = obj->n_tip;
if (LENGTH(tip_y) != neq * n_tip)
error("Wrong length tip_y - expected %d, got %d",
Rf_error("Wrong length tip_y - expected %d, got %d",
neq * n_tip, LENGTH(tip_y));

for (i = 0; i < n_tip; i++) {
Expand Down
3 changes: 2 additions & 1 deletion src/hdr.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <R.h>
#include <Rinternals.h>
#include <float.h> /* for DBL_EPSILON */

#include "util-splines.h"

Expand All @@ -21,7 +22,7 @@ SEXP r_hdr(SEXP x, SEXP y, SEXP alpha) {
dat->w = 1-a;

xl = RSRC_Brent_fmin(0, a, fn, (void *)dat, tol);
PROTECT(ret = allocVector(REALSXP, 2));
PROTECT(ret = Rf_allocVector(REALSXP, 2));
REAL(ret)[0] = dt_spline_eval1(dat->spline, xl);
REAL(ret)[1] = dt_spline_eval1(dat->spline, xl+dat->w);
UNPROTECT(1);
Expand Down
18 changes: 9 additions & 9 deletions src/mkn-pij.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ SEXP r_asr_marginal_mkn(SEXP r_k, SEXP r_pars, SEXP r_nodes,
int idx, i, j, k;
double *vals;

if ( !isFunction(root_f) )
error("root_f must be a function");
if ( !isEnvironment(rho) )
error("rho must be a function");
if ( !Rf_isFunction(root_f) )
Rf_error("root_f must be a function");
if ( !Rf_isEnvironment(rho) )
Rf_error("rho must be a function");

PROTECT(ret = allocMatrix(REALSXP, n_states, n_nodes));
PROTECT(cpy_root_vals = allocVector(REALSXP, neq));
PROTECT(cpy_lq = allocVector(REALSXP, n_out));
PROTECT(ret = Rf_allocMatrix(REALSXP, n_states, n_nodes));
PROTECT(cpy_root_vals = Rf_allocVector(REALSXP, neq));
PROTECT(cpy_lq = Rf_allocVector(REALSXP, n_out));

for ( i = 0; i < n_nodes; i++ ) {
idx = nodes[i];
Expand All @@ -148,8 +148,8 @@ SEXP r_asr_marginal_mkn(SEXP r_k, SEXP r_pars, SEXP r_nodes,

memcpy(REAL(cpy_root_vals), root_vals, neq * sizeof(double));
memcpy(REAL(cpy_lq), lq, n_out * sizeof(double));
PROTECT(R_fcall = lang4(root_f, r_pars, cpy_root_vals, cpy_lq));
PROTECT(tmp = eval(R_fcall, rho));
PROTECT(R_fcall = Rf_lang4(root_f, r_pars, cpy_root_vals, cpy_lq));
PROTECT(tmp = Rf_eval(R_fcall, rho));
vals[j] = REAL(tmp)[0];
UNPROTECT(2);
}
Expand Down
Loading

0 comments on commit 1687f05

Please sign in to comment.