当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R predict.smooth.spline 通过平滑样条拟合进行预测


R语言 predict.smooth.spline 位于 stats 包(package)。

说明

预测新点处的平滑样条拟合,如果需要则返回导数。预测拟合与原始数据呈线性关系。

用法

## S3 method for class 'smooth.spline'
predict(object, x, deriv = 0, ...)

参数

object

来自 smooth.spline 的拟合。

x

x 的新值。

deriv

整数;所需导数的阶数。

...

传入或传出其他方法的进一步参数。

包含组件的列表

x

输入 x

y

x 处的拟合值或导数。

例子

require(graphics)

attach(cars)
cars.spl <- smooth.spline(speed, dist, df = 6.4)


## "Proof" that the derivatives are okay, by comparing with approximation
diff.quot <- function(x, y) {
  ## Difference quotient (central differences where available)
  n <- length(x); i1 <- 1:2; i2 <- (n-1):n
  c(diff(y[i1]) / diff(x[i1]), (y[-i1] - y[-i2]) / (x[-i1] - x[-i2]),
    diff(y[i2]) / diff(x[i2]))
}

xx <- unique(sort(c(seq(0, 30, by = .2), kn <- unique(speed))))
i.kn <- match(kn, xx)   # indices of knots within xx
op <- par(mfrow = c(2,2))
plot(speed, dist, xlim = range(xx), main = "Smooth.spline & derivatives")
lines(pp <- predict(cars.spl, xx), col = "red")
points(kn, pp$y[i.kn], pch = 3, col = "dark red")
mtext("s(x)", col = "red")
for(d in 1:3){
  n <- length(pp$x)
  plot(pp$x, diff.quot(pp$x,pp$y), type = "l", xlab = "x", ylab = "",
       col = "blue", col.main = "red",
       main = paste0("s" ,paste(rep("'", d), collapse = ""), "(x)"))
  mtext("Difference quotient approx.(last)", col = "blue")
  lines(pp <- predict(cars.spl, xx, deriv = d), col = "red")

  points(kn, pp$y[i.kn], pch = 3, col = "dark red")
  abline(h = 0, lty = 3, col = "gray")
}
detach(); par(op)

也可以看看

smooth.spline

相关用法


注:本文由纯净天空筛选整理自R-devel大神的英文原创作品 Predict from Smoothing Spline Fit。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。