- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要平滑一些模拟数据,但是当要平滑的模拟纵坐标大多是相同的值时,偶尔会遇到问题。这是最简单情况的一个可重现的小示例。
> x <- 0:50
> y <- rep(0,51)
> loess.smooth(x,y)
Error in simpleLoess(y, x, w, span, degree, FALSE, FALSE, normalize = FALSE, :
NA/NaN/Inf in foreign function call (arg 1)
loess(y~x)
,
lowess(x,y)
, 以及它们在 MATLAB 中的类似物在这个例子中产生了预期的结果,没有错误。我正在使用
loess.smooth
在这里是因为我需要在一定数量的点上评估估计值。根据文档,我相信
loess.smooth
和
loess
使用相同的估计函数,但前者是处理评估点的“辅助函数”。错误似乎来自 C 函数:
> traceback()
3: .C(R_loess_raw, as.double(pseudovalues), as.double(x), as.double(weights),
as.double(weights), as.integer(D), as.integer(N), as.double(span),
as.integer(degree), as.integer(nonparametric), as.integer(order.drop.sqr),
as.integer(sum.drop.sqr), as.double(span * cell), as.character(surf.stat),
temp = double(N), parameter = integer(7), a = integer(max.kd),
xi = double(max.kd), vert = double(2 * D), vval = double((D +
1) * max.kd), diagonal = double(N), trL = double(1),
delta1 = double(1), delta2 = double(1), as.integer(0L))
2: simpleLoess(y, x, w, span, degree, FALSE, FALSE, normalize = FALSE,
"none", "interpolate", control$cell, iterations, control$trace.hat)
1: loess.smooth(x, y)
loess
还调用
simpleLoess
,但似乎是不同的论点。当然,如果您将足够多的 y 值改变为非零值,
loess.smooth
运行没有错误,但我需要程序在最极端的情况下运行。
loess.smooth
,而不是其他函数,会产生此错误并找到此问题的解决方案。 loess
找到解决方法但仍在评估可能与向量 x 不同的指定数量的点的估计。例如,我可能只想使用 x <- seq(0,50,10)
在平滑中,但在 x <- 0:50
处评估估计值.据我所知,使用 predict
使用新的数据框将无法正确处理这种情况,但如果我在那里遗漏了什么,请告诉我。 最佳答案
第 1 部分:
这需要一些跟踪,但如果你这样做:loess.smooth(x, y, family = "guassian")
该模型将适合。这是由于 loess.smooth
的不同默认值引起的和 loess
;前者有family = c("symmetric", "gaussian")
而后者则相反。如果您搜索 loess
的代码和 loess.smooth
, 你会看到当 family = "gaussian"
iterations
设置为 1
.否则取值 loess.control()$iterations
.如果您在 simpleLoess
中进行迭代,以下函数调用返回 NaN
的向量:
pseudovalues <- .Fortran(R_lowesp, as.integer(N), as.double(y),
as.double(z$fitted.values), as.double(weights), as.double(robust),
integer(N), pseudovalues = double(N))$pseudovalues
zz <- .C(R_loess_raw, as.double(pseudovalues), as.double(x),
as.double(weights), as.double(weights), as.integer(D),
as.integer(N), as.double(span), as.integer(degree),
as.integer(nonparametric), as.integer(order.drop.sqr),
as.integer(sum.drop.sqr), as.double(span * cell),
as.character(surf.stat), temp = double(N), parameter = integer(7),
a = integer(max.kd), xi = double(max.kd), vert = double(2 *
D), vval = double((D + 1) * max.kd), diagonal = double(N),
trL = double(1), delta1 = double(1), delta2 = double(1),
as.integer(0L))
family = "gaussian"
在您的
loess.smooth
称呼。
loess.smooth
的默认值与
loess
的不同,例如为
'span'
和
'degree'
.所以请仔细检查您想要拟合的模型并调整相关功能的默认值。
DF <- data.frame(x = 0:50, y = rep(0,51))
mod <- loess(y ~ x, data = DF)
pred <- predict(mod, newdata = data.frame(x = c(-1, 10, 15, 55)))
mod2 <- loess(y ~ x, data = DF, control = loess.control(surface = "direct"))
pred2 <- predict(mod2, newdata = data.frame(x = c(-1, 10, 15, 55)))
> pred
1 2 3 4
NA 0 0 NA
> pred2
1 2 3 4
0 0 0 0
predict
有什么问题事实上,这里是。
?try
和
?tryCatch
您可以环绕 loess 拟合函数(
loess.smooth
说),如果
loess.smooth
中出现错误,这将允许计算继续遇到了。
try
的输出或
tryCatch
通过包含类似的内容(如果您在循环中执行此操作:
mod <- try(loess.smooth(x, y))
if(inherits(mod, "try-error"))
next
## if here, model work, do something with `mod`
try
或
tryCatch
带配件通过
loess
并使用
predict
对于这样的问题。
关于r - 使用 `loess.smooth` 但不是 `loess` 或 `lowess` 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4645682/
我目前正在修改我提供的一些 R 代码以满足我的需要。 情况如下: 我们正在绘制约 200 行。然后他们使用 LOWESS 获得最佳拟合曲线。 现在看起来像这样: lines(lowess(x.lowe
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭去年。 Im
我正在写一篇研究论文,想从我的数据集中绘制类似这样的东西 基本上它是一个 Matlab 散点图,带有一条低曲线,显示每个 bin 的平均值 我已经在 Google 上搜索了 2 天,但找不到解决方案或
如何在 python 中拟合局部加权回归,以便它可以用于预测新数据? 有 statsmodels.nonparametric.smoothers_lowess.lowess,但它只返回原始数据集的估计
在数据上绘制一条线(例如,拟合回归线或非参数 LOWESS 线)通常很有帮助。同样,当变量被混淆时,绘制 x1, y 数据按 x2 分层通常很有帮助。但是,我不太清楚如何使用 ?coplot 将两者结
这个问题在这里已经有了答案: gdata - "object is masked..." [duplicate] (1 个回答) 关闭 6 年前。 当我使用 R ROCR 包时,我收到错误消息。 L
对于 R 的“统计”包的 LOWESS 回归线的置信区间 (CI),我没有找到任何令人满意的答案: plot(cars, main = "lowess(cars)") lines(lowess(car
如何使用 Python 查找和绘制如下所示的 LOWESS 曲线? 我知道 LOWESS 实现 in statsmodels ,但它似乎无法给我 95% 的置信区间线,我可以在它们之间进行遮蔽。 Se
我正在使用 lowess拟合两个变量之间回归的函数 x和 y .现在我想知道新值 x 处的拟合值.例如,如何在 x=2.5 处找到拟合值在下面的例子中。我知道 loess可以做到,但我想重现某人的情节
这是我使用 Plotly lib 时的 LOWESS Smoothing 片段: 现在,我知道使用 Plotly,如果我将鼠标悬停在线上,它会为我提供 y 趋势值: 问题:我不知道如何打印这些趋势值。
我需要平滑一些模拟数据,但是当要平滑的模拟纵坐标大多是相同的值时,偶尔会遇到问题。这是最简单情况的一个可重现的小示例。 > x y loess.smooth(x,y) Error in simpl
我正在比较两个图表,每个图表都叠加了一条非参数低(w)ess曲线。问题是曲线看起来非常不同,尽管它们的参数(例如跨度)是相同的。 y<-rnorm(100) x<-rgamma(100,2,2) qp
黄土的这个结果如何使用 loess 拟合转载于lowess ? 黄土代码: > data = data.frame(x=c(1,0.5,3,4,5,5.5,6,7), y=c(10, 25, 38,
我是一名优秀的程序员,十分优秀!