Computes predicted values of the potential niches of species from the fitted trophicSDMfit model at environmental conditions specified by Xnew. Predictions are obtained by setting preys to present when mode = "prey" or setting predators to absent when mode = "predator".

predictPotential(
  tSDM,
  Xnew = NULL,
  pred_samples = NULL,
  verbose = FALSE,
  fullPost = TRUE
)

Arguments

tSDM

A trophicSDMfit object obtained with trophicSDM()

Xnew

a matrix specifying the environmental covariates for the predictions to be made. If NULL (default), predictions are done on the training dataset (e.g. by setting Xnew = tSDM$data$X).

pred_samples

Number of samples to draw from species posterior predictive distribution when method = "stan_glm". If NULL, set by the default to the number of iterations/10.

verbose

Whether to print advances of the algorithm.

fullPost

Optional parameter for stan_glm only. Whether to give back the full posterior predictive distribution (default, fullPost = TRUE) or just the posterior mean, and 2.5% and 97.5% quantiles.

Value

A list containing for each species the predicted value at each sites. If method = "stan_glm", then each element of the list is a sites x pred_samples matrix containing the posterior predictive distribution of the species at each sites.

Author

Giovanni Poggiato and Jérémy Andréoletti

Examples

data(Y, X, G)
# define abiotic part of the model
env.formula = "~ X_1 + X_2"
# Run the model with bottom-up control using stan_glm as fitting method and no penalisation
# (set iter = 1000 to obtain reliable results)
m = trophicSDM(Y, X, G, env.formula, iter = 100,
               family = binomial(link = "logit"), penal = NULL, 
               mode = "prey", method = "stan_glm")
#> Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#bulk-ess
#> Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#tail-ess
#> Warning: The largest R-hat is 1.07, indicating chains have not mixed.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#r-hat
#> Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#bulk-ess
#> Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#tail-ess
#> Warning: The largest R-hat is 1.14, indicating chains have not mixed.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#r-hat
#> Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#bulk-ess
#> Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#tail-ess
#> Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#bulk-ess
#> Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#tail-ess
#> Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#bulk-ess
#> Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#tail-ess
#> Warning: The largest R-hat is 1.08, indicating chains have not mixed.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#r-hat
#> Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#bulk-ess
#> Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#tail-ess
# Obtain 100 draws from the posterior predictive distribution of species potential niche
# (pred_samples = 50)
# Since we don't specify Xnew, the function sets Xnew = X by default
Ypred = predictPotential(m, fullPost = TRUE, pred_samples = 50)
# We can ask the function to only give back posterior mean and 95% credible intervals with
# fullPost = FALSE
# \donttest{
Ypred = predictPotential(m, fullPost = FALSE, pred_samples = 50)
# }
#' We can now evaluate species probabilities of presence for the enviromental
# conditions c(0.5, 0.5)
predictPotential(m, Xnew = data.frame(X_1 = 0.5, X_2 = 0.5), pred_samples = 50)
#> $Y1
#>    iterations
#>          [,1]      [,2]    [,3]      [,4]     [,5]      [,6]      [,7]
#>   1 0.6545044 0.6182437 0.63368 0.6118405 0.660032 0.6462264 0.6428036
#>    iterations
#>          [,8]      [,9]     [,10]     [,11]     [,12]     [,13]     [,14]
#>   1 0.6178748 0.6335385 0.6457833 0.6372104 0.6219266 0.6251632 0.6085761
#>    iterations
#>         [,15]     [,16]     [,17]     [,18]     [,19]     [,20]     [,21]
#>   1 0.6287966 0.6209957 0.6234762 0.6348431 0.6394842 0.6089998 0.6275742
#>    iterations
#>         [,22]    [,23]     [,24]     [,25]     [,26]     [,27]     [,28]
#>   1 0.6159277 0.623478 0.6297146 0.6127545 0.6093242 0.6481572 0.6288652
#>    iterations
#>         [,29]     [,30]     [,31]     [,32]     [,33]     [,34]     [,35]
#>   1 0.5985056 0.6312939 0.6318826 0.6338561 0.6217674 0.6058944 0.6261422
#>    iterations
#>         [,36]     [,37]     [,38]    [,39]     [,40]     [,41]     [,42]
#>   1 0.6353402 0.6200997 0.6557343 0.608249 0.6381668 0.6401194 0.6319808
#>    iterations
#>         [,43]     [,44]     [,45]     [,46]    [,47]     [,48]     [,49]
#>   1 0.6148253 0.6247107 0.6327132 0.6156696 0.614173 0.6267165 0.6247265
#>    iterations
#>        [,50]
#>   1 0.628408
#> 
#> $Y2
#>    iterations
#>         [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]
#>   1 0.679015 0.6869489 0.7015458 0.7050992 0.6889761 0.6741702 0.6701799
#>    iterations
#>        [,8]      [,9]     [,10]     [,11]     [,12]     [,13]     [,14]
#>   1 0.66348 0.7011663 0.6899764 0.7287358 0.7124701 0.7085245 0.6829314
#>    iterations
#>         [,15]     [,16]     [,17]     [,18]     [,19]     [,20]     [,21]
#>   1 0.7049694 0.7085084 0.7042884 0.7104766 0.6682006 0.7020268 0.6748832
#>    iterations
#>         [,22]     [,23]     [,24]     [,25]     [,26]     [,27]     [,28]
#>   1 0.6933023 0.6775862 0.6770809 0.6874956 0.6699316 0.7180882 0.6883318
#>    iterations
#>         [,29]     [,30]    [,31]     [,32]     [,33]     [,34]    [,35]
#>   1 0.6948387 0.6806721 0.679946 0.7223562 0.7087405 0.6837108 0.673216
#>    iterations
#>         [,36]     [,37]     [,38]     [,39]     [,40]     [,41]     [,42]
#>   1 0.6806625 0.6560125 0.6869442 0.7202016 0.7092481 0.6732364 0.6890211
#>    iterations
#>         [,43]     [,44]     [,45]     [,46]     [,47]     [,48]     [,49]
#>   1 0.7036591 0.6806691 0.7310592 0.6545381 0.6813945 0.7281092 0.6903349
#>    iterations
#>         [,50]
#>   1 0.7262759
#> 
#> $Y3
#>    iterations
#>          [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]
#>   1 0.7349776 0.6916054 0.7284142 0.7255198 0.7256805 0.7282916 0.7085984
#>    iterations
#>          [,8]      [,9]     [,10]     [,11]     [,12]     [,13]     [,14]
#>   1 0.6950018 0.7028179 0.7334992 0.7098328 0.7301366 0.7091457 0.7090521
#>    iterations
#>         [,15]    [,16]    [,17]     [,18]     [,19]     [,20]     [,21]
#>   1 0.7231348 0.750435 0.732035 0.7309513 0.7230304 0.7229406 0.7153008
#>    iterations
#>         [,22]     [,23]    [,24]     [,25]     [,26]     [,27]     [,28]
#>   1 0.7261103 0.7132204 0.746989 0.7260448 0.7176976 0.7239221 0.7153797
#>    iterations
#>         [,29]     [,30]     [,31]     [,32]     [,33]     [,34]     [,35]
#>   1 0.7157102 0.7272604 0.7149441 0.7063574 0.7265614 0.7000494 0.7112952
#>    iterations
#>         [,36]     [,37]     [,38]     [,39]     [,40]     [,41]     [,42]
#>   1 0.7186188 0.7416684 0.7209272 0.7334078 0.7031443 0.7051043 0.7179223
#>    iterations
#>         [,43]     [,44]     [,45]     [,46]     [,47]    [,48]     [,49]
#>   1 0.7116272 0.7014925 0.7054626 0.7398277 0.7157682 0.728109 0.7274202
#>    iterations
#>        [,50]
#>   1 0.714812
#> 
#> $Y4
#>    iterations
#>          [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]
#>   1 0.4655026 0.5025745 0.4880367 0.4651723 0.5378842 0.4601714 0.4889774
#>    iterations
#>          [,8]      [,9]    [,10]     [,11]     [,12]     [,13]     [,14]
#>   1 0.5217483 0.4405431 0.501139 0.4979686 0.4799549 0.5028456 0.4701492
#>    iterations
#>         [,15]     [,16]   [,17]     [,18]     [,19]     [,20]     [,21]
#>   1 0.4693816 0.4948201 0.45881 0.4479292 0.4682182 0.4671953 0.4742571
#>    iterations
#>         [,22]    [,23]     [,24]     [,25]     [,26]     [,27]     [,28]
#>   1 0.4764065 0.496871 0.4808437 0.4673219 0.4692908 0.5039878 0.4822692
#>    iterations
#>       [,29]     [,30]     [,31]     [,32]     [,33]     [,34]     [,35]
#>   1 0.45797 0.4678848 0.4193334 0.4775504 0.4508146 0.5084965 0.4796583
#>    iterations
#>         [,36]     [,37]     [,38]     [,39]     [,40]     [,41]     [,42]
#>   1 0.4853838 0.4917618 0.4927231 0.4702168 0.4380608 0.4597738 0.4704448
#>    iterations
#>         [,43]     [,44]    [,45]     [,46]    [,47]     [,48]     [,49]
#>   1 0.4469868 0.4527778 0.495018 0.4944045 0.508102 0.4708423 0.5036012
#>    iterations
#>         [,50]
#>   1 0.5044165
#> 
#> $Y5
#>    iterations
#>          [,1]      [,2]      [,3]     [,4]    [,5]     [,6]      [,7]     [,8]
#>   1 0.4549129 0.4842445 0.4603783 0.494675 0.47687 0.487221 0.4573131 0.474461
#>    iterations
#>          [,9]     [,10]     [,11]  [,12]     [,13]     [,14]     [,15]
#>   1 0.4487971 0.4627428 0.4505283 0.4566 0.4456803 0.4737427 0.4879551
#>    iterations
#>         [,16]     [,17]    [,18]     [,19]     [,20]     [,21]     [,22]
#>   1 0.4419376 0.4623147 0.458376 0.5177213 0.4752563 0.4327504 0.4843091
#>    iterations
#>         [,23]     [,24]     [,25]     [,26]     [,27]     [,28]     [,29]
#>   1 0.5085669 0.4568741 0.4688465 0.4566563 0.4459982 0.4481011 0.4644538
#>    iterations
#>       [,30]     [,31]     [,32]     [,33]     [,34]     [,35]     [,36]
#>   1 0.45842 0.4924661 0.4547665 0.4389772 0.4510301 0.4263437 0.4814954
#>    iterations
#>         [,37]    [,38]     [,39]   [,40]     [,41]     [,42]     [,43]
#>   1 0.4493404 0.481378 0.4899763 0.49018 0.5069535 0.4483026 0.4662248
#>    iterations
#>         [,44]     [,45]     [,46]     [,47]     [,48]     [,49]     [,50]
#>   1 0.4472148 0.4379634 0.4725839 0.4736669 0.4735103 0.3875117 0.4342873
#> 
#> $Y6
#>    iterations
#>          [,1]     [,2]      [,3]      [,4]      [,5]      [,6]      [,7]
#>   1 0.4622364 0.554894 0.5548217 0.5305671 0.5467007 0.5010545 0.5425539
#>    iterations
#>         [,8]      [,9]     [,10]     [,11]     [,12]     [,13]   [,14]
#>   1 0.549292 0.5882104 0.5364192 0.5236175 0.5885501 0.5152018 0.54208
#>    iterations
#>         [,15]     [,16]     [,17]     [,18]     [,19]     [,20]     [,21]
#>   1 0.5254573 0.6026325 0.5650705 0.5775674 0.4972296 0.5575084 0.5403177
#>    iterations
#>         [,22]     [,23]     [,24]     [,25]     [,26]     [,27]     [,28]
#>   1 0.4762124 0.5626662 0.5107622 0.4927207 0.5610642 0.6014842 0.5054201
#>    iterations
#>         [,29]    [,30]     [,31]     [,32]     [,33]     [,34]   [,35]    [,36]
#>   1 0.4886663 0.525432 0.4770958 0.5491433 0.5080618 0.5574042 0.62311 0.483821
#>    iterations
#>         [,37]     [,38]     [,39]     [,40]     [,41]     [,42]     [,43]
#>   1 0.5269751 0.4814855 0.5375748 0.5079692 0.5005342 0.5101413 0.5262478
#>    iterations
#>         [,44]     [,45]     [,46]     [,47]     [,48]   [,49]     [,50]
#>   1 0.4772981 0.4830988 0.5314577 0.5445891 0.5111587 0.48584 0.4882751
#> 

# If we fit the model using in a frequentist  way (e.g. glm)
m = trophicSDM(Y, X, G, env.formula, 
               family = binomial(link = "logit"), penal = NULL, 
               mode = "prey", method = "glm")
# We are obliged to set pred_samples = 1 
# (this is done by default if pred_samples is not provided)
# In the frequentist case, fullPost is useless.
Ypred = predictPotential(m, pred_samples = 1)