Assignment II: Classification of Flowers
Ferris Moser
24 March 2025
Introduction
This document analyzes the classification of Iris versicolor and Iris virginica flowers using petal length and
width. We’ll use simulation, density-based methods, and graphical analysis inspired by the style of our coin
flip assignment.
Installing and Loading Required Packages
library(tidyverse)
## -- Attaching core tidyverse packages ------------------------ tidyverse 2.0.0 -## v dplyr
1.1.4
v readr
2.1.5
## v forcats
1.0.0
v stringr
1.5.1
## v ggplot2
3.5.1
v tibble
3.2.1
## v lubridate 1.9.4
v tidyr
1.3.1
## v purrr
1.0.4
## -- Conflicts ------------------------------------------ tidyverse_conflicts() -## x dplyr::filter() masks stats::filter()
## x dplyr::lag()
masks stats::lag()
## i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(MASS)
##
## Attaching package: ’MASS’
##
## The following object is masked from ’package:dplyr’:
##
##
select
library(ggplot2)
A: Plotting Petal Length vs Width
1
data(iris)
x0 <- iris[iris$Species == "versicolor", c("Petal.Length", "Petal.Width")]
x1 <- iris[iris$Species == "virginica", c("Petal.Length", "Petal.Width")]
# Stretch vertically by setting the figure size via par()
par(pin = c(4, 2)) # width = 4 inches, height = 8 inches
plot(x0$Petal.Length, x0$Petal.Width, col = 'blue', pch = 16,
xlab = "Petal Length", ylab = "Petal Width",
xlim = c(3, 7), ylim = c(0.5, 3.5),
main = "Petal Dimensions: Versicolor vs Virginica")
points(x1$Petal.Length, x1$Petal.Width, col = 'red', pch = 17)
legend("topleft", legend = c("versicolor", "virginica"),
col = c("blue", "red"), pch = c(16, 17))
1.5
2.5
versicolor
virginica
0.5
Petal Width
3.5
Petal Dimensions: Versicolor vs Virginica
3
4
5
Petal Length
B: Manual Classification Based on Plot
(4.0, 1.1) → Versicolor
(6.0, 2.3) → Virginica
(5.0, 1.7) → Virginica
Based on spatial proximity in the scatter plot.
2
6
7
C: Bayes Rule Transformation
We confirm:
P (Y = 0|x) > P (Y = 1|x) ⇐⇒ f0 (x)p0 > f1 (x)p1
by applying Bayes’ Theorem.
D: Estimating Class Probabilities
n0 <- nrow(x0)
n1 <- nrow(x1)
p_hat_0 <- n0 / (n0 + n1)
p_hat_1 <- 1 - p_hat_0
E: Bivariate Normal Generator
rbinorm <- function(n, mu1, mu2, rho, sigma1, sigma2) {
U <- rnorm(n)
V <- rnorm(n)
X1 <- mu1 + sigma1 * sqrt(1 - rhoˆ2) * U + sigma1 * rho * V
X2 <- mu2 + sigma2 * V
return(data.frame(Petal.Length = X1, Petal.Width = X2))
}
F: Simulating and Plotting Distributions
set.seed(123)
m1 <- rbinorm(50, 1, 3, -0.7, 1, sqrt(2))
m2 <- rbinorm(50, 1, 3, 0.1, 1, sqrt(2))
m3 <- rbinorm(50, 1, 3, 0.9, 1, sqrt(2))
m1$rho <- "-0.7"
m2$rho <- "0.1"
m3$rho <- "0.9"
m_all <- rbind(m1, m2, m3)
# Plot all in one frame
ggplot(m_all, aes(x = Petal.Length, y = Petal.Width, color = rho)) +
geom_point() +
facet_wrap(~rho) +
labs(title = "Bivariate Normal Simulations with Different Rho") +
theme_linedraw()
3
Bivariate Normal Simulations with Different Rho
−0.7
0.1
0.9
Petal.Width
6
rho
4
−0.7
0.1
0.9
2
0
−1
0
1
2
3
−1
0
1
2
Petal.Length
G: Estimating Mean and Covariance
M <- rbinorm(10000, 1, 3, 0.7, 1, sqrt(3))
mu_hat <- colMeans(M)
sigma_hat <- cov(M)
mu_hat
## Petal.Length
##
0.9891347
Petal.Width
2.9759915
sigma_hat
##
Petal.Length Petal.Width
## Petal.Length
1.004130
1.223986
## Petal.Width
1.223986
3.023231
H-I: Estimate Parameters from Iris Data
mu0_hat <- colMeans(x0)
sigma0_hat <- cov(x0)
4
3
−1
0
1
2
3
mu1_hat <- colMeans(x1)
sigma1_hat <- cov(x1)
J: Classification Rule Derivation
We use the formula:
rk2 (x) = (x − µ̂k )T Σ̂−1
k (x − µ̂k )
Then choose type 0 if:
r02 (x) < log
det Σ1
det Σ0
+ 2 log
p̂0
p̂1
+ r12 (x)
K: Implementation of Classifier and Prediction
classify <- function(x, mu0, mu1, sigma0, sigma1, p0, p1) {
r0 <- t(x - mu0) %*% solve(sigma0) %*% (x - mu0)
r1 <- t(x - mu1) %*% solve(sigma1) %*% (x - mu1)
threshold <- log(det(sigma1)/det(sigma0)) + 2 * log(p0 / p1)
if (r0 < threshold + r1) {
return("versicolor")
} else {
return("virginica")
}
}
x_new <- c(5.0, 1.8)
classify(x_new, mu0_hat, mu1_hat, sigma0_hat, sigma1_hat, p_hat_0, p_hat_1)
## [1] "virginica"
The predicted class is virginica.
5