代码之家  ›  专栏  ›  技术社区  ›  Leosar

使用晶格和quantreg绘制多条分位数回归线

  •  3
  • Leosar  · 技术社区  · 12 年前

    我想画几条分位数回归线( 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))}
    )
    
    1 回复  |  直到 9 年前
        1
  •  4
  •   agstudy    12 年前

    使用 lattice 你应该使用 y x 在面板功能内。

    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")
             }
    
           }
    )