Created
January 9, 2025 20:13
-
-
Save mikelove/16cd54942f8435b1a94c09ae20118d3c to your computer and use it in GitHub Desktop.
splines and GP disagreeing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
Author
mikelove
commented
Jan 9, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment