在R中拟合黄土线

在R中拟合黄土线,r,simulation,predict,loess,R,Simulation,Predict,Loess,在R中使用leash()和predict()时遇到一些问题。我使用了以下代码来模拟数据: Overall=0.6 RR=1.1 Noise=0.05 x=seq(from=0.01, to=10, by=0.01) logRR=log(RR) logBeta0=log(Overall) linear.pred = logBeta0 + (logRR*x) + rnorm(length(x), 0, Noise*sqrt(x)) linear.pred.high = logBeta0 +

在R中使用
leash()
predict()
时遇到一些问题。我使用了以下代码来模拟数据:

Overall=0.6
RR=1.1
Noise=0.05

x=seq(from=0.01, to=10, by=0.01) 
logRR=log(RR)
logBeta0=log(Overall)


linear.pred = logBeta0 + (logRR*x) + rnorm(length(x), 0, Noise*sqrt(x))
linear.pred.high = logBeta0 + (logRR*15) + rnorm(length(x), 0, Noise/5)

PoissonRate <- function (x) ifelse(x<=9, exp(linear.pred), exp(linear.pred.high))

xyplot(PoissonRate(x)~x) #the shape of the 'raw' data


loess_fit <- loess(x~PoissonRate(x))
lines(predict(loess_fit), col = "black")
总体=0.6
RR=1.1
噪音=0.05
x=序列(从=0.01到=10,由=0.01)
logRR=log(RR)
logBeta0=对数(总体)
linear.pred=logBeta0+(logRR*x)+rnorm(长度(x),0,噪声*sqrt(x))
线性.pred.high=logBeta0+(logRR*15)+rnorm(长度(x),0,噪声/5)

泊松比至少在我的理解中,你不应该在
xyplot
调用之外调用
llines()
(如果这就是你所说的
lines()
)<代码>?llines
具有:

Description:

     These functions are intended to replace common low level
     traditional graphics functions, primarily for use in panel
     functions.
因此,一种选择是按照它的建议去做,动态地构建自己的面板功能。以下是一个例子:

set.seed(1)
dat <- data.frame(pr = PoissonRate(x), x = x)
loess_fit <- loess(pr ~ x, data = dat)

xyplot(PoissonRate(x) ~ x,
  panel = function(x, y, ...) {
    panel.xyplot(x, y, ...)
    llines(dat$x, predict(loess_fit), col.line = "red")
  })

不能使用
向晶格图形绘图添加数据。请参见
lline()
。您正在从前到后绘制数据。如果模型为
x~PoissonRate(x)
,则在
xyplot
中绘制
x~PoissonRate(x)
。在
llines()
中,您需要给出
x
y
参数-但您只给出
predict()
的输出,一个向量。。。
## following on from the above
pred <- with(dat, data.frame(x = seq(min(x), max(x), length = 100)))
pred <- transform(pred, pr = predict(loess_fit, newdata = x))

xyplot(PoissonRate(x) ~ x,
  panel = function(x, y, ...) {
    panel.xyplot(x, y, ...)
    with(pred, llines(x, pr, col.line = "red"))
  })