我正在尝试为我的情节创建一个自定义的 LOESS 面板功能。基本上它应该做的与简单的panel.loess
or相同type = "smooth"
。原因是稍后我想让它变得更复杂一些,用上面的方法不容易实现。但是,它失败了。
这是一个 MWE(其中一些大致基于 Sarkar 的 Lattice 书第 235 页的示例:
library(lattice)
set.seed(9)
foo <- rexp(100)
bar <- rexp(100)
thing <- factor(rep(c("this", "that"), times = 50))
d.f <- data.frame(foo = foo, bar = bar, thing = thing)
loess.c <- function(x) {
mod <- loess(foo ~ bar, data = x)
return(mod)
}
panel.cloess <- function(x, n = 50, ...){
panel.xyplot(x, ...)
lfit <- loess.c(x)
xx <- do.breaks(range(x$x), n)
yy <- predict(lfit, newdata = data.frame(bar = xx),
se = TRUE)
print(yy) # doesn't do anything
panel.lines(x = xx, y = yy$fit, ...)
}
xyplot(foo ~ bar | thing, data = d.f,
panel = panel.cloess)
结果是这样的:
显然,这是行不通的。我收到以下错误消息:Error using packet n numeric 'envir' arg not of length one
。我调试它的尝试(例如使用 that print(yy)
)效果不佳,所以我不知道从哪里开始寻找解决方案。
关于造成这种情况的任何想法?