gpt4 book ai didi

r - 了解 R 中的 loess 错误

转载 作者:行者123 更新时间:2023-12-04 18:06:12 55 4
gpt4 key购买 nike

我正在尝试使用 loess 拟合模型,但出现错误,例如“3 处使用的伪逆”、“邻域半径 1”和“倒数条件数 0”。这是一个 MWE:

x = 1:19
y = c(NA,71.5,53.1,53.9,55.9,54.9,60.5,NA,NA,NA
,NA,NA,178.0,180.9,180.9,NA,NA,192.5,194.7)
fit = loess(formula = y ~ x,
control = loess.control(surface = "direct"),
span = 0.3, degree = 1)
x2 = seq(0,20,.1)
library(ggplot2)
qplot(x=x2
,y=predict(fit, newdata=data.frame(x=x2))
,geom="line")

我意识到我可以通过选择更大的跨度值来修复这些错误。但是,我正在尝试使这种拟合自动化,因为我有大约 100,000 个与此类似的时间序列(每个长度约为 20)。有没有一种方法可以自动选择一个跨度值来防止这些错误,同时仍然为数据提供相当灵活的拟合?或者,谁能解释这些错误的含义?我在 loess() 和 simpleLoess() 函数中做了一些探索,但在调用 C 代码时我放弃了。

最佳答案

比较 fit$fittedy .你会注意到你的回归有问题。选择足够的带宽,否则它只会插入数据。数据点太少时,线性函数在小带宽上表现为常数,并触发共线性。因此,您会看到警告伪逆、奇点的错误。如果你使用 degree=0 你就不会看到这样的错误或 ksmooth . span 的一种可理解的、数据驱动的选择用于交叉验证,您可以在 Cross Validated 询问.

> fit$fitted
[1] 71.5 53.1 53.9 55.9 54.9 60.5 178.0 180.9 180.9 192.5 194.7
> y
[1] NA 71.5 53.1 53.9 55.9 54.9 60.5 NA NA NA NA NA 178.0
[14] 180.9 180.9 NA NA 192.5 194.7

您会看到过度拟合(完美拟合),因为在您的模型中,参数数量与有效样本数量一样多。
fit
#Call:
#loess(formula = y ~ x, span = 0.3, degree = 1, control = loess.control(surface = "direct"))

#Number of Observations: 11
#Equivalent Number of Parameters: 11
#Residual Standard Error: Inf

或者,您也可以使用自动化 geom_smooth . (再次设置 geom_smooth(span=0.3) 抛出警告)
ggplot(data=data.frame(x, y), aes(x, y)) + 
geom_point() + geom_smooth()

enter image description here

关于r - 了解 R 中的 loess 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27528918/

55 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com