Minima and Maxima in R
Introduction to Concepts
In mathematics and programming, minima and maxima refer to the lowest and highest values in a dataset, respectively. These can be categorized into:
- Local Minima: Points where a function takes a value lower than its immediate neighbors.
- Local Maxima: Points where a function takes a value higher than its immediate neighbors.
- Global Minima: The lowest value over the entire domain of the function.
- Global Maxima: The highest value over the entire domain of the function.
Basic Functions for Finding Minima and Maxima
min() Function
Returns the minimum value of a vector or matrix.
Example:
# Example vector vec <- c(7, 2, 5, 8, 1, 9) # Find the minimum value min_val <- min(vec) print(min_val) # [1] 1
max() Function
Returns the maximum value of a vector or matrix.
Example:
# Example vector vec <- c(7, 2, 5, 8, 1, 9) # Find the maximum value max_val <- max(vec) print(max_val) # [1] 9
Finding the Indices of Minima and Maxima
which.min() Function
Returns the index of the first minimum in a vector.
Example:
# Example vector vec <- c(7, 2, 5, 8, 1, 9) # Find the index of the minimum min_index <- which.min(vec) print(min_index) # [1] 5
which.max() Function
Returns the index of the first maximum in a vector.
Example:
# Example vector vec <- c(7, 2, 5, 8, 1, 9) # Find the index of the maximum max_index <- which.max(vec) print(max_index) # [1] 6
Finding Local Minima and Maxima
For more complex functions or time series, you may need to find local minima and maxima, which basic functions min() and max() cannot directly handle.
Using the pracma Package
The pracma package provides tools for finding local extrema.
Installation and Loading the Package:
install.packages("pracma") library(pracma)
Example:
# Example function f <- function(x) x^3 - 6*x^2 + 9*x # Find local extrema in the interval [0, 4] x <- seq(0, 4, length.out = 100) y <- f(x) # Find local minima and maxima extrema <- findpeaks(y) print(extrema)
Using the stats Package
For continuous functions, you can use optimization methods.
Example:
# Example function f <- function(x) x^2 - 4*x + 4 # Find the global minimum using `optimize` opt <- optimize(f, interval = c(0, 4)) print(opt$minimum) print(opt$objective)
Minima and Maxima in Matrices
Finding Minima and Maxima in a Matrix
For matrices, min() and max() applied directly return the minimum and maximum values over the entire matrix.
Example:
# Example matrix mat <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3) # Find minima and maxima in the matrix min_mat <- min(mat) max_mat <- max(mat) print(min_mat) # [1] 1 print(max_mat) # [1] 9
Finding Minima and Maxima by Column or Row
Example:
# Find the minimum by column min_col <- apply(mat, 2, min) print(min_col) # [1] 1 2 3 # Find the maximum by row max_row <- apply(mat, 1, max) print(max_row) # [1] 3 6 9
Practical Example: Finding Minima and Maxima of a Function
Consider a quadratic function and find its minima and maxima.
Example:
# Define the function f <- function(x) (x - 2)^2 + 1 # Find the global minimum opt <- optimize(f, interval = c(-10, 10)) print(paste("Minima:", opt$minimum)) print(paste("Value at minima:", opt$objective))
Summary
- Minima and Maxima: Use min() and max() for global values in vectors and matrices.
- Indices of Minima and Maxima: Use which.min() and which.max() for their indices.
- Local Minima and Maxima: Use packages like pracma or optimization techniques for complex functions.
- Matrices: Use apply() to find minima and maxima by column or row.