1

我正在尝试为我的情节创建一个自定义的 LOESS 面板功能。基本上它应该做的与简单的panel.loessor相同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)

结果是这样的:

R输出

显然,这是行不通的。我收到以下错误消息:Error using packet n numeric 'envir' arg not of length one。我调试它的尝试(例如使用 that print(yy))效果不佳,所以我不知道从哪里开始寻找解决方案。

关于造成这种情况的任何想法?

4

1 回答 1

1

经过一些(小)修改后,我使用了这个脚本

xyplot(foo ~ bar | thing, data = d.f, panel =  function(x,y){
    xx <- do.breaks(range(x), 49)
    mod <- loess(y~x)
    yy <- predict(mod, newdata = data.frame(foo=xx))
    panel.xyplot(x,y)
    panel.lines(x=xx, y= yy)
})

我更改了点数,因为有一些警告信息。它是否有效,这是您所期望的吗?

于 2015-03-12T11:31:16.463 回答