R/predict.trophicSDMfit.R
predict.trophicSDMfit.Rd
Computes predicted values from the fitted trophicSDMfit model at environmental conditions specified by Xnew
. Once predictions have been obtained, their quality can eventually be evaluated with evaluateModelFit()
.
# S3 method for trophicSDMfit
predict(
object,
Xnew = NULL,
prob.cov = FALSE,
pred_samples = NULL,
run.parallel = FALSE,
verbose = FALSE,
fullPost = TRUE,
filter.table = NULL,
...
)
A trophicSDMfit object obtained with trophicSDM()
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).
Parameter to predict with trophicSDM with presence-absence data. Whether to use predicted probability of presence (prob.cov = T) or the transformed presence-absences (default, prov.cov = F) to predict species distribution.
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.
Whether to use parallelise code when possible. Can speed up computation time.
Whether to print advances of the algorithm
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,
Optional, default to NULL, should be provided only if the users wants to filter some species predictions. A sites x species matrix of zeros and ones.
additional arguments
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.
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 = 50,
family = binomial(link = "logit"), penal = NULL,
mode = "prey", method = "stan_glm")
#> Warning: The largest R-hat is 1.17, 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.13, 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: Markov chains did not converge! Do not analyze results!
#> Warning: The largest R-hat is 1.17, 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: Markov chains did not converge! Do not analyze results!
#> Warning: The largest R-hat is 1.11, 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: Markov chains did not converge! Do not analyze results!
#> Warning: The largest R-hat is 1.09, 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.4, 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
# We can now evaluate species probabilities of presence for the environmental conditions c(0.5, 0.5)
predict(m, Xnew = data.frame(X_1 = 0.5, X_2 = 0.5))
#> $Y1
#> [,1] [,2] [,3] [,4] [,5]
#> 1 0.6342476 0.6130067 0.6300371 0.6267161 0.61897
#>
#> $Y2
#> [,1] [,2] [,3] [,4] [,5]
#> 1 0.7003627 0.6984545 0.6918214 0.6995717 0.6724
#>
#> $Y3
#> [,1] [,2] [,3] [,4] [,5]
#> 1 0.7337134 0.7330132 0.7024801 0.7401973 0.7138056
#>
#> $Y4
#> [,1] [,2] [,3] [,4] [,5]
#> 1 0.4696927 0.4569712 0.5162909 0.5171987 0.5415792
#>
#> $Y5
#> [,1] [,2] [,3] [,4] [,5]
#> 1 0.4487304 0.459831 0.6697429 0.7431611 0.7218115
#>
#> $Y6
#> [,1] [,2] [,3] [,4] [,5]
#> 1 0.5863913 0.6555618 0.8184787 0.699581 0.7512535
#>
# Obtain 50 draws from the posterior predictive distribution of species (pred_samples = 10)
# using predicted presence-absences of species to predict their predators (prob.cov = TRUE)
# Since we don't specify Xnew, the function sets Xnew = X by default
Ypred = predict(m, fullPost = TRUE, pred_samples = 10, prob.cov = FALSE)
# We can ask the function to only give back posterior mean and 95% credible intervals with
# fullPost = F
# \donttest{
Ypred = predict(m, fullPost = TRUE, pred_samples = 30, prob.cov = FALSE)
# }
# 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 = predict(m, pred_samples = 1, prob.cov = FALSE)