[英]Is there a function for not-a-knot cubic splines?
我需要在我的 R 腳本中使用“非結”三次樣條進行插值。
盡管有一些用於樣條的 R 包,但它們似乎都沒有考慮“非結”類型,即使據說它是一種相當“流行”的三次樣條類型,並且它在 Matlab 中可用。
我擔心“非結”三次樣條曲線還有另一個名稱。 這是一個三次樣條,其中兩個額外條件是關於第二個和最后一個節點中的三次導數連續性(而不是像自然三次樣條那樣將一階導數固定在端點節點處,或其他選擇)。
通過挖掘一點,似乎這是在pracma::interp1
中實現的
從?pracma::interp1
插值以找到“yi”,即向量“xi”中點處的基礎函數的值。 ... 方法 `spline' 使用 Moler 等人的樣條方法,與 Matlab 的同名選項相同,但與 R 的樣條函數略有不同。
這沒有提到“not-a-knot”,但我通過使用sos::findFn("not-a-knot")
到了那里,這把我帶到了gsignal::pulstran()
。 該函數的“樣條”方法被描述為“使用非結結束條件的樣條插值”; method=
參數直接傳遞給pracma::interp1
。
這是一個比較示例:
pracma::interp1
pracma:::.ml.spline
(不做任何參數檢查,排序,重復刪除......但確實允許外推。我不知道為什么pracma::interp1
做出這個限制。這個包是在 r上開發的-forge但我沒有看到郵件列表等,您可以聯系維護者或自己破解interp1
...)RcppOctave
包,但它已存檔,我無法輕松地通過remotes::install_version("RcppOctave", version = "0.18-1")
安裝該包很多配置漂移...)library(splines)
library(pracma)
x <- 1:6
y <- c(16, 18, 21, 17, 15, 12)
xi0 <- seq(1, 6, by = 0.1)
xi1 <- seq(1, 7, by = 0.1)
ys1 <- interp1(x, y, xi0, method = "spline")
ys2 <- predict(interpSpline(x, y), xi1)$y
ys3 <- spline(x, y, xout = xi1)$y
ys4 <- pracma:::.ml.spline(x, y, xi1)
ys5 <- scan("octave_out.mat", comment = "#") ## see below
png("spline2.png")
par(las=1, bty = "l", cex = 1.5, lwd = 2)
plot(x,y, xlim = range(xi1), ylim = range(ys4),
pch = 16, col = "gray")
cvec <- c(palette()[c(1,2,4,6)],
## make last color semi-transparent so we can see the overlay
adjustcolor(palette()[5], alpha.f = 0.4))
matlines(xi1, cbind(ys2,ys3,ys4, ys5) , col = cvec[1:4], lwd = 2)
lines(xi0, ys1, lwd = 3, col = cvec[5])
legend("bottomleft",
lty = c(1:4, 1),
col = cvec,
legend = c("interpSpline", "spline", ".ml.spine", "octave", "interp1"))
dev.off()
.ml.spline
輸出一致interp1
在數據范圍內一致(無外推)Octave 代碼(在單獨的 shell 中運行)
x = 1:6;
y = [16, 18, 21, 17, 15, 12];
xi1 = linspace(1, 7, 61);
pp = interp1(x, y, 'spline', 'pp');
yy = ppval(pp, xi1);
save octave_out.mat yy
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.