从平滑对象中获取值

Obtain values from smoothing objects

library(stats)
x = 1:9
y1 = c(1.577544, 3.128399, 4.233402, 5.735423, 4.338297, 4.338656, 4.643472, 4.525278, 4.710244)
y2 = c(1.395986, 1.221213, 4.157185, 3.722469, 3.482445, 3.996337, 4.318460, 4.293782, 4.441601)
dat <- data.frame(x = c(x, x), y = c(y1, y2), ID = c(rep(1, 9), rep(2, 9)))

dat
   x        y ID
1  1 1.577544  1
2  2 3.128399  1
3  3 4.233402  1
4  4 5.735423  1
5  5 4.338297  1
6  6 4.338656  1
7  7 4.643472  1
8  8 4.525278  1
9  9 4.710244  1
10 1 1.395986  2
11 2 1.221213  2
12 3 4.157185  2
13 4 3.722469  2
14 5 3.482445  2
15 6 3.996337  2
16 7 4.318460  2
17 8 4.293782  2
18 9 4.441601  2

我这里有一个简单的数据集,我用两种类型的平滑曲线绘制了它。

plot(y ~ x, data = dat, xlim = c(0, 9))
subjects <- unique(dat$ID)
for (subject in subjects){
  with(dat, lines(x[ID == subject], (y)[ID == subject], col = "grey"))
}
with(dat, lines(smooth.spline(x, y), lwd = 2))
with(dat, lines(lowess(x, y), lwd = 2, col = "blue"))

我尝试了两条平滑曲线,第一条是三次样条,第二条是黄土。我的问题是,如何从这些平滑器中获取实际值?即实际的 x 和 y 坐标。

这是一个附带问题,但是除了这 2 种平滑方法之外,我还可以尝试使用其他什么方法来拟合我的数据?

我们可以得到如下数据:

# smooth spline data
data.frame(smooth.spline(dat$x, dat$y)[1:4])
#   x        y w      yin
# 1 1 1.494558 2 1.486765
# 2 2 2.650096 2 2.174806
# 3 3 3.700306 2 4.195294
# 4 4 4.255043 2 4.728946
# 5 5 4.324492 2 3.910371
# 6 6 4.318693 2 4.167497
# 7 7 4.377625 2 4.480966
# 8 8 4.456206 2 4.409530
# 9 9 4.553078 2 4.575923

注意: smooth.spline 适用于不同的 x 值,它将 return 9 行用于您的数据。

# lowes data
data.frame(lowess(dat$x, dat$y))
#    x        y
# 1  1 1.779179
# 2  1 1.779179
# 3  2 2.605521
# 4  2 2.605521
# 5  3 3.384997
# 6  3 3.384997
# 7  4 3.898521
# 8  4 3.898521
# 9  5 4.026922
# 10 5 4.026922
# 11 6 4.174382
# 12 6 4.174382
# 13 7 4.338500
# 14 7 4.338500
# 15 8 4.465401
# 16 8 4.465401
# 17 9 4.580555
# 18 9 4.580555