1

这是一个 data.frame,其第三个“列”实际上是一个矩阵:

pred.Alb <- structure(list(Age = 
   c(20, 30, 40, 50, 60, 70, 80, 20, 30, 40, 
   50, 60, 70, 80), Sex = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
   2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Male", "Female"), 
   class = "factor"), 
pred = structure(c(4.34976914720261, 4.3165897157342, 4.2834102842658, 
4.23952109360855, 4.15279286619591, 4.05535487959442, 3.95791689299294, 
4.02417706540447, 4.05661037005163, 4.08904367469879, 4.0942071858864, 
3.9902915232358, 3.85910606712565, 3.72792061101549, 4.37709246711838, 
4.38914906337186, 4.40120565962535, 4.3964228776405, 4.32428258270227, 
4.23530290952571, 4.14632323634915, 4.3, 4.3, 4.3, 4.28809523809524, 
4.22857142857143, 4.15714285714286, 4.08571428571429, 4.59781730640631, 
4.59910124381436, 4.60038518122242, 4.58132673532165, 4.48089875618564, 
4.36012839374081, 4.23935803129598, 4.39298701298701, 4.39711229946524, 
4.40123758594347, 4.39484310896076, 4.34636957813428, 4.28737628384687, 
4.22838298955946), .Dim = c(14L, 3L), .Dimnames = list(c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", 
"13", "14"), c("tau= 0.10", "tau= 0.25", "tau= 0.50")))), 
  .Names = c("Age", "Sex", "pred"), out.attrs =
  structure(list(dim = structure(c(7L, 2L), .Names = c("Age", "Sex")), 
  dimnames = structure(list(Age = c("Age=20", 
        "Age=30", "Age=40", "Age=50", "Age=60", "Age=70", "Age=80"), 
Sex = c("Sex=Male", "Sex=Female")),
.Names = c("Age", "Sex"))), 
.Names = c("dim", "dimnames")), row.names = c(NA, -14L), 
 class = "data.frame")

它是使用以下代码创建的:

require(rms) # also loads Hmisc
require(quantreg) # might also get loaded by rms
rqAlb10fit2 <- rq(BL_ALBUMIN ~ rcs(Age,3) *Sex , data=redBan, 
                                    tau= c(0.1, 0.25, 0.5) )
pred.Alb <- expand.grid(Age=seq(20,80,by=10), Sex=c("Male", "Female") ) 
pred.Alb$pred <- predict(rqAlb10fit2, 
  newdata=expand.grid(Age=seq(20,80,by=10), Sex=c("Male", "Female") ) )

我想要一个按性别和 tau 水平预测的线图。我可以得到一个点图:

xyplot(pred~Age|Sex, data=pred.Alb, type="p")

当我添加 type="l" 时,线条来回摆动,连接tau.

我怀疑它是否重要,但在 Mac 10.7.5 上使用 quantreg_4.96/rms_3.6-3/Hmisc_3.10-1 运行。如果你想给我看一个经典主题的 ggplot 解决方案,我也可以,只是我对 ggplot2 不是很好,而且 Harrell 的 rms 包与 lattice 配合使用。

4

2 回答 2

5

问题似乎是y当它被传递到面板函数时失去它的维度属性,成为一个简单的向量。它仍然继续并绘制,循环x以匹配y的长度,你看不到type="p",但可以在 when type="l"

y这是一个自定义面板函数,它通过首先转换回矩阵然后panel.xyplot在其每一列上单独调用来完成您想要的功能:

panel.matplot <- function(x,y,...) {
    y <- matrix(y, nrow=length(x))
    apply(y, 2, function(Y) panel.xyplot(x,Y, ...))
}

xyplot(pred~Age|Sex, data=pred.Alb, type="l", panel=panel.matplot)

在此处输入图像描述


顺便说一句:在这种情况下,我经常发现在面板函数调用的“内部”进行探索很有用。一种简单的方法是构造一个包含browser()调用的虚拟面板函数。例如,这是我在这种情况下发现问题的方式:

xyplot(pred~Age|Sex, data=pred.Alb, type="l",
       panel = function(x,y,...) browser())
Browse[2]> x
# [1] 20 30 40 50 60 70 80
Browse[2]> y
#  [1] 4.349769 4.316590 4.283410 4.239521 4.152793 4.055355 3.957917 4.377092
#  [9] 4.389149 4.401206 4.396423 4.324283 4.235303 4.146323 4.597817 4.599101
# [17] 4.600385 4.581327 4.480899 4.360128 4.239358

...此时所需的修复(a)非常明显并且(b)可以从现有的浏览器调用中测试出来。

于 2013-06-09T21:20:46.037 回答
2

您可以通过重塑为 long 并使用groups参数来做到这一点xyplot

pred2 <- as.data.frame(pred.Alb$pred)
varying=names(pred2)
pred2$Age <- pred.Alb$Age
pred2$Sex <- pred.Alb$Sex
pred2.long <- reshape(pred2, direction='long', varying=varying, sep='= ')

xyplot(tau~Age|Sex, data=pred2.long, type="l", groups=time)

在此处输入图像描述

于 2013-06-09T21:49:48.557 回答