我无法将黄土平滑加置信限拟合到残差散点图。
我的模型是高度~体重+胸围。为了检查胸围的线性度,我拟合了一个没有胸围的模型(即高度 ~ 体重),并根据胸围绘制了该模型的残差。到现在为止还挺好。然后我尝试使用 loess()
和 predict()
绘制黄土线,加上置信限。结果看起来像这样(在图片中我只绘制了中心线,但 CI 线看起来相同):
Loess problem in scatterplot
这些点是正确的(当我将 loess fit 绘制为看起来正确的点时),但由于某种原因,这条线没有按照我的预期绘制。我的代码如下:
# bf.red = data set; mod.nch = model; chestc = chest circumference;
# loess = loess model; lo.pred = predict loess
plot(bf.red$chestc #Chest circumference
,residuals(mod.nch)) #Residuals from height ~ weight model
loess <- loess(mod.nch$residuals ~ bf.red$chestc)
lo.pred <- predict(loess, se=T)
lines(bf.red$chestc,lo.pred$fit,pch=2) #Main line
lines(bf.red$chestc,lo.pred$fit+2*lo.pred$s, lty=2) #rough & ready CI
lines(bf.red$chestc,lo.pred$fit-2*lo.pred$s, lty=2)
希望你能帮忙。非常感谢,
垫
最佳答案
lines
按照它们出现的顺序连接点,
这有时是不可取的。
您可以按如下方式对它们进行排序:
i <- order(bf.red$chestc)
lines(bf.red$chestc[i], lo.pred$fit[i])
...
关于r - 黄土线绘制不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10007830/