如何使R的loess和R的lowess函数给出相同的结果?

6

如何使用 lowess 重现使用 loess 拟合的结果?

loess 代码:

> data = data.frame(x=c(1,0.5,3,4,5,5.5,6,7), y=c(10, 25, 38, 44.5, 500, 550, 600, 705))
> fit = loess("y ~ x", data=data)
> new_y = predict(fit, data$x)
> new_y
[1]   6.251022  28.272100  -2.840750 150.006042 481.927307 563.161187 640.825415 693.166150

lowess code:

> new_fit = lowess(data, f=0.8)
> new_fit
$x
[1] 0.5 1.0 3.0 4.0 5.0 5.5 6.0 7.0

$y
[1]  -4.330119  38.931265 255.000000 400.000000 500.000000 550.241949 601.519903 704.247275
这些结果有很大的不同。我正在尝试根据给定的x值获取新的适配y值。loess可以提供。
[1]   6.251022  28.272100  -2.840750 150.006042 481.927307 563.161187 640.825415 693.166150

lowess 返回:

[1]  -4.330119  38.931265 255.000000 400.000000 500.000000 550.241949 601.519903 704.247275

我该如何重写lowess函数调用,以便为新的y值提供与使用loess拟合和x值的predict函数非常相似的结果?谢谢。


一样的吗?不合理的要求?我不是投票关闭问题的人,但我理解这种冲动。 - IRTFM
@Dwin:显然,当我说“同样”的时候,我的意思是相似。-2.8和255并不相似。 - user248237
我认为期望两个不同的非参数平滑器在具有大于两个log10范围的小数据集中的所有点产生“相同结果”是不合理的。它们选择不同的“移位点”,而选择移位点通常是非常困难的。 - IRTFM
1
这里有一些信息:https://stat.ethz.ch/pipermail/bioconductor/2003-September/002337.html - Robert Kubrick
3个回答

4

你为什么需要这个?

我认为这在一般情况下是无法完成的。以下是一个具体案例,它可以给出几乎相同的结果,但由于某种原因,最后一个值仍然不同:

fit1 <- loess(y~x,data=data,span=0.8,degree=1)
predict(fit1)
#[1]  19.08622  12.55692  37.93642 188.35019 401.53528 506.87040 591.41854 740.71060

fit2 <- lowess(data$x,data$y,f=0.8,iter=0)
fit2

# $x
# [1] 0.5 1.0 3.0 4.0 5.0 5.5 6.0 7.0
# 
# $y
# [1]  12.55692  19.08622  37.93642 188.35019 401.53528 506.87040 591.41854 741.12187
#Note that lowess reorders by x.

但是你怎样才能让它类似于 loess 的默认特性呢?我想这样做是因为 lowess 更快,而且我没有使用 loess 的方程特性,只是一个简单的 y~x - user248237
也许你应该解释一下你更喜欢 loess 的哪些“特性”,而不是要求平等。 - IRTFM
@DWin:loess 在我的数据上运行良好。所以我只想要那个功能,但更快一些。假设它们应该给出类似的答案并不是不合理的... 默认值差异巨大令人困惑。 - user248237
如果在loess调用中添加 surface = "direct",则最后一个值是相同的。无论如何,在这种情况下曲线看起来不同,因为loess中默认的度数为2,而lowess仅实现了度数1。 - wmay

3
显然无法完成该操作的原因是两个函数在内部使用不同的算法,我能找到的唯一解释是由Brian Ripley在这里提供的:

http://www.mail-archive.com/r-help@stat.math.ethz.ch/msg63623.html

"It is not possible: the algorithms differ considerably in their details. 

...

In determining 'local' loess() uses a tricubic weighting, lowess() uses a 
uniform weighting (on reading the code)."

文档清楚地说明了如何选择与默认值相似的 span/f 参数,但由于使用不同的平滑算法,两个函数之间的所有其他参数都无法互相转换。


1
data = data.frame(x=c(1,0.5,3,4,5,5.5,6,7), y=c(10, 25, 38, 44.5, 500, 550, 600, 705))
 fit = loess("y ~ x", data=data)
 new_y = predict(fit, data$x)
 plot( data$x , new_y)
lines(lowess(data, f=0.8)$x, lowess(data, f=0.8)$y)
# Obviously lowess with f=0.8 is giving different smoothing

与较低的 f 值进行比较
lines(lowess(data, f=0.8)$x, lowess(data, f=0.5)$y, col="red") 

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接