Skip to content

Instantly share code, notes, and snippets.

@mikelove
Created January 9, 2025 20:13
Show Gist options
  • Save mikelove/16cd54942f8435b1a94c09ae20118d3c to your computer and use it in GitHub Desktop.
Save mikelove/16cd54942f8435b1a94c09ae20118d3c to your computer and use it in GitHub Desktop.
splines and GP disagreeing
# Created by chat GPT
# Load necessary libraries
library(splines)
library(GPfit)
# Set seed for reproducibility
set.seed(123)
# Generate non-uniform data
x <- c(rnorm(50, mean = -1, sd = 0.3), rnorm(50, mean = 1, sd = 0.3)) # Two modes
x <- sort(x) # Sort x for visualization
y <- sin(2 * pi * x) + rnorm(length(x), mean = 0, sd = 0.2) # Sinusoidal curve with constant scatter
# Normalize x for GP fitting
x_scaled <- (x - min(x)) / (max(x) - min(x))
# Fit a natural spline
spline_fit <- lm(y ~ ns(x, df = 5))
# Fit a Gaussian Process
gp_model <- GP_fit(X = matrix(x_scaled, ncol = 1), Y = y)
# Generate predictions
x_pred <- seq(min(x), max(x), length.out = 500) # Dense grid for smooth lines
x_pred_scaled <- (x_pred - min(x)) / (max(x) - min(x)) # Normalize x_pred
# Spline predictions
spline_pred <- predict(spline_fit, newdata = data.frame(x = x_pred))
# Gaussian Process predictions
gp_pred <- predict(gp_model, matrix(x_pred_scaled, ncol = 1))
# Plot the data
plot(x, y, pch = 16, col = "blue", main = "Spline and Gaussian Process Fit (Non-Uniform Data)",
xlab = "x", ylab = "y", ylim = c(-2, 2))
# Add spline fit
lines(x_pred, spline_pred, col = "red", lwd = 2, lty = 2)
# Add Gaussian Process fit
lines(x_pred, gp_pred$Y_hat, col = "green", lwd = 2)
# Add legend
legend("topright", legend = c("Scatter", "Spline Fit", "GP Fit"),
col = c("blue", "red", "green"), pch = c(16, NA, NA),
lty = c(NA, 2, 1), lwd = c(NA, 2, 2), bty = "n")
@mikelove
Copy link
Author

mikelove commented Jan 9, 2025

Uploading Screenshot 2025-01-09 at 3.13.13 PM.png…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment