r - 使用 lattice 和 quantreg 绘制多分位数回归线

标签 r plot lattice quantreg

我想像这样绘制几条分位数回归线(quantreg 包):

 library(quantreg)
 data(engel)
 attach(engel)
 plot(income, foodexp, cex = 0.25, type = "n",
      xlab = "Household Income", ylab = "Food Expenditure")
 points(income, foodexp, cex = 0.5, col = "blue")
 abline(rq(foodexp ~ income, tau = 0.5), col = "blue")
 abline(lm(foodexp ~ income), lty = 2, col = "red")
 taus <- c(0.05, 0.1, 0.25, 0.75, 0.9, 0.95)
 for (i in 1:length(taus)) {
   abline(rq(foodexp ~ income, tau = taus[i]),
          col = "gray")
 }
 detach(engel)

但是使用 lattice,我只尝试了一个回归但不起作用:

require(lattice)
xyplot(foodexp~income , data =engel, 
   type = c("g"),
   auto.key=list(x=.8,y=.35,cex=.8,cex.title=.8, title="", points=TRUE), 
   scales=list(tck=-1),ylab=list("Food Expenditure",font=3),
   xlab=list("Household Income",font=3),
   panel=function(x,y){
     panel.xyplot(x,y)
     panel.grid()
     panel.abline(rq(foodexp ~ income, tau = 0.5))}
)

最佳答案

使用 lattice,您应该只在面板函数中使用 yx

enter image description here

xyplot(foodexp~income , data =engel, 
       type = c("g"),
       auto.key=list(x=.8,y=.35,cex=.8,cex.title=.8, title="", points=TRUE), 
       scales=list(tck=-1),ylab=list("Food Expenditure",font=3),
       xlab=list("Household Income",font=3),
       panel=function(x,y,...){
         panel.xyplot(x,y)
         panel.grid()
         panel.abline(rq(y ~ x, tau = 0.5))
         panel.points(x, y, cex = 0.5, col = "blue")
         panel.abline(rq(y ~ x, tau = 0.5), col = "blue")
         panel.abline(lm(y ~ x), lty = 2, col = "red")
         taus <- c(0.05, 0.1, 0.25, 0.75, 0.9, 0.95)
         for (i in 1:length(taus)) {
           panel.abline(rq(y ~ x, tau = taus[i]),
                  col = "gray")
         }

       }
)

关于r - 使用 lattice 和 quantreg 绘制多分位数回归线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15582321/

相关文章:

r - 数据表1.8.1。 : “DT1 = DT2” is not the same as DT1 = copy(DT2)?

matlab - 更改轴框默认线宽

r - Shiny 的 ggvis react 图

r - R中的谱图中的x轴和y轴标签

r - 优雅地结合连续和因子栅格

r - 如何在R中的点阵图形中包装长标题?

r - 如何禁用 R 中的第二个悬停信息?

javascript - 在 R Shiny 的数据表中添加工具提示

r - LaTeX 表格中的链接文本与 knitr/kable