confidenceellipse

The ConfidenceEllipse package provides functions for computing the coordinate points of confidence ellipses and ellipsoids for a given bivariate and trivariate dataset, at user-defined confidence level.

https://github.com/christiangoueguel/confidenceellipse

Science Score: 13.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (18.4%) to scientific vocabulary

Keywords

confidence-ellipse confidence-ellipsoid confidence-region multivariate-distribution outliers-detection robust-statistics
Last synced: 6 months ago · JSON representation

Repository

The ConfidenceEllipse package provides functions for computing the coordinate points of confidence ellipses and ellipsoids for a given bivariate and trivariate dataset, at user-defined confidence level.

Basic Info
Statistics
  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • Open Issues: 0
  • Releases: 2
Topics
confidence-ellipse confidence-ellipsoid confidence-region multivariate-distribution outliers-detection robust-statistics
Created almost 2 years ago · Last pushed 9 months ago
Metadata Files
Readme License

README.Rmd

---
output: github_document
---



```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%",
  fig.retina = 2
)
```




# ConfidenceEllipse package logo

[![R-CMD-check](https://github.com/ChristianGoueguel/ConfidenceEllipse/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/ChristianGoueguel/ConfidenceEllipse/actions/workflows/R-CMD-check.yaml)
[![Project Status: Active – The project has reached a stable, usable
state and is being actively
developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![Lifecycle:
stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)
[![License:
MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
[![codecov](https://codecov.io/gh/ChristianGoueguel/ConfidenceEllipse/graph/badge.svg?token=JAGMXN2F70)](https://app.codecov.io/gh/ChristianGoueguel/ConfidenceEllipse)
[![](https://cranlogs.r-pkg.org/badges/last-week/ConfidenceEllipse?color=orange)](https://cran.r-project.org/package=ConfidenceEllipse)
[![](https://cranlogs.r-pkg.org/badges/ConfidenceEllipse?color=yellowgreen)](https://cran.r-project.org/package=ConfidenceEllipse)
[![](https://cranlogs.r-pkg.org/badges/grand-total/ConfidenceEllipse)](https://cran.r-project.org/package=ConfidenceEllipse)
[![CRAN
status](https://www.r-pkg.org/badges/version/ConfidenceEllipse)](https://CRAN.R-project.org/package=ConfidenceEllipse)


The `ConfidenceEllipse` package computes the coordinate points of confidence region for a given bivariate and trivariate dataset. The size of the elliptical region is determined by the confidence level, and the shape is determined by the covariance matrix. The confidence level, typically 95% or 99%, indicates the probability that the ellipse contains the true mean vector, assuming a distribution.

## Installation

You can install `ConfidenceEllipse` from CRAN using:

```r
install.packages("ConfidenceEllipse")
```

Alternatively you can grab the development version from github using devtools:

```r
install.packages("devtools")
devtools::install_github("ChristianGoueguel/ConfidenceEllipse")
```

## Example

```{r message=FALSE, warning=FALSE}
library(magrittr)
library(tidyselect)
library(patchwork)
library(dplyr)
library(ggplot2)
library(purrr)
library(tidyr)
```

```{r message=FALSE, warning=FALSE}
library(ConfidenceEllipse)
```

### Dataset

The dataset is comprised of 13 different measurements for 180
archaeological glass vessels from different groups (Janssen, K.H.A., De
Raedt, I., Schalm, O., Veeckman, J.: Microchim. Acta 15 (suppl.) (1998)
253-267. Compositions of 15th - 17th century archaeological glass
vessels excavated in Antwerp.)

```{r}
data("glass", package = "ConfidenceEllipse")
```

```{r}
glass %>% glimpse()
```

### Confidence Region
#### Classical and robust confidence ellipses

First, the `confidence_ellipse` function is used to compute coordinate points of the confidence ellipse and then the ellipse is plotted on a two-dimensional plot `x`-`y` of the data. It shows a region that, with a specified confidence level `conf_level`, is expected to contain the population mean vector of a bivariate distribution.

```{r message=FALSE, warning=FALSE}
ellipse_95 <- confidence_ellipse(glass, x = SiO2, y = Na2O, conf_level = 0.95, robust = FALSE, distribution = "normal")
rob_ellipse_95 <- confidence_ellipse(glass, x = SiO2, y = Na2O, conf_level = 0.95, robust = TRUE, distribution = "normal")
```

```{r message=FALSE, warning=FALSE}
ellipse_95 %>% glimpse()
```

```{r}
cutoff <- qchisq(0.95, df = 2)
MDsquared <- glass %>%
  select(SiO2, Na2O) %>%
  as.matrix() %>%
  mahalanobis(colMeans(.), cov(.), inverted = FALSE)
```

```{r}
plot1 <-
  ggplot() +
  geom_path(data = ellipse_95, aes(x = x, y = y), color = "blue", linewidth = 1L) +
  geom_point(data = glass %>% mutate(md = MDsquared) %>% filter(md <= cutoff), aes(x = SiO2, y = Na2O), shape = 21L, color = "black", fill = "lightblue", size = 3L) +
  geom_point(data = glass %>% mutate(md = MDsquared) %>% filter(md > cutoff), aes(x = SiO2, y = Na2O), shape = 21L, color = "black", fill = "gold", size = 3L) +
  labs(x = "SiO2 (wt.%)", y = "Na2O (wt.%)", title = "Classical confidence ellipse\nat 95% confidence level") +
  theme_bw() +
  theme(
    panel.grid = element_blank(),
    legend.position = "none"
  )
```

```{r}
x_mcd <- glass %>%
  select(SiO2, Na2O) %>%
  as.matrix() %>%
  robustbase::covMcd()
```

```{r}
rob_MDsquared <- glass %>%
  select(SiO2, Na2O) %>%
  as.matrix() %>%
  mahalanobis(x_mcd$center, x_mcd$cov)
```

```{r}
plot2 <-
  ggplot() +
  geom_path(data = rob_ellipse_95, aes(x = x, y = y), color = "blue", linewidth = 1L) +
  geom_point(data = glass %>% mutate(md = rob_MDsquared) %>% filter(md <= cutoff), aes(x = SiO2, y = Na2O), shape = 21L, color = "black", fill = "lightblue", size = 3L) +
  geom_point(data = glass %>% mutate(md = rob_MDsquared) %>% filter(md > cutoff), aes(x = SiO2, y = Na2O), shape = 21L, color = "black", fill = "gold", size = 3L) +
  labs(x = "SiO2 (wt.%)", y = "Na2O (wt.%)", title = "Robust confidence ellipse\nat 95% confidence level") +
  theme_bw() +
  theme(
    panel.grid = element_blank(),
    legend.position = "none"
  )
```

```{r}
plot1 | plot2
```

##### Grouping

For grouping bivariate data, the `.group_by` argument can be used if the data contains an unique grouping variable (`.group_by = NULL` by default). When a grouping variable is provided, the function will compute the ellipses separately for each level of the factor, allowing you to explore potential differences or patterns within subgroups of the data.

It's important to note that the grouping variable should be appropriately coded as a factor before passing it to the `.group_by` argument. If the variable is currently stored as a character or numeric type, you may need to convert it to a factor using functions like `as.factor()` or `forcats::as_factor()`.

```{r}
rpca_scores <- glass %>%
  select(where(is.numeric)) %>%
  pcaPP::PCAproj(method = "qn") %>%
  pluck("scores") %>%
  as_tibble() %>%
  mutate(glassType = glass %>% pull(glassType)) %>%
  rename(PC1 = Comp.1, PC2 = Comp.2)
```

```{r message=FALSE, warning=FALSE}
ellipse_pca <- rpca_scores %>% confidence_ellipse(x = PC1, y = PC2, .group_by = glassType)
```

```{r}
ggplot() +
  geom_point(data = rpca_scores, aes(x = PC1, y = PC2, color = glassType, shape = glassType), size = 3L) +
  geom_path(data = ellipse_pca, aes(x = x, y = y, color = glassType), linewidth = 1L) +
  scale_color_brewer(palette = "Set1", direction = 1) +
  labs(x = "PC1", y = "PC2", title = "Principal component analysis") +
  theme_bw() +
  theme(
    aspect.ratio = .7,
    panel.grid = element_blank(),
    legend.position = "none"
  )
```

#### Ellipsoid

The `confidence_ellipsoid` function accepts an additional variable `z` and computes the ellipsoid for trivariate data.

```{r message=FALSE, warning=FALSE}
ellipsoid_grp <- glass %>% confidence_ellipsoid(SiO2, Na2O, Fe2O3, glassType)
```

```{r}
ellipsoid_grp %>% glimpse()
```

```{r message=FALSE, warning=FALSE}
rgl::setupKnitr(autoprint = TRUE)
rgl::plot3d(
  x = ellipsoid_grp$x,
  y = ellipsoid_grp$y,
  z = ellipsoid_grp$z,
  xlab = "SiO2 (wt.%)",
  ylab = "Na2O (wt.%)",
  zlab = "Fe2O3 (wt.%)",
  type = "s",
  radius = 0.03,
  col = as.numeric(ellipsoid_grp$glassType)
)
rgl::points3d(
  x = glass$SiO2,
  y = glass$Na2O,
  z = glass$Fe2O3,
  col = as.numeric(glass$glassType),
  size = 5
)
rgl::view3d(theta = 260, phi = 30, fov = 60, zoom = .85)
```

















Owner

  • Name: Christian L. Goueguel
  • Login: ChristianGoueguel
  • Kind: user
  • Location: Scottsdale, AZ, United States

Optics | Laser Spectroscopy | Chemometrics

GitHub Events

Total
  • Release event: 1
  • Watch event: 1
  • Push event: 6
  • Create event: 1
Last Year
  • Release event: 1
  • Watch event: 1
  • Push event: 6
  • Create event: 1

Packages

  • Total packages: 1
  • Total downloads:
    • cran 624 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 2
  • Total maintainers: 1
cran.r-project.org: ConfidenceEllipse

Computation of 2D and 3D Elliptical Joint Confidence Regions

  • Versions: 2
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 624 Last month
Rankings
Dependent packages count: 27.8%
Dependent repos count: 35.8%
Average: 49.5%
Downloads: 84.8%
Last synced: 7 months ago

Dependencies

.github/workflows/R-CMD-check.yaml actions
  • actions/checkout v4 composite
  • r-lib/actions/check-r-package v2 composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/pkgdown.yaml actions
  • JamesIves/github-pages-deploy-action v4.5.0 composite
  • actions/checkout v4 composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
DESCRIPTION cran
  • dplyr * imports
  • forcats * imports
  • magrittr * imports
  • pcaPP * imports
  • purrr * imports
  • stats * imports
  • tibble * imports
  • tidyr * imports
  • tidyselect * imports