如何用固定点拟合一条线? (在R中)

时间:2019-05-02 17:05:41

标签: r regression

我有一些点对,我想在R中为它们拟合一条线。我现在正在使用黄土,但是其预测也改变了原始点的值。我想找到一个可以生成具有这些固定点的直线的拟合,而不必是多项式拟合。

这是我的原始代码:

height = c(0, 11000, 20000, 32000, 47000, 51000, 71000, 84852)
beta = c(-0.0065, 0, 0.001, 0.0028, 0, -0.0028, -0.002, 0)

heightseq = seq(0, 84900, 100)
loesssmooth <- loess(beta ~ height, degree = 2)
pred = predict(loesssmooth, data.frame(height = heightseq), se = T)
plot(height, beta, col = "gray")
lines(heightseq, pred$fit, lwd = 2)

1 个答案:

答案 0 :(得分:2)

您正在寻找插值。

## linear interpolation
linear <- approx(height, beta, xout = heightseq)
plot(linear, type = "l"); points(height, beta, col = 8)

## natural cubic spline interpolation
ncs <- spline(height, beta, xout = heightseq, method = "natural")
plot(ncs, type = "l"); points(height, beta, col = 8)