The default rank of the association matrix, bioticStruct(df = NULL), is
max(5, min(floor(J/2), floor(n/10))) for J species and n sites; it used to be
max(5, floor(J/2)). Fits change only where J/2 > n/10, i.e. with more species than a
fifth of the sites. There the old default gave the model more latent dimensions than the
sites can identify, and the associations were overestimated: with 500 sites and 5000
species (df = 2500) the slope of estimated on true covariances was 36 in simulations, and
0.89 with df = 100. sjSDM_cv() resolves the default on the full data, so every fold
fits the same rank. See the details of bioticStruct().
NN() is new. It marks a group of covariates inside an environmental formula as a
non-linear block, so that a flexible term can sit beside interpretable linear terms
rather than replacing them: linear(X, ~ temp + NN(soil + ndvi, hidden = c(50, 50))).
The blocks are summed on the linear-predictor scale, so coef(), summary() and the
standard errors still describe the parametric part; the standard errors are conditional
on the fitted block and a warning says so. A covariate may not appear both in the
parametric part and in an NN() block, NN() may not appear inside an interaction, and
it is not yet supported in the spatial formula. update() gains env_blocks to refit
with a subset of the blocks, which is how a block is switched off.
Grouping-factor random effects are new. lme4 bars are written directly in the
environmental formula and carry lme4's own semantics, through the 'reformulas' package:
linear(X, ~ temp + (1 | plot)), (temp | plot) for a correlated intercept and slope,
(0 + temp | plot), (temp || plot) and (1 | plot/subplot). re() is the explicit
form where a bar needs arguments, re(1 | plot, df = 2).
A bar is a design-level latent factor, not a scalar shift: the group effect carries its
own species-species covariance Lambda Lambda' of rank df, so species may respond to a
plot with different magnitude and sign. df defaults to max(q, 5), where q is the
number of columns of the bar, and must not be smaller than q — at df < q the
intercept-slope correlation would be forced to exactly +-1. loading = "shared" gives
the classical mixed-model case instead, one shift per group identical for every species.
The bars are integrated out by a stochastic variational approximation: each group gets a
Gaussian posterior drawn with the reparameterisation trick, and the Kullback-Leibler term
to the N(0, I) prior supplies the shrinkage. The Monte-Carlo likelihood itself is
unchanged. The variational parameters are optimised without weight decay, so groups that
are absent from a minibatch are shrunk only by the prior, not by the optimiser.
getRE() returns, per bar and named by the grouping variable, the group covariance, the
predicted group effects, the loadings and the posterior Cholesky factor. getCov() and getCor() gain
which = c("residual", "re"); "re" returns one entry per bar, named by the grouping
variable.
anova() of a model with random effects is conditional on the random intercepts: the null
model and every sub-model contain the random intercept of each grouping factor, random
slopes belong to the abiotic component, every model is scored on its marginal likelihood
with the group effects integrated out by importance sampling, and the grouping
component reports what the random intercepts alone explain.
Known limitations of the random effects. logLik() for such a model is the conditional
value at the posterior means, so it is not comparable with a model fitted without a bar.
sjSDM_cv() does not support bars or NN() blocks and raises an error rather than
dropping them. Bars are not available in the spatial formula. The reported group variance
grows with df, because the Kullback-Leibler term constrains the posterior and not the
loadings; report the rank alongside the variance, and prefer a small df.
The default optimizer is Adam() instead of RMSprop(weight_decay = 1e-4). With
random-effect bars, RMSprop shrank the estimated group standard deviation by an amount
that grows with the share of groups missing from a minibatch -- about 8 % below the exact
maximum-likelihood value at a batch of a quarter of the number of groups, against 1-2 % with
the full batch -- and switching the starting point between batch sizes showed the fit settles
at the level of the batch size it is trained with. Adam removes the dependence. Every fit
changes; pass sjSDMControl(optimizer = RMSprop()) to reproduce earlier results.
Conditional predictions, predict(model, Y = ), were rewritten. NA cells of Y are
predicted from the observed cells of the same site through the latent factors: the latent
factors are drawn from a Laplace approximation of their conditional distribution and
weighted by the likelihood of the observed species, and all NA cells of a site are predicted
in one pass, whatever the pattern of missing values. Binomial models give the conditional
probability of occurrence, poisson and negative binomial models the conditional expected
abundance, gaussian models the conditional mean. predict(loo = TRUE) with a complete Y
predicts every species from all the others in one pass. The effective sample size of the
weights is returned per site in attr(, "ess"). The earlier estimator divided two
independently simulated likelihoods: in simulations with 100 or more species it was worse
than the unconditional prediction (AUC 0.53 against 0.75 at 1000 species, 43 % of the
predictions exactly 0 or 1), and for counts it returned the probability of exactly one
individual. Sites are processed in batches, so memory no longer grows with the number of
sites. Conditional predictions change.
anova() gains proposal, and the default is "laplace": the latent factors are drawn
from a Gaussian fitted to the per-site mode of the integrand and each draw is reweighted by
log p(z) - log q(z). "prior" draws them from N(0, I), which is what every earlier
version did. At many species almost every prior draw lands where the observed community is
implausible -- at 100 species the effective sample size is 3 draws out of 1000 -- and the
resulting bias does not cancel between the sub-models the fractions are differenced from.
Variation partitioning results change accordingly. The realised effective sample size
is returned per sub-model in out$ess, and attr(out$ess, "newton_decrement") carries the
largest Newton decrement the mode-finder left behind; a warning is raised above 1e-2.
anova() no longer fits a saturated model. The saturated log-likelihood is available in
closed form for all six links and is now computed, which removes one fit per call and
makes the Saturated row exact rather than an optimiser's best effort.
anova() takes all leave-one-out species likelihoods from a single forward pass, and
the joint and the leave-one-out likelihoods now share one set of Monte-Carlo draws.
The association-only sub-model of anova() is now fitted with an intercept. It used to
be fitted with ~0, which pins the linear predictor at exactly zero, so under a probit
link every species was held at a marginal prevalence of 0.5 and the covariance cannot
compensate -- it shifts no mean. The deficit was not a property of the associations but
of the missing intercept, and it was differenced against a null model that does have one.
It landed in the unique environment fraction of a non-spatial model and in the shared
abiotic-spatial fraction of a spatial one, with the opposite sign in the three-way shared
fraction. Spatial and non-spatial variation partitioning results change accordingly.
The sub-models of anova() without associations, and its null model, now carry no latent
term at all. They used to be fitted with an identity covariance. Under a probit link that
only rescales the coefficients, but for the count, negative binomial and gaussian links it
adds a fixed unit latent variance on the link scale, which the full model does not contain.
The misfit was reported as associations -- on simulated data with independent species,
228, 34 and 587 log-likelihood units for the three links -- and the null model was scored
with a likelihood it had not been fitted under, which inflated Rsquared().
Variation partitioning and R-squared results change for these three families; probit
results move within the Monte-Carlo error. For poisson() the Associations fraction
now includes independent log-normal overdispersion, which only the full model's latent term
can absorb: on simulated independent species it was 0.002, 0.02 and 0.15 (McFadden) for a
log-normal standard deviation of 0, 0.5 and 1, against 0.002, 0.004 and 0.008 with
family = "nbinom". Use the negative binomial family for overdispersed counts; see the details
of anova(). anova() now refuses a model fitted with
bioticStruct(diag = TRUE), whose association sub-models would not be nested in it.
The sub-models and the null model of anova() now inherit the fitted model's
bioticStruct() settings. They used to be refitted with a fresh bioticStruct(), which
silently dropped df, lambda and alpha, so a model tuned with sjSDM_cv() was
differenced against unpenalised sub-models of a different latent rank.
anova() for a spatial model used to leave the spatial term in the null model, because
the class test matched "spatial " with a trailing space. Every spatial variation
partitioning computed before this fix used a null that already contained space.
Spatial results change.
The backend was rewritten. sjSDM now runs on the R 'torch' package (libtorch) and no
longer depends on 'python', 'conda', 'reticulate' or 'PyTorch'. install_sjSDM() is
now a thin wrapper around torch::install_torch(); the 'r-sjsdm' conda environment is
no longer used and can be deleted.
Results are not bit-identical to the 'PyTorch' backend. The two backends use different
random number streams, so weight initialisation and the Monte-Carlo draws differ. With
the old optimizer (sjSDMControl(optimizer = RMSprop())), coefficients, associations
and log-likelihoods agreed with 1.0.7 within Monte-Carlo noise across all families,
spatial and DNN models.
install_sjSDM() no longer takes the version, conda or restart_session arguments; whether the GPU is used is
decided by the 'torch' installation. device = "mps" now works on Apple silicon.
sjSDMControl(mixed = TRUE) (half precision) is accepted but ignored.
model$history is a data.frame with columns epoch and train_l, not a numeric
vector, and it holds only the epochs that ran. An early-stopped fit used to leave the
remaining epochs as zeros, which plot() drew as part of the loss curve. Code that
indexed model$history as a vector has to read model$history$train_l.
Adam(), AdamW() and Adagrad() are new and are the recommended optimizers.
Adamax(), AdaBound() and madgrad() have no counterpart in 'torch',
redirect to Adam() with a message, and AccSGD() redirects to SGD(). They are kept
only because they are part of the 1.0.7 API and will be removed in 1.2.0.
A GPU request is now validated before anything is allocated. device = 0L, "gpu",
"cuda" and "cuda:0" all fall back to the CPU with a warning when no CUDA device is
present, and an index larger than the number of installed devices is an error instead of
a backend failure inside libtorch. The check sits in the one place every model rebuild
goes through, so it also covers anova(), sjSDM_cv() and a reloaded model.
summary() of an anova() object defaults to fractions = "mvp_proportional" (was
"all"), and internalStructure() to "mvp_proportional" (was "discard"). "mvp" and
"mvp_proportional" are new: Abiotic and Spatial are split within the model without
associations and the associations receive the remainder of the full model. Without a spatial
component the printed tables equal "proportional", while the per-species and per-site
components give Abiotic the environment-only model and the associations the remainder.
summary(fractions = "proportional") and "equal" allocate the shared fractions on the
community totals instead of per species, and in a spatial model the three-way fraction is
first split onto the pairwise fractions and then onto the single ones. Results change.
The printed row label "Assocations" in anova() tables is now "Associations"; code that
indexes the tables by the old name has to be updated.
summary() of a fitted model prints the random-effect blocks and the architecture of the
NN() blocks.
update(), and through it every anova() sub-model and null model, reuses the seed of the
fitted model; update(seed = ) sets another one.
Models fitted and saved with sjSDM <= 1.0.7 cannot be restored by the 'torch' backend and have to be refitted.
'torch' and 'reformulas' are new dependencies; 'reticulate' was removed.
plot() of an anova() object keeps "discard" as its default.
anova() takes mc_batch through ..., the number of sites whose Monte-Carlo draws are
held in memory at once.
predict(model, Y = ) without newdata ignored Y and returned the unconditional
prediction; conditional predictions on the training sites now condition on Y.sjSDM() failed with "missing value where TRUE/FALSE needed" whenever Y contained NA;
missing responses are now masked in the likelihood as documented.anova() (and internalStructure() with
"proportional" or "equal") were computed from the fractions instead of the sub-model R
squared, and were empty for non-spatial models. Values change.sjSDM_cv() with n_cores and blocks > 1 attached results to the wrong
hyperparameters: the per-block results were combined with rbind() into a list matrix
that was then read column-wise. summary(), plot() and sjSDM.tune() of such a run
reported the performance of one tuning step under the settings of another.sjSDM_cv() scored a species that had only one class in a fold as AUC 0.5, which pulled
the mean towards chance for rare species; such species are now left out of the mean, and a
missing value in Y no longer makes the macro AUC NA. The AUC_macro_train column held
the test value. n_gpu without n_cores now stops with a message instead of failing, and
plot() of a sjSDM_cv() object drew some tuning points against the wrong axes.simulate() drew every species independently around predict(), so the simulated
communities carried none of the fitted associations, and it failed for the gaussian
family. It now draws the latent factors first and the response from the family, for
binomial, poisson, negative binomial and gaussian models, and always returns an array of
dimension (nsim, sites, species).residuals() returned the response minus the linear predictor; it now returns the
response minus the fitted marginal expectation on the response scale.logLik(), and through it anova() and Rsquared(), as well as the standard errors
evaluated a model reloaded with readRDS() with dropout switched on.simulate_SDM(link = "logit") produced exactly the probit data; it now draws the
presence-absence response from the logistic probabilities. link = "probit" with
response = "count" returned presence-absence data, and a link without a matching response
returned no response at all; both now follow response or stop with a message.print() of an internalStructure() object printed nothing.library(sjSDM) only, not whenever the namespace is
loaded by sjSDM:: or by another package.anova() had the opposite sign
to every other fraction, so the "all" table did not add up to the full model. It now
follows the classical convention: a negative shared fraction means the components explain
more together than the sum of what each explains alone. The shared row, the Venn plot
and the per-species and per-site shared values change sign.summary(fractions = "equal") of a spatial anova() split the pairwise shared fractions
in thirds instead of halves and gave the spatial component the abiotic-associations share
instead of the abiotic-spatial one.Deviance and Residual_deviance of anova() were on a scale of -1/2
relative to the printed table.anova() and Rsquared() returned NA for every value when the response contained a
missing value, because the null log-likelihood was not masked.sjSDM(), anova() and simulate() set the global random seed and left it set, so
data simulated after a fit inside a loop repeated across iterations. The caller's random
number stream is now restored.sjSDM() now stops when Y and the design have different numbers of rows. Rows with a
missing predictor are dropped from the design by model.frame() while Y kept them, so
every later design row was paired with a different site, without a warning.predict(newdata = ) keeps rows with a missing predictor and returns NA for them, as
predict.lm() does, instead of dropping them, which made the rows of the prediction
disagree with the rows of newdata.linear(), DNN() and update() were evaluated in the package's own
frame. A name in the formula could resolve to a variable of that frame (I(x * alpha)
read the function's alpha), and a formula passed as a variable from inside a user
function was not found. Formulas now keep the environment
they were written in.getWeights() returns the full model state in state, and setWeights() restores it.
The three positional parts do not include the negative binomial dispersion, so a round
trip silently left it unchanged.predict(dropout = TRUE) was ignored for a non-spatial model when newdata was not
given.anova() and of Rsquared() was evaluated with the
dispersion of the full model instead of its own fitted dispersion.getSe() did not pass the spatial predictors to the Hessian, so standard errors for
spatial models were computed as if the spatial term were absent. plot() on a spatial
model silently fell back to no standard errors as a result.NA was converted to the integer minimum instead
of NaN, which silently corrupted the missing-value masking and conditional predictions.
Responses are now always converted through double.setWeights(model) with the default weights = NULL errored with a subscript out of
bounds for non-spatial models.se() dropped the last incomplete batch, so the Hessian was accumulated over fewer
observations than the model was fitted on.predict(type = "raw") returned 0.999999 * mu + 5e-7 instead of mu, and the same
probability guard was applied to Poisson, negative binomial and Gaussian rates in the
likelihood. It is now applied to bounded links only.se() was added to every element of the matrix rather than to its
diagonal.verbose argument passed to sjSDM_cv() errored with "matched by multiple actual
arguments".step_size larger than the number of observations silently trained on no
batches at all.predict() applied the link to the linear
predictor directly, which is the prediction at latent z = 0, not the expectation over
the latent factor that generates the species associations. The marginal is
E_z[link(mu + z sigma')]: for probit pnorm(mu / sqrt(diag(sigma sigma' + I))), for the
log links exp(mu + rowSums(sigma^2)/2), and unchanged for gaussian. The error grew with
the strength of the associations — up to 0.12 in probability for probit, and a factor of
roughly 1.5 on the rate for poisson and nbinom. predict() now returns the marginal;
predict(marginal = FALSE) restores the old behaviour for reproducing earlier results.
Code that rescaled predict() output by hand to obtain the marginal, e.g. dividing the
probit linear predictor by sqrt(diag(getCov(model))), must drop that step: applied to
the new default it scales twice and pulls the probabilities towards 0.5.
probit, poisson, nbinom and gaussian use exact closed forms and stay deterministic; logit
and linear have no closed form and are approximated by Monte Carlo, so they are stochastic
and respond to sampling.predict(model, Y = ...)) failed for a model with two species
when one of them was conditioned on, with IndexError: tuple index out of range. With a
single conditioning species Y[, focal], predictions[, focal] and sigma[focal, ] all
dropped to vectors, so the backend was handed a one-dimensional association matrix. Three
or more species hid it, because the conditioning set was then never a single column. The
torch backend keeps the matrix shape and the case is covered by a regression test.anova() did not switch a DNN component off. The ~0 refits build a design matrix
with zero columns, but only the first layer of a deep component was built without a bias,
so a switched-off DNN still emitted a learned species constant out of its output layer and
the fraction attributed to it was not zero. Linear components were never affected. On a
model with a DNN environmental and a DNN spatial component, McFadden F_S moves
0.01422 -> 0.00438, F_AS 0.00027 -> 0.02454, F_ABS -0.01161 -> -0.03448, the saturated
R squared 0.9232 -> 0.8296 and the null log-likelihood -488.470 -> -488.489. Variation
partitioning of DNN models from earlier versions is not comparable.predict(newdata = ) re-derived a data-dependent basis from the new rows. The fitted
model now carries its terms object and factor levels, so poly(), ns(), scale() and
factor contrasts are evaluated exactly as they were at fit time. Predictions from a model
whose formula contains any of these were wrong on new data and silently so; predictions on
the training data were unaffected.sjSDM_cv() failed for any formula with an interaction. Each fold overwrites the
design matrix with a sliced, already expanded one and sets formula = ~0+. without
rebuilding the stored terms, so predict(newdata = ) evaluated the original formula
against the expanded design and failed with "object 'X1' not found" on, for example,
~0+X1:X2. The default formula passed only by coincidence.sjSDM_cv() penalised the intercept in every fold. Whether the intercept is excluded
from the L1/L2 penalty was derived from the string "(Intercept)" in the design's column
names, and the per-fold design is rebuilt through data.frame(), which renames that
column. With lambda_coef = 0.5 on a 60 x 5 problem the fold log-likelihood moved
208.3551 -> 211.1313 and AUC_test 0.4330 -> 0.4082. The flag is now carried on the
linear() / DNN() object instead of being re-derived from a column name, and the
lambda = 0 arm was never affected.setWeights() was undone by the next saveRDS() / readRDS(). It changed the live
'torch' module, but a saved model is rebuilt from the serialised state captured at fit
time, so the injected weights were silently replaced by the fitted ones on reload
(measured maximum deviation 1.089 in the prediction). setWeights() now refreshes that
state as well.getSe() now fails with a clear message on a model with a DNN() environmental
component. Standard errors are only defined for the linear model; the Hessian code
previously errored inside 'torch' when the number of species exceeded the width of the
first hidden layer, and was silently wrong otherwise.sjSDM() (verbose = TRUE) is shown from the first optimiser step,
as in the 'PyTorch' version, and reports the epoch, the step within the epoch, the loss,
the elapsed time and the steps per second. It used to appear only after two seconds and
to advance once per epoch, so short fits looked silent.torch_logsumexp() instead of a
hand-rolled max-shift, which replaces seven kernel launches with three. torch_logsumexp
performs the same max-subtraction internally, so the computation is unchanged; the
fixed-noise parity against the 'PyTorch' backend agrees to 1e-8 for all six links.optim_ignite_* builds, which run in libtorch
rather than in R. torch::optim_rmsprop() and torch::optim_sgd() are not used: in
'torch' 0.17.0 they apply weight_decay with the parameter's index instead of its value.
The optimizers with no 'torch' counterpart redirect, see Major changes.sjSDMControl(jit = TRUE) trains with the likelihood traced by torch::jit_trace() on
the first batch; the Monte-Carlo noise is an input of the trace and the last incomplete
batch is dropped as before, so every traced batch has the same shape. The fitted model is
the same as with jit = FALSE up to float rounding. On a GPU (binomial, 500 sites,
200 epochs) the fit is 1.4 to 3.9 times faster, from 50 to 5000 species, and at 5000
species as fast as the 'PyTorch' version. On the CPU it helps only below about 100
species (1.3 times faster at 50) and is 13 to 25 % slower from 500 species on, so it is
meant for GPU fits. The default is FALSE.Suggests. It was a leftover from the removed importance() and
was used nowhere in the package.sjSDM::sjSDM_cv (thanks to Máté) (added unit tests) #88sjSDM::Rsquared bug for spatial models (thanks to Máté) #90sjSDM::anova(...) corresponds now to a type I anova (removed CV) #76sjSDM::Rsquared() uses now Nagelkerke or McFadden R-squared (which is also used in the anova) #76sjSDM::sLVM because of instability issues and other reasonssjSDM::install_sjSDM(), it works now for all x64 systems/versions #81 #79 #71?sjSDMsjSDM::update.sjSDM method to re-fit model with different formula(s)sjSDM::sjSDM.tune method to fit quickly a model with optimized regularization parameters (from sjSDM::sjSDM_cv)sjSDM::sjSDM_cv() #84