labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal()
# Step 2: Train polynomial regression models with increasing degrees
train_errors <- c()
test_errors <- c()
degrees <- 1:110  # Polynomial degrees from 1 to 29
for (degree in degrees) {
# Create polynomial features
poly_model <- lm(y ~ poly(X, degree, raw=TRUE), data = df_train)
# Predictions on the training set
y_train_pred <- predict(poly_model, df_train)
# Predictions on the testing set
y_test_pred <- predict(poly_model, df_test)
# Calculate Mean Squared Error
train_errors <- c(train_errors, mean((df_train$y - y_train_pred)^2))
test_errors <- c(test_errors, mean((df_test$y - y_test_pred)^2))
}
# Step 3: Plot the training and test errors
df <- data.frame(
Degree = degrees[-c(1:3)],
TrainError = train_errors[-c(1:3)],
TestError = test_errors[-c(1:3)]
)
ggplot(df, aes(x = Degree)) +
geom_line(aes(y = TrainError, color = "Training Error")) +
geom_line(aes(y = TestError, color = "Test Error")) +
geom_point(aes(y = TrainError, color = "Training Error")) +
geom_point(aes(y = TestError, color = "Test Error")) +
geom_vline(xintercept = 10, linetype = "dashed", color = "red",
label = "Interpolation Threshold") +
labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal()
df_train_pred = tibble::tibble(X = df_train$X, y = y_train_pred)  |> dplyr::arrange(X)
df_test = tibble::tibble(X = df_test$X, y = df_test$y)  |> dplyr::arrange(X)
df_test_pred = tibble::tibble(X = df_test$X, y = y_test_pred)  |> dplyr::arrange(X)
ggplot() +
geom_point(aes(X, y), data = df_train) +
geom_line(aes(X, y), data = df_train_pred) +
geom_point(aes(X, y), data = df_test, color = 'red') +
geom_line(aes(X, y), data = df_test_pred, color = 'red') +
coord_cartesian(ylim = c(-2, 2))
# Step 2: Train polynomial regression models with increasing degrees
train_errors <- c()
test_errors <- c()
degrees <- 1:80  # Polynomial degrees from 1 to 29
for (degree in degrees) {
# Create polynomial features
poly_model <- lm(y ~ poly(X, degree, raw=TRUE), data = df_train)
# Predictions on the training set
y_train_pred <- predict(poly_model, df_train)
# Predictions on the testing set
y_test_pred <- predict(poly_model, df_test)
# Calculate Mean Squared Error
train_errors <- c(train_errors, mean((df_train$y - y_train_pred)^2))
test_errors <- c(test_errors, mean((df_test$y - y_test_pred)^2))
}
# Step 3: Plot the training and test errors
df <- data.frame(
Degree = degrees[-c(1:3)],
TrainError = train_errors[-c(1:3)],
TestError = test_errors[-c(1:3)]
)
ggplot(df, aes(x = Degree)) +
geom_line(aes(y = TrainError, color = "Training Error")) +
geom_line(aes(y = TestError, color = "Test Error")) +
geom_point(aes(y = TrainError, color = "Training Error")) +
geom_point(aes(y = TestError, color = "Test Error")) +
geom_vline(xintercept = 10, linetype = "dashed", color = "red",
label = "Interpolation Threshold") +
labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal()
df_train_pred = tibble::tibble(X = df_train$X, y = y_train_pred)  |> dplyr::arrange(X)
df_test = tibble::tibble(X = df_test$X, y = df_test$y)  |> dplyr::arrange(X)
df_test_pred = tibble::tibble(X = df_test$X, y = y_test_pred)  |> dplyr::arrange(X)
ggplot() +
geom_point(aes(X, y), data = df_train) +
geom_line(aes(X, y), data = df_train_pred) +
geom_point(aes(X, y), data = df_test, color = 'red') +
geom_line(aes(X, y), data = df_test_pred, color = 'red') +
coord_cartesian(ylim = c(-2, 2))
# Step 3: Plot the training and test errors
df <- data.frame(
Degree = degrees[-c(1:4)],
TrainError = train_errors[-c(1:4)],
TestError = test_errors[-c(1:4)]
)
ggplot(df, aes(x = Degree)) +
geom_line(aes(y = TrainError, color = "Training Error")) +
geom_line(aes(y = TestError, color = "Test Error")) +
geom_point(aes(y = TrainError, color = "Training Error")) +
geom_point(aes(y = TestError, color = "Test Error")) +
geom_vline(xintercept = 10, linetype = "dashed", color = "red",
label = "Interpolation Threshold") +
labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal()
# Step 3: Plot the training and test errors
df <- data.frame(
Degree = degrees,
TrainError = train_errors,
TestError = test_errors
)
ggplot(df, aes(x = Degree)) +
geom_line(aes(y = TrainError, color = "Training Error")) +
geom_line(aes(y = TestError, color = "Test Error")) +
geom_point(aes(y = TrainError, color = "Training Error")) +
geom_point(aes(y = TestError, color = "Test Error")) +
geom_vline(xintercept = 10, linetype = "dashed", color = "red",
label = "Interpolation Threshold") +
labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal() +
coord_cartesian(xlim(0, 10))
ggplot(df, aes(x = Degree)) +
geom_line(aes(y = TrainError, color = "Training Error")) +
geom_line(aes(y = TestError, color = "Test Error")) +
geom_point(aes(y = TrainError, color = "Training Error")) +
geom_point(aes(y = TestError, color = "Test Error")) +
geom_vline(xintercept = 10, linetype = "dashed", color = "red",
label = "Interpolation Threshold") +
labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal() +
coord_cartesian(xlim = c(0, 10))
ggplot(df, aes(x = Degree)) +
geom_line(aes(y = TrainError, color = "Training Error")) +
geom_line(aes(y = TestError, color = "Test Error")) +
geom_point(aes(y = TrainError, color = "Training Error")) +
geom_point(aes(y = TestError, color = "Test Error")) +
geom_vline(xintercept = 10, linetype = "dashed", color = "red",
label = "Interpolation Threshold") +
labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal() +
coord_cartesian(xlim = c(0, 20))
ggplot(df, aes(x = Degree)) +
geom_line(aes(y = TrainError, color = "Training Error")) +
geom_line(aes(y = TestError, color = "Test Error")) +
geom_point(aes(y = TrainError, color = "Training Error")) +
geom_point(aes(y = TestError, color = "Test Error")) +
geom_vline(xintercept = 10, linetype = "dashed", color = "red",
label = "Interpolation Threshold") +
labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal() +
coord_cartesian(xlim = c(0, 40))
ggplot(df, aes(x = Degree)) +
geom_line(aes(y = TrainError, color = "Training Error")) +
geom_line(aes(y = TestError, color = "Test Error")) +
geom_point(aes(y = TrainError, color = "Training Error")) +
geom_point(aes(y = TestError, color = "Test Error")) +
geom_vline(xintercept = 10, linetype = "dashed", color = "red",
label = "Interpolation Threshold") +
labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal() +
coord_cartesian(xlim = c(5, 40))
ggplot(df, aes(x = Degree)) +
geom_line(aes(y = TrainError, color = "Training Error")) +
geom_line(aes(y = TestError, color = "Test Error")) +
geom_point(aes(y = TrainError, color = "Training Error")) +
geom_point(aes(y = TestError, color = "Test Error")) +
geom_vline(xintercept = 10, linetype = "dashed", color = "red",
label = "Interpolation Threshold") +
labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal() +
coord_cartesian(xlim = c(5, 80))
ggplot(df, aes(x = Degree)) +
geom_line(aes(y = TrainError, color = "Training Error")) +
geom_line(aes(y = TestError, color = "Test Error")) +
geom_point(aes(y = TrainError, color = "Training Error")) +
geom_point(aes(y = TestError, color = "Test Error")) +
geom_vline(xintercept = 10, linetype = "dashed", color = "red",
label = "Interpolation Threshold") +
labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal() +
coord_cartesian(xlim = c(10, 20))
ggplot(df, aes(x = Degree)) +
geom_line(aes(y = TrainError, color = "Training Error")) +
geom_line(aes(y = TestError, color = "Test Error")) +
geom_point(aes(y = TrainError, color = "Training Error")) +
geom_point(aes(y = TestError, color = "Test Error")) +
geom_vline(xintercept = 10, linetype = "dashed", color = "red",
label = "Interpolation Threshold") +
labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal() +
coord_cartesian(xlim = c(10, 20), ylim = c(0, 0.1))
ggplot(df, aes(x = Degree)) +
geom_line(aes(y = TrainError, color = "Training Error")) +
geom_line(aes(y = TestError, color = "Test Error")) +
geom_point(aes(y = TrainError, color = "Training Error")) +
geom_point(aes(y = TestError, color = "Test Error")) +
geom_vline(xintercept = 10, linetype = "dashed", color = "red",
label = "Interpolation Threshold") +
labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal() +
coord_cartesian(xlim = c(5, 20), ylim = c(0, 0.1))
ggplot(df, aes(x = Degree)) +
geom_line(aes(y = TrainError, color = "Training Error")) +
geom_line(aes(y = TestError, color = "Test Error")) +
geom_point(aes(y = TrainError, color = "Training Error")) +
geom_point(aes(y = TestError, color = "Test Error")) +
geom_vline(xintercept = 10, linetype = "dashed", color = "red",
label = "Interpolation Threshold") +
labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal() +
coord_cartesian(xlim = c(5, 100), ylim = c(0, 0.1))
ggplot(df, aes(x = Degree)) +
geom_line(aes(y = TrainError, color = "Training Error")) +
geom_line(aes(y = TestError, color = "Test Error")) +
geom_point(aes(y = TrainError, color = "Training Error")) +
geom_point(aes(y = TestError, color = "Test Error")) +
geom_vline(xintercept = 10, linetype = "dashed", color = "red",
label = "Interpolation Threshold") +
labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal() +
coord_cartesian(xlim = c(5, 25), ylim = c(0, 0.1))
ggplot(df, aes(x = Degree)) +
geom_line(aes(y = TrainError, color = "Training Error")) +
geom_line(aes(y = TestError, color = "Test Error")) +
geom_point(aes(y = TrainError, color = "Training Error")) +
geom_point(aes(y = TestError, color = "Test Error")) +
geom_vline(xintercept = 10, linetype = "dashed", color = "red",
label = "Interpolation Threshold") +
labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal() +
coord_cartesian(xlim = c(5, 25), ylim = c(0.01, 0.1))
ggplot(df, aes(x = Degree)) +
geom_line(aes(y = TrainError, color = "Training Error")) +
geom_line(aes(y = TestError, color = "Test Error")) +
geom_point(aes(y = TrainError, color = "Training Error")) +
geom_point(aes(y = TestError, color = "Test Error")) +
geom_vline(xintercept = 10, linetype = "dashed", color = "red",
label = "Interpolation Threshold") +
labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal() +
coord_cartesian(xlim = c(5, 25), ylim = c(0.01, 0.025))
# Step 1: Generate a synthetic dataset
set.seed(42)
df_orig =
tibble::tibble(
X = seq(0, 10, length.out = 1000),  # Features: 100 points in the range [0, 10]
y = sin(X) + rnorm(1000, sd = 0.1)  # Target: sin(x) with noise
)
# Split the dataset into training and testing sets
train_indices <- sample(1:100, 80)
df_train = df_orig[train_indices,] |> dplyr::arrange(X)
df_test  = df_orig[-train_indices,] |> dplyr::arrange(X)
# Step 2: Train polynomial regression models with increasing degrees
train_errors <- c()
test_errors <- c()
degrees <- 1:80  # Polynomial degrees from 1 to 29
for (degree in degrees) {
# Create polynomial features
poly_model <- lm(y ~ poly(X, degree, raw=TRUE), data = df_train)
# Predictions on the training set
y_train_pred <- predict(poly_model, df_train)
# Predictions on the testing set
y_test_pred <- predict(poly_model, df_test)
# Calculate Mean Squared Error
train_errors <- c(train_errors, mean((df_train$y - y_train_pred)^2))
test_errors <- c(test_errors, mean((df_test$y - y_test_pred)^2))
}
# Step 3: Plot the training and test errors
df <- data.frame(
Degree = degrees,
TrainError = train_errors,
TestError = test_errors
)
ggplot(df, aes(x = Degree)) +
geom_line(aes(y = TrainError, color = "Training Error")) +
geom_line(aes(y = TestError, color = "Test Error")) +
geom_point(aes(y = TrainError, color = "Training Error")) +
geom_point(aes(y = TestError, color = "Test Error")) +
geom_vline(xintercept = 10, linetype = "dashed", color = "red",
label = "Interpolation Threshold") +
labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal() +
coord_cartesian(xlim = c(5, 25), ylim = c(0.01, 0.025))
ggplot(df, aes(x = Degree)) +
geom_line(aes(y = TrainError, color = "Training Error")) +
geom_line(aes(y = TestError, color = "Test Error")) +
geom_point(aes(y = TrainError, color = "Training Error")) +
geom_point(aes(y = TestError, color = "Test Error")) +
geom_vline(xintercept = 10, linetype = "dashed", color = "red",
label = "Interpolation Threshold") +
labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal()
df_train_pred = tibble::tibble(X = df_train$X, y = y_train_pred)  |> dplyr::arrange(X)
df_test = tibble::tibble(X = df_test$X, y = df_test$y)  |> dplyr::arrange(X)
df_test_pred = tibble::tibble(X = df_test$X, y = y_test_pred)  |> dplyr::arrange(X)
ggplot() +
geom_point(aes(X, y), data = df_train) +
geom_line(aes(X, y), data = df_train_pred) +
geom_point(aes(X, y), data = df_test, color = 'red') +
geom_line(aes(X, y), data = df_test_pred, color = 'red') +
coord_cartesian(ylim = c(-2, 2))
# Split the dataset into training and testing sets
train_indices <- sample(1:1000, 800)
df_train = df_orig[train_indices,] |> dplyr::arrange(X)
df_test  = df_orig[-train_indices,] |> dplyr::arrange(X)
# Step 2: Train polynomial regression models with increasing degrees
train_errors <- c()
test_errors <- c()
degrees <- 1:80  # Polynomial degrees from 1 to 29
for (degree in degrees) {
# Create polynomial features
poly_model <- lm(y ~ poly(X, degree, raw=TRUE), data = df_train)
# Predictions on the training set
y_train_pred <- predict(poly_model, df_train)
# Predictions on the testing set
y_test_pred <- predict(poly_model, df_test)
# Calculate Mean Squared Error
train_errors <- c(train_errors, mean((df_train$y - y_train_pred)^2))
test_errors <- c(test_errors, mean((df_test$y - y_test_pred)^2))
}
# Step 3: Plot the training and test errors
df <- data.frame(
Degree = degrees,
TrainError = train_errors,
TestError = test_errors
)
ggplot(df, aes(x = Degree)) +
geom_line(aes(y = TrainError, color = "Training Error")) +
geom_line(aes(y = TestError, color = "Test Error")) +
geom_point(aes(y = TrainError, color = "Training Error")) +
geom_point(aes(y = TestError, color = "Test Error")) +
geom_vline(xintercept = 10, linetype = "dashed", color = "red",
label = "Interpolation Threshold") +
labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal()
df_train_pred = tibble::tibble(X = df_train$X, y = y_train_pred)  |> dplyr::arrange(X)
df_test = tibble::tibble(X = df_test$X, y = df_test$y)  |> dplyr::arrange(X)
df_test_pred = tibble::tibble(X = df_test$X, y = y_test_pred)  |> dplyr::arrange(X)
ggplot() +
geom_point(aes(X, y), data = df_train) +
geom_line(aes(X, y), data = df_train_pred) +
geom_point(aes(X, y), data = df_test, color = 'red') +
geom_line(aes(X, y), data = df_test_pred, color = 'red') +
coord_cartesian(ylim = c(-2, 2))
ggplot(df, aes(x = Degree)) +
geom_line(aes(y = TrainError, color = "Training Error")) +
geom_line(aes(y = TestError, color = "Test Error")) +
geom_point(aes(y = TrainError, color = "Training Error")) +
geom_point(aes(y = TestError, color = "Test Error")) +
geom_vline(xintercept = 10, linetype = "dashed", color = "red",
label = "Interpolation Threshold") +
labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal() +
coord_cartesian(xlim = c(5, 25), ylim = c(0.01, 0.025))
# Step 2: Train polynomial regression models with increasing degrees
train_errors <- c()
test_errors <- c()
degrees <- 1:160  # Polynomial degrees from 1 to 29
for (degree in degrees) {
# Create polynomial features
poly_model <- lm(y ~ poly(X, degree, raw=TRUE), data = df_train)
# Predictions on the training set
y_train_pred <- predict(poly_model, df_train)
# Predictions on the testing set
y_test_pred <- predict(poly_model, df_test)
# Calculate Mean Squared Error
train_errors <- c(train_errors, mean((df_train$y - y_train_pred)^2))
test_errors <- c(test_errors, mean((df_test$y - y_test_pred)^2))
}
# Step 3: Plot the training and test errors
df <- data.frame(
Degree = degrees,
TrainError = train_errors,
TestError = test_errors
)
ggplot(df, aes(x = Degree)) +
geom_line(aes(y = TrainError, color = "Training Error")) +
geom_line(aes(y = TestError, color = "Test Error")) +
geom_point(aes(y = TrainError, color = "Training Error")) +
geom_point(aes(y = TestError, color = "Test Error")) +
geom_vline(xintercept = 10, linetype = "dashed", color = "red",
label = "Interpolation Threshold") +
labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal() +
coord_cartesian(xlim = c(5, 25), ylim = c(0.01, 0.025))
df_train_pred = tibble::tibble(X = df_train$X, y = y_train_pred)  |> dplyr::arrange(X)
df_test = tibble::tibble(X = df_test$X, y = df_test$y)  |> dplyr::arrange(X)
df_test_pred = tibble::tibble(X = df_test$X, y = y_test_pred)  |> dplyr::arrange(X)
ggplot() +
geom_point(aes(X, y), data = df_train) +
geom_line(aes(X, y), data = df_train_pred) +
geom_point(aes(X, y), data = df_test, color = 'red') +
geom_line(aes(X, y), data = df_test_pred, color = 'red') +
coord_cartesian(ylim = c(-2, 2))
ggplot(df, aes(x = Degree)) +
geom_line(aes(y = TrainError, color = "Training Error")) +
geom_line(aes(y = TestError, color = "Test Error")) +
geom_point(aes(y = TrainError, color = "Training Error")) +
geom_point(aes(y = TestError, color = "Test Error")) +
geom_vline(xintercept = 10, linetype = "dashed", color = "red",
label = "Interpolation Threshold") +
labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal()
# Step 2: Train polynomial regression models with increasing degrees
train_errors <- c()
test_errors <- c()
degrees <- 1:320  # Polynomial degrees from 1 to 29
for (degree in degrees) {
# Create polynomial features
poly_model <- lm(y ~ poly(X, degree, raw=TRUE), data = df_train)
# Predictions on the training set
y_train_pred <- predict(poly_model, df_train)
# Predictions on the testing set
y_test_pred <- predict(poly_model, df_test)
# Calculate Mean Squared Error
train_errors <- c(train_errors, mean((df_train$y - y_train_pred)^2))
test_errors <- c(test_errors, mean((df_test$y - y_test_pred)^2))
}
ggplot(df, aes(x = Degree)) +
geom_line(aes(y = TrainError, color = "Training Error")) +
geom_line(aes(y = TestError, color = "Test Error")) +
geom_point(aes(y = TrainError, color = "Training Error")) +
geom_point(aes(y = TestError, color = "Test Error")) +
geom_vline(xintercept = 10, linetype = "dashed", color = "red",
label = "Interpolation Threshold") +
labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal() +
coord_cartesian(xlim = c(5, 25), ylim = c(0.01, 0.025))
ggplot(df, aes(x = Degree)) +
geom_line(aes(y = TrainError, color = "Training Error")) +
geom_line(aes(y = TestError, color = "Test Error")) +
geom_point(aes(y = TrainError, color = "Training Error")) +
geom_point(aes(y = TestError, color = "Test Error")) +
geom_vline(xintercept = 10, linetype = "dashed", color = "red",
label = "Interpolation Threshold") +
labs(title = "Double Descent of Test Error",
x = "Polynomial Degree",
y = "Mean Squared Error",
color = "Error Type") +
theme_minimal() +
coord_cartesian(xlim = c(5, 80), ylim = c(0.01, 0.025))
install.packages(c("corrplot", "cpp11", "curl", "data.table", "digest", "minqa", "mvtnorm", "openssl", "RcppEigen", "rmarkdown", "waldo", "xfun"))
install.packages(c("boot", "nlme"), lib="/opt/homebrew/Cellar/r/4.4.1/lib/R/library")
install.packages(c("abind", "bit", "bit64", "broom", "car", "curl", "Deriv", "evaluate", "ggrepel", "glue", "jsonlite", "microbenchmark", "mvtnorm", "openssl", "patchwork", "pbdZMQ", "ps", "ragg", "timeDate", "tinytex"))
setwd("~/Desktop/TAPS_paper/natcom_rev2/figs/source_data")
tmp = readr::read_rds('SuppFig5.rds')
tmp
tmp = readr::read_rds('SuppFig4.rds')
tmp
readRDS('Fig2.rds')
