library(ggplot2)
# Create a grid of points for region shading
create_hyperplane_plot_with_shading <- function() {
# Create a grid of points
x1_seq <- seq(-5, 5, length.out = 200)
x2_seq <- seq(-5, 15, length.out = 200)
grid <- expand.grid(X1 = x1_seq, X2 = x2_seq)
# Calculate values for each hyperplane equation
grid$eq1 <- 1 + 3*grid$X1 - grid$X2
grid$eq2 <- -2 + grid$X1 + 2*grid$X2
# Create region indicators
grid$region1 <- ifelse(grid$eq1 > 0, "> 0", "< 0")
grid$region2 <- ifelse(grid$eq2 > 0, "> 0", "< 0")
# Create the plot with region shading
p <- ggplot() +
# Add shaded regions for hyperplane 1
geom_raster(data = grid, aes(x = X1, y = X2, fill = region1), alpha = 0.3) +
scale_fill_manual(values = c("> 0" = "lightblue", "< 0" = "pink"),
name = "1 + 3X1 - X2") +
# Add the hyperplane lines
geom_abline(intercept = 1, slope = 3, color = "blue", linewidth = 1) +
geom_abline(intercept = 1, slope = -0.5, color = "red", linewidth = 1) +
# Add text annotations
annotate("text", x = -3, y = 10, label = "1 + 3X1 - X2 > 0", color = "blue") +
annotate("text", x = 2, y = 0, label = "1 + 3X1 - X2 < 0", color = "blue") +
annotate("text", x = -4, y = 7, label = "-2 + X1 + 2X2 > 0", color = "red") +
annotate("text", x = 4, y = -2, label = "-2 + X1 + 2X2 < 0", color = "red") +
# Add other plot elements
coord_cartesian(xlim = c(-5, 5), ylim = c(-5, 15)) +
labs(x = "X1", y = "X2", title = "Hyperplanes with Shaded Regions") +
theme_minimal()
return(p)
}
# Generate and display the plot with shading
create_hyperplane_plot_with_shading()
First hyperplane (blue): 1 + 3X1 - X2 = 0
This can be rewritten as X2 = 1 + 3X1
Points above this line satisfy 1 + 3X1 - X2 < 0
Points below this line satisfy 1 + 3X1 - X2 > 0
Second hyperplane (red): -2 + X1 + 2X2 = 0
This can be rewritten as X2 = (2 - X1)/2 = 1 - X1/2
Points above this line satisfy -2 + X1 + 2X2 > 0
Points below this line satisfy -2 + X1 + 2X2 < 0
plot_decision_boundary <- function() {
# Create a grid of points for visualization
x_range <- seq(-5, 5, length.out = 200)
y_range <- seq(-3, 7, length.out = 200)
grid <- expand.grid(X1 = x_range, X2 = y_range)
# Calculate whether each point is inside or outside the circle
grid$region <- ifelse((1 + grid$X1)^2 + (2 - grid$X2)^2 <= 4, "≤ 4 (Red)", "> 4 (Blue)")
# Create the circle boundary
theta <- seq(0, 2*pi, length.out = 100)
circle_x <- -1 + 2 * cos(theta) # Center at (-1, 2) with radius 2
circle_y <- 2 + 2 * sin(theta)
circle_df <- data.frame(x = circle_x, y = circle_y)
# Create data for the specific points in part (c)
points <- data.frame(
X1 = c(0, -1, 2, 3),
X2 = c(0, 1, 2, 8),
label = c("(0,0)", "(-1,1)", "(2,2)", "(3,8)")
)
# Calculate their classification
points$value <- (1 + points$X1)^2 + (2 - points$X2)^2
points$class <- ifelse(points$value > 4, "Blue", "Red")
# Plot
p <- ggplot() +
# Add shaded regions
geom_tile(data = grid, aes(x = X1, y = X2, fill = region), alpha = 0.3) +
scale_fill_manual(values = c("> 4 (Blue)" = "blue", "≤ 4 (Red)" = "red"),
name = "(1 + X1)² + (2 - X2)²") +
# Add the circle boundary
geom_path(data = circle_df, aes(x = x, y = y), color = "black", linewidth = 1) +
# Add the center point
geom_point(aes(x = -1, y = 2), color = "black", size = 2) +
annotate("text", x = -1, y = 2, label = "(-1,2)", hjust = -0.3, vjust = -0.3) +
# Add the specific points from part (c)
geom_point(data = points, aes(x = X1, y = X2, color = class), size = 3) +
scale_color_manual(values = c("Blue" = "blue", "Red" = "red")) +
geom_text(data = points, aes(x = X1, y = X2, label = label), hjust = -0.3, vjust = -0.3) +
# Add plot details
coord_cartesian(xlim = c(-5, 5), ylim = c(-3, 7)) +
labs(x = "X1",
y = "X2",
title = "Decision Boundary: (1 + X1)² + (2 - X2)² = 4",
subtitle = "Blue region: (1 + X1)² + (2 - X2)² > 4 | Red region: (1 + X1)² + (2 - X2)² ≤ 4") +
theme_minimal()
return(p)
}
# Generate the plot for parts (a) and (b)
cat("Parts (a) and (b): Plotting the curve and regions\n")
## Parts (a) and (b): Plotting the curve and regions
plot <- plot_decision_boundary()
print(plot)
classify_points <- function() {
points <- data.frame(
X1 = c(0, -1, 2, 3),
X2 = c(0, 1, 2, 8),
label = c("(0,0)", "(-1,1)", "(2,2)", "(3,8)")
)
# Calculate values for each point
points$value <- (1 + points$X1)^2 + (2 - points$X2)^2
points$class <- ifelse(points$value > 4, "Blue", "Red")
# Create a table with detailed calculations
result <- data.frame(
Point = points$label,
Calculation = paste0(
"(1 + ", points$X1, ")² + (2 - ", points$X2, ")² = ",
"(", 1 + points$X1, ")² + (", 2 - points$X2, ")² = ",
round(points$value, 2)
),
Class = points$class
)
return(result)
}
# Classify the given points
cat("\nPart (c): Classification of specific points\n")
##
## Part (c): Classification of specific points
results <- classify_points()
print(results)
## Point Calculation Class
## 1 (0,0) (1 + 0)² + (2 - 0)² = (1)² + (2)² = 5 Blue
## 2 (-1,1) (1 + -1)² + (2 - 1)² = (0)² + (1)² = 1 Red
## 3 (2,2) (1 + 2)² + (2 - 2)² = (3)² + (0)² = 9 Blue
## 4 (3,8) (1 + 3)² + (2 - 8)² = (4)² + (-6)² = 52 Blue
explain_linearity <- function() {
cat("Part (d): Demonstration of linearity in transformed feature space\n\n")
cat("Starting with the decision boundary: (1 + X1)² + (2 - X2)² = 4\n\n")
cat("Step 1: Expand the squares\n")
cat("(1 + X1)² + (2 - X2)² = 4\n")
cat("(1 + 2X1 + X1²) + (4 - 4X2 + X2²) = 4\n\n")
cat("Step 2: Combine terms\n")
cat("1 + 2X1 + X1² + 4 - 4X2 + X2² = 4\n\n")
cat("Step 3: Rearrange to standard form\n")
cat("1 + 2X1 + X1² + 4 - 4X2 + X2² - 4 = 0\n")
cat("1 + 2X1 + X1² - 4X2 + X2² + 0 = 0\n\n")
cat("This can be written as a linear combination of {1, X1, X1², X2, X2²}:\n")
cat("β₀·1 + β₁·X1 + β₂·X1² + β₃·X2 + β₄·X2² = 0\n")
cat("Where: β₀ = 1, β₁ = 2, β₂ = 1, β₃ = -4, β₄ = 1\n\n")
cat("Therefore, while the decision boundary is non-linear in the original feature\n")
cat("space {X1, X2}, it is linear in the transformed feature space {1, X1, X1², X2, X2²}.\n")
}
# For part (d): Explain the linearity in transformed feature space
cat("\n")
explain_linearity()
## Part (d): Demonstration of linearity in transformed feature space
##
## Starting with the decision boundary: (1 + X1)² + (2 - X2)² = 4
##
## Step 1: Expand the squares
## (1 + X1)² + (2 - X2)² = 4
## (1 + 2X1 + X1²) + (4 - 4X2 + X2²) = 4
##
## Step 2: Combine terms
## 1 + 2X1 + X1² + 4 - 4X2 + X2² = 4
##
## Step 3: Rearrange to standard form
## 1 + 2X1 + X1² + 4 - 4X2 + X2² - 4 = 0
## 1 + 2X1 + X1² - 4X2 + X2² + 0 = 0
##
## This can be written as a linear combination of {1, X1, X1², X2, X2²}:
## β₀·1 + β₁·X1 + β₂·X1² + β₃·X2 + β₄·X2² = 0
## Where: β₀ = 1, β₁ = 2, β₂ = 1, β₃ = -4, β₄ = 1
##
## Therefore, while the decision boundary is non-linear in the original feature
## space {X1, X2}, it is linear in the transformed feature space {1, X1, X1², X2, X2²}.