LOESS warnings/errors 与 R 中的跨度相关

LOESS warnings/errors related to span in R

我是 运行 R 中的 LOESS 回归,我的一些较小的数据集遇到了警告。

警告消息:

1: In simpleLoess(y, x, w, span, degree = degree, parametric = parametric,  :   pseudoinverse used at -2703.9

2: In simpleLoess(y, x, w, span, degree = degree, parametric = parametric,  :   neighborhood radius 796.09

3: In simpleLoess(y, x, w, span, degree = degree, parametric = parametric,  :   reciprocal condition number  0

4: In simpleLoess(y, x, w, span, degree = degree, parametric = parametric,  :   There are other near singularities as well. 6.1623e+005

这些错误在此处的另一个 post 中讨论: Understanding loess errors in R .

这些警告似乎与为 LOESS 回归设置的跨度有关。我正在尝试对其他数据集应用类似的方法,其中可接受的平滑跨度的参数在 0.3 到 0.6 之间。在某些情况下,我可以调整跨度以避免这些问题,但在其他数据集中,必须将跨度增加到可接受的水平之外以避免 errors/warnings.

很好奇这些warnings具体是什么意思,这会不会是regression可用的情况,但是需要注意的是出现了这些warnings,或者regression是完全无效的。

这是一个有问题的数据集示例:

Period  Value   Total1  Total2
-2950   0.104938272 32.4    3.4  
-2715   0.054347826 46  2.5  
-2715   0.128378378 37  4.75  
-2715   0.188679245 39.75   7.5  
-3500   0.245014245 39  9.555555556  
-3500   0.163120567 105.75  17.25  
-3500   0.086956522 28.75   2.5  
-4350   0.171038825 31.76666667 5.433333333  
-3650   0.143798024 30.36666667 4.366666667  
-4350   0.235588972 26.6    6.266666667  
-3500   0.228840125 79.75   18.25  
-4933   0.154931973 70  10.8452381  
-4350   0.021428571 35  0.75  
-3500   0.0625  28  1.75  
-2715   0.160714286 28  4.5  
-2715   0.110047847 52.25   5.75  
-3500   0.176923077 32.5    5.75  
-3500   0.226277372 34.25   7.75  
-2715   0.132625995 188.5   25

这是没有换行符的数据

Period  Value   Total1  Total2
-2950   0.104938272 32.4    3.4
-2715   0.054347826 46  2.5
-2715   0.128378378 37  4.75
-2715   0.188679245 39.75   7.5
-3500   0.245014245 39  9.555555556
-3500   0.163120567 105.75  17.25
-3500   0.086956522 28.75   2.5
-4350   0.171038825 31.76666667 5.433333333
-3650   0.143798024 30.36666667 4.366666667
-4350   0.235588972 26.6    6.266666667
-3500   0.228840125 79.75   18.25
-4933   0.154931973 70  10.8452381
-4350   0.021428571 35  0.75
-3500   0.0625  28  1.75
-2715   0.160714286 28  4.5
-2715   0.110047847 52.25   5.75
-3500   0.176923077 32.5    5.75
-3500   0.226277372 34.25   7.75
-2715   0.132625995 188.5   25

这是我使用的代码:

Analysis <- read.csv(file.choose(), header = T)
plot(Value ~ Period, Analysis)
a <- order(Analysis$Period)
Analysis.lo <- loess(Value ~ Period, Analysis, weights = Total1)
pred <- predict(Analysis.lo, se = TRUE)
lines(Analysis$Period[a], pred$fit[a], col="red", lwd=3)
lines(Analysis$Period[a], pred$fit[a] - qt(0.975, pred$df)*pred$se[a],lty=2)
lines(Analysis$Period[a], pred$fit[a] + qt(0.975,pred$df)*pred$se[a],lty=2)

感谢您的帮助,如果需要任何其他信息,请告诉我。

发出警告是因为 loess 的算法发现数值困难,因为 Period 有一些值重复了相对较多的次数,如您所见从你的情节还有:

table(Analysis$Period)

在这方面,Period 实际上表现得像一个离散变量(一个因子),而不是连续变量,因为它需要进行适当的平滑处理。添加一些抖动会删除警告:

Analysis <- read.table(header = T,text="Period  Value   Total1  Total2
-2950   0.104938272 32.4    3.4
-2715   0.054347826 46  2.5
-2715   0.128378378 37  4.75
-2715   0.188679245 39.75   7.5
-3500   0.245014245 39  9.555555556
-3500   0.163120567 105.75  17.25
-3500   0.086956522 28.75   2.5
-4350   0.171038825 31.76666667 5.433333333
-3650   0.143798024 30.36666667 4.366666667
-4350   0.235588972 26.6    6.266666667
-3500   0.228840125 79.75   18.25
-4933   0.154931973 70  10.8452381
-4350   0.021428571 35  0.75
-3500   0.0625  28  1.75
-2715   0.160714286 28  4.5
-2715   0.110047847 52.25   5.75
-3500   0.176923077 32.5    5.75
-3500   0.226277372 34.25   7.75
-2715   0.132625995 188.5   25")

table(Analysis$Period)    
Analysis$Period <- jitter(Analysis$Period, factor=0.2)

plot(Value ~ Period, Analysis)
a <- order(Analysis$Period)
Analysis.lo <- loess(Value ~ Period, Analysis, weights = Total1)
pred <- predict(Analysis.lo, se = TRUE)
lines(Analysis$Period[a], pred$fit[a], col="red", lwd=3)
lines(Analysis$Period[a], pred$fit[a] - qt(0.975, pred$df)*pred$se[a],lty=2)
lines(Analysis$Period[a], pred$fit[a] + qt(0.975,pred$df)*pred$se[a],lty=2)

增加span参数有"squashing out"的效果,沿Period轴,重复值出现的地方;对于小数据集,您需要进行大量压缩以补偿重复 Periods.

的堆积

从实际的角度来看,我通常仍然相信回归,可能是在检查了图形输出之后。但我绝对不会增加 span 来实现压缩:为此目的使用少量 jitter 会好得多; span 应由其他考虑因素决定,例如 Period 数据的整体分布等。