---
title: "3. Niche modeling"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{3. Niche modeling}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse  = TRUE,
  comment   = "#>",
  fig.width = 7,
  fig.height = 5,
  out.width = "90%"
)
```

After thinning, the environmental niche is summarised by a multivariate
ellipsoid via `fit_ellipsoid()`. Two estimators are available:

- `"covmat"` — classical sample mean and covariance.
- `"mve"` — robust Minimum Volume Ellipsoid (Rousseeuw, 1985).

The ellipsoid boundary is defined by a chi-square cutoff on the squared
Mahalanobis distance, controlled by `level` (default 95 %).

```{r setup}
library(bean)
data(origin_dat_prepared,    package = "bean")
data(thinned_stochastic,     package = "bean")
data(thinned_deterministic,  package = "bean")
env_vars <- c("bio_1", "bio_4", "bio_12", "bio_15")
```

## Fit three ellipsoids

We fit one ellipsoid to the raw prepared data and one to each of the
thinned datasets, so we can compare what bias correction does to the
inferred niche.

```{r}
origin_ellipse <- fit_ellipsoid(
  data     = origin_dat_prepared,
  env_vars = env_vars,
  method   = "covmat",
  level    = 0.95
)

stochastic_ellipse <- fit_ellipsoid(
  data     = thinned_stochastic$thinned_data,
  env_vars = env_vars,
  method   = "covmat",
  level    = 0.95
)

deterministic_ellipse <- fit_ellipsoid(
  data     = thinned_deterministic$thinned_points,
  env_vars = env_vars,
  method   = "covmat",
  level    = 0.95
)

origin_ellipse
stochastic_ellipse
deterministic_ellipse
```

## Visualise the ellipsoids (2-D slices)

```{r, fig.width = 6, fig.height = 6}
plot(origin_ellipse,        dims = c("bio_1", "bio_12"))
plot(stochastic_ellipse,    dims = c("bio_1", "bio_12"))
plot(deterministic_ellipse, dims = c("bio_1", "bio_12"))
```

For an interactive 3-D view, install the optional package **rgl** and
call `plot(origin_ellipse, dims = c(1, 2, 3))`. If `rgl` is not
installed, the plot falls back to the 2-D view of the first two
requested variables.

## Inspecting the inferred niche

`fit_ellipsoid()` returns an object that exposes the centroid, the
covariance matrix, the precomputed inverse, the variable names, and the
subset of points classified as inside or outside the ellipsoid. These
are the building blocks downstream species-distribution-modelling
pipelines need.

```{r}
origin_ellipse$centroid
origin_ellipse$cov_matrix
origin_ellipse$dimensions
origin_ellipse$var_names
head(origin_ellipse$points_in_ellipse)
```

## Projecting suitability with nicheR

If you intend to project a `bean_ellipsoid` into geographic space,
please install the **nicheR** package and use its `predict()` method.
`bean_ellipsoid` objects carry `"nicheR_ellipsoid"` as a second S3
class, so once nicheR is attached its `predict.nicheR_ellipsoid()`
method dispatches on them automatically — no conversion step is
required. If you use the prediction step in published work, please
cite nicheR (see the References section at the bottom of this
vignette).

### Installing and loading nicheR

Install the `nicheR` from GitHub:

```{r, eval = FALSE}
# Installing and loading packages
if (!require("devtools")) install.packages("devtools")

# To install the package use
devtools::install_github("castanedaM/nicheR")

library(nicheR)
```

### Predicting suitability

```{r, eval = FALSE}
library(terra)

env <- terra::rast(system.file("extdata", "thai_env.tif", package = "bean"))

# 'origin_ellipse' is the bean_ellipsoid we fitted above.
suit <- predict(
  origin_ellipse,
  newdata               = env,
  include_suitability   = TRUE,
  suitability_truncated = TRUE,
  include_mahalanobis   = FALSE
)
terra::plot(suit)
```

## References

- Castaneda-Guzman, M., Hughes, C., Paansri, P., & Cobos, M. E. (2026).
  *nicheR: Ellipsoid-Based Virtual Niches and Visualization.* R package
  version 0.1.0. <https://github.com/castanedaM/nicheR>
- Rousseeuw, P. J. (1985). Multivariate estimation with high breakdown
  point. *Mathematical Statistics and Applications, Vol. B*, 283–297.
- Van Aelst, S. & Rousseeuw, P. (2009). Minimum volume ellipsoid.
  *Wiley Interdisciplinary Reviews: Computational Statistics*, 1(1),
  71–82.
