基于R语言在特定点进行边界内外的插值与外推
Alright, let's work through how to do interpolation and extrapolation for your dataset in R. I'll walk you through practical, adaptable code examples since you mentioned handling points both inside and outside your boundary values.
First, let's formalize your x vector and add example y values (replace these with your actual response data—you'll need paired x/y values for interpolation):
# Your provided x sequence x <- c(1600L, 1650L, 1675L, 1700L, 1725L, 1775L, 1800L, 1825L, 1850L, 1875L, 1880L, 1885L, 1900L, 1920L, 1925L, 1930L, 1935L, 1940L, 1945L, 1950L, 1955L, 1960L, 1965L, 1975L, 1980L, 1985L, 1990L, 1995L, 2000L, 2005L, 2010L, 2015L, 2020L, 2025L, 2030L, 2035L, 2040L, 2045L, 2050L, 2055L, 2060L, 2065L, 2070L, 2075L, 2080L, 2085L, 2090L, 2095L, 2100L, 2105L, 2110L, 2115L, 2120L, 2125L, 2130L, 2135L, 2140L, 2145L, 2150L, 2155L, 2160L, 2165L, 2170L, 2175L, 2180L, 2185L, 2190L, 2195L) # Example y values (replace with your real data!) set.seed(123) # For reproducibility y <- rnorm(length(x), mean = 50 + 0.02*x, sd = 2)
First, clarify which points count as your "boundary"—for this example, let's say we want to:
- Interpolate values inside the range
1950to2100 - Extrapolate values outside that range (e.g.,
1500or2200)
We'll use these test points to demonstrate:
test_x <- c(1500, 1975, 2050, 2200)
Linear Interpolation & Extrapolation
This is the simplest approach. Use approxfun for interpolation inside the data range, and either extend a linear fit or use boundary values for extrapolation:
Option 1: Use Nearest Boundary Values for Extrapolation
# Create linear interpolation function; rule=2 uses nearest edge for out-of-range points linear_fun <- approxfun(x, y, method = "linear", rule = 2) # Generate predictions linear_pred <- linear_fun(test_x) # View results data.frame(Test_X = test_x, Linear_Prediction = linear_pred)
Option 2: True Linear Extrapolation
If you want to extend the linear trend from your data instead of clamping to boundary values, fit a linear model:
# Fit linear regression to your data lm_model <- lm(y ~ x) # Predict for test points lm_pred <- predict(lm_model, newdata = data.frame(x = test_x)) # View results data.frame(Test_X = test_x, Linear_Extrapolation = lm_pred)
Smoothing Spline (For Curved Interpolation/Extrapolation)
If your data has a non-linear trend, splines will give smoother interpolations and can extrapolate beyond your boundaries:
# Create natural spline function (extrapolates smoothly) spline_fun <- splinefun(x, y, method = "natural") # Generate predictions spline_pred <- spline_fun(test_x) # View results data.frame(Test_X = test_x, Spline_Prediction = spline_pred)
If you need fine-grained control (e.g., only interpolate between specific boundaries and use different extrapolation rules for low/high ends), build a custom function:
# Define your custom boundaries lower_bound <- 1950 upper_bound <- 2100 custom_interp_extrap <- function(new_x) { pred <- numeric(length(new_x)) # Interpolate inside boundary using splines inside_idx <- new_x >= lower_bound & new_x <= upper_bound pred[inside_idx] <- splinefun( x[x >= lower_bound & x <= upper_bound], y[x >= lower_bound & x <= upper_bound], method = "natural" )(new_x[inside_idx]) # Extrapolate below lower bound using linear fit on nearby edge points low_edge_idx <- x >= lower_bound - 50 & x <= lower_bound low_lm <- lm(y[low_edge_idx] ~ x[low_edge_idx]) pred[new_x < lower_bound] <- predict(low_lm, newdata = data.frame(x = new_x[new_x < lower_bound])) # Extrapolate above upper bound using linear fit on nearby edge points high_edge_idx <- x >= upper_bound & x <= upper_bound + 50 high_lm <- lm(y[high_edge_idx] ~ x[high_edge_idx]) pred[new_x > upper_bound] <- predict(high_lm, newdata = data.frame(x = new_x[new_x > upper_bound])) return(pred) } # Test the custom function custom_pred <- custom_interp_extrap(test_x) data.frame(Test_X = test_x, Custom_Prediction = custom_pred)
Pick the method that best matches your data's trend—linear is great for simple relationships, splines work for curved data, and the custom function lets you tailor behavior exactly to your boundaries.
内容的提问来源于stack exchange,提问作者Novic

