为什么ggplot(.predict)不在R / rms软件包中绘制?

时间:2019-01-29 08:44:27

标签: r ggplot2 plot regression rms

请在此页面底部找到My Data中的ww1。我有一个预测变量(w$testw1$test),它是每个淋巴结总产量中阳性淋巴结的数量,即0到1之间。

我制作了两个模型-每个模型代表两个不同的疾病阶段。我希望将它们绘制在一起,但我所能得到的是:

enter image description here

该图是使用以下代码生成的:

library(ggplot2)
library(rms)
library(ggsci)

d <- datadist(w)
j <- options(datadist="d")

d1 <- datadist(w1)
j1 <- options(datadist="d1")

model <- cph(Surv(os.neck,mors)~rcs(test),data=w)
model1 <- cph(Surv(os.neck,mors)~rcs(test),data=w1)

ggplot(Predict(model1, fun=exp)) + scale_x_continuous(limits = c(0,0.80))

out <- bind_rows(fortify(Predict(model, fun=exp)), fortify(Predict(model1, fun=exp)), .id = "model")


ggplot(as.data.frame(out), aes(x = test)) +
  geom_ribbon(aes(fill = model, ymin = lower, ymax = upper), alpha = .05) +
  geom_line(aes(y = yhat, col = model)) + 
  scale_color_jco(name="", labels = c("A", "B")) + 
  scale_fill_jco(name="", labels = c("A", "B")) +
  geom_segment(aes(x = 0, y = 1, xend = 0.55, yend = 1), lty="dashed", size=0.1, alpha=0.75)

如您所见,该图在x轴上被切成0.35左右。我不明白为什么,我希望情节继续,因为有多个w $ test和w1 $ test值大于0.35。请注意,此代码是由30个示例中的dput()生成的,而不是整个队列的代码。

当我查看View(out)时,我意识到只有400个实体-每个modelmodel1中有200个实体。似乎每个实体编号200等于测试值截止0.35。请在这里查看:

Data structure

The first model

The second model

如何根据所有测试值完成图?

My data w and w1




w1 <- structure(list(sex = c(1L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 
                             0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 
                             1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L), mors = c(1L, 0L, 1L, 1L, 
                                                                           1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                           1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L), 
                     os = c(26.01, 138.68, 8.41, 29.63, 10, 19.59, 22.17, 63.52, 
                            21.44, 27.87, 40.81, 64.15, 43.24, 8.14, 17.01, 23.16, 24.38, 
                            25.61, 29.59, 29.9, 44.7, 44.52, 64.65, 93.06, 102.88, 140.79, 
                            157.07, 34.1, 81.15, 133.42, 24.57, 2.35, 3.44, 3.98, 4.8
                     ), os.beh = c(20.9, 138.68, NA, 20.24, 4.7, 13.01, 16.1, 
                                   45.17, 15.56, 20.24, NA, 45.47, 42.32, 2.49, 12.26, 19, 17.02, 
                                   18.6, NA, 20.83, 31.28, 39.86, 45.34, 67.02, 96.45, NA, NA, 
                                   32.99, 77.73, 131.98, 17.38, 0.79, 0.5, 2.23, 2.33), os.neck = c(18.2, 
                                                                                                    138.68, 5.42, 19.55, 6.6, 13.01, 16.1, 45.17, 14.29, 20.24, 
                                                                                                    28.85, 45.47, 42.32, 4.99, 11.73, 16.36, 17.02, 18.6, 20.53, 
                                                                                                    20.83, 31.28, 31.51, 45.31, 67.02, 73.07, 99.98, 112.03, 
                                                                                                    32.99, 80.46, 131.98, 17.38, 0.79, 2.04, 2.23, 2.3), rfs.neck = c(11.07, 
                                                                                                                                                                      10.32, 4.44, 17.25, 5.39, 5.49, 7.03, 33.61, 12.71, 5.49, 
                                                                                                                                                                      16.92, 14.52, 13.37, 4.14, 9.36, 11.53, 8.8, 9.59, 16.53, 
                                                                                                                                                                      8.34, 8.28, 18.17, 29.6, 10.32, 7.13, 22.51, 43.93, 24.74, 
                                                                                                                                                                      12.85, 28.94, NA, NA, NA, NA, NA), rfs.neck.tsite = c(11.07, 
                                                                                                                                                                                                                            10.32, NA, NA, NA, NA, 7.03, 33.61, NA, NA, NA, NA, NA, 4.14, 
                                                                                                                                                                                                                            9.36, 11.53, 8.8, 9.59, 16.53, 8.34, 8.28, 18.17, 29.6, 10.32, 
                                                                                                                                                                                                                            7.13, 22.51, 43.93, 24.74, 12.85, 28.94, NA, NA, NA, NA, 
                                                                                                                                                                                                                            NA), rfs.neck.nsite = c(11.07, 10.32, 4.44, 17.25, NA, NA, 
                                                                                                                                                                                                                                                    7.03, 33.61, 12.71, 5.49, 16.92, 14.52, 13.37, NA, NA, NA, 
                                                                                                                                                                                                                                                    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                                                                                                                                                                                                                                    NA, NA, NA, NA), rfs.neck.msite = c(11.07, 10.32, 4.44, 17.25, 
                                                                                                                                                                                                                                                                                        5.39, 5.49, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                                                                                                                                                                                                                                                                        NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                                                                                                                                                                                                                                                                        NA, NA), recidiv.tsite = c(1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 
                                                                                                                                                                                                                                                                                                                   0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                                                                                                                                                                                                   1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L), recidiv.nsite = c(1L, 
                                                                                                                                                                                                                                                                                                                                                                                      1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 
                                                                                                                                                                                                                                                                                                                                                                                      0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
                                                                                                                                                                                                                                                                                                                                                                                      0L, 0L, 0L, 0L), n.fjernet = c(19L, 7L, 28L, 2L, 15L, 12L, 
                                                                                                                                                                                                                                                                                                                                                                                                                     19L, 17L, 9L, 5L, 6L, 33L, 10L, 27L, 34L, 28L, 14L, NA, 8L, 
                                                                                                                                                                                                                                                                                                                                                                                                                     11L, 14L, 19L, 5L, 38L, 5L, 8L, 10L, 55L, 22L, 8L, 16L, 18L, 
                                                                                                                                                                                                                                                                                                                                                                                                                     6L, 23L, 5L), n.sygdom = c(2L, 0L, 2L, 0L, 9L, 1L, 1L, 1L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                0L, 1L, 0L, 4L, 0L, 4L, 0L, 0L, 0L, NA, 2L, 1L, 0L, 0L, 0L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 2L, 1L), stadie = c(1L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            2L, 2L, 1L, 1L), test = c(0.105263157894737, 0, 0.0714285714285714, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      0, 0.6, 0.0833333333333333, 0.0526315789473684, 0.0588235294117647, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      0, 0.2, 0, 0.121212121212121, 0, 0.148148148148148, 0, 0, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      0, NA, 0.25, 0.0909090909090909, 0, 0, 0, 0, 0.4, 0, 0, 0, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      0, 0, 0, 0, 0.333333333333333, 0.0869565217391304, 0.2)), .Names = c("sex", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           "mors", "os", "os.beh", "os.neck", "rfs.neck", "rfs.neck.tsite", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           "rfs.neck.nsite", "rfs.neck.msite", "recidiv.tsite", "recidiv.nsite", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           "n.fjernet", "n.sygdom", "stadie", "test"), row.names = c(3L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     4L, 5L, 12L, 29L, 40L, 59L, 61L, 69L, 74L, 78L, 82L, 86L, 95L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     101L, 108L, 109L, 113L, 115L, 116L, 120L, 121L, 128L, 130L, 134L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     139L, 141L, 144L, 150L, 153L, 156L, 159L, 164L, 165L, 166L), class = "data.frame")



w <- structure(list(sex = c(1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 
                            0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 
                            0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L), mors = c(1L, 1L, 1L, 1L, 
                                                                          1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                          1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 
                    os = c(16.56, 12.03, 12.08, 18.28, 17.28, 20.86, 23.48, 38.27, 
                           58.63, 96.18, 47.84, 25.7, 27.01, 45.38, 50.04, 70.21, 7.69, 
                           13.26, 15.2, 15.79, 15.74, 15.29, 18.59, 17.24, 22.35, 26.6, 
                           31.85, 31.94, 31.62, 33.52, 34.2, 55.92, 55.92, 67.27, 80.17
                    ), os.beh = c(NA, 7.28, NA, 11.17, 4.93, 64.33, 15.77, 26.94, 
                                  40.77, 69.09, 31.7, 17.05, 15.16, 32.3, 34.46, 49.81, 4.9, 
                                  5.47, 8.73, 9.92, 10.05, 10.77, 12.48, 12.52, 14.82, 18.19, 
                                  21.45, 27.05, NA, 27.01, 24.28, 40.11, 51.39, 62.11, 76.28
                    ), os.neck = c(10.97, 8.02, 8.77, 11.66, 12.55, 13.8, 15.77, 
                                   26.94, 40.77, 69.06, 46.82, 17.05, 18.76, 32.3, 34.46, 49.81, 
                                   4.9, 8.61, 9.92, 9.92, 10.05, 10.51, 12.48, 12.52, 14.82, 
                                   15.87, 21.45, 22.14, 22.97, 23.26, 24.28, 40.11, 40.11, 47.08, 
                                   52.14), rfs.neck = c(8.21, 6.7, 5.36, 7.72, 3.71, 5.39, 8.61, 
                                                        18.46, 9.56, 19.29, 12.42, 11.01, 18.14, 26.05, 15.87, 9.46, 
                                                        3.81, 7.79, 8.34, 8.61, 8.28, 9.79, 6.21, 5.36, 7.49, 9.56, 
                                                        16.07, 4.63, 13.31, 12.68, 20.67, 21.59, 30.16, 22.21, 0), 
                    rfs.neck.tsite = c(8.21, NA, NA, NA, NA, NA, NA, NA, NA, 
                                       NA, NA, 11.01, 18.14, 26.05, 15.87, 9.46, NA, NA, NA, NA, 
                                       NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
                    ), rfs.neck.nsite = c(8.21, 6.7, 5.36, 7.72, 3.71, 5.39, 
                                          8.61, 18.46, 9.56, 19.29, 12.42, NA, NA, NA, NA, NA, NA, 
                                          NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                          NA, NA, NA), rfs.neck.msite = c(8.21, 6.7, 5.36, 7.72, 3.71, 
                                                                          5.39, 8.61, 18.47, 9.56, 19.29, 12.42, 11.01, 18.14, 26.06, 
                                                                          15.87, 9.46, 3.81, 7.79, 8.35, 8.61, 8.28, 9.79, 6.21, 5.36, 
                                                                          7.49, 9.56, 16.07, 4.63, 13.31, 12.68, 20.67, 21.59, 30.16, 
                                                                          22.21, 0), recidiv.tsite = c(1L, 0L, 0L, 0L, 0L, 0L, 0L, 
                                                                                                       0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 
                                                                                                       0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), recidiv.nsite = c(1L, 
                                                                                                                                                                              1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 
                                                                                                                                                                              0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
                                                                                                                                                                              0L, 0L, 0L, 0L), n.fjernet = c(15L, 7L, 12L, 57L, 6L, 27L, 
                                                                                                                                                                                                             18L, 11L, 24L, 9L, 25L, 9L, 13L, 19L, 8L, 10L, 33L, 23L, 
                                                                                                                                                                                                             10L, 3L, 15L, 15L, 3L, 6L, 16L, 9L, 9L, 13L, 10L, 12L, 20L, 
                                                                                                                                                                                                             30L, 16L, 16L, NA), n.sygdom = c(2L, 1L, 6L, 6L, 0L, 0L, 
                                                                                                                                                                                                                                              9L, 0L, 0L, 0L, 0L, 2L, 3L, 0L, 0L, 0L, 2L, 1L, 0L, 2L, 1L, 
                                                                                                                                                                                                                                              4L, 1L, 2L, 4L, 3L, 2L, 0L, 0L, 2L, 0L, 0L, 1L, 0L, NA), 
                    stadie = c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 4L, 
                               3L, 4L, 4L, 3L, 4L, 3L, 3L, 3L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 
                               3L, 5L, 3L, 3L, 4L, 4L, 4L, 4L), test = c(0.133333333333333, 
                                                                         0.142857142857143, 0.5, 0.105263157894737, 0, 0, 0.5, 0, 
                                                                         0, 0, 0, 0.222222222222222, 0.230769230769231, 0, 0, 0, 0.0606060606060606, 
                                                                         0.0434782608695652, 0, 0.666666666666667, 0.0666666666666667, 
                                                                         0.266666666666667, 0.333333333333333, 0.333333333333333, 
                                                                         0.25, 0.333333333333333, 0.222222222222222, 0, 0, 0.166666666666667, 
                                                                         0, 0, 0.0625, 0, NA)), .Names = c("sex", "mors", "os", "os.beh", 
                                                                                                           "os.neck", "rfs.neck", "rfs.neck.tsite", "rfs.neck.nsite", "rfs.neck.msite", 
                                                                                                           "recidiv.tsite", "recidiv.nsite", "n.fjernet", "n.sygdom", "stadie", 
                                                                                                           "test"), row.names = c(2L, 6L, 7L, 8L, 9L, 10L, 11L, 14L, 15L, 
                                                                                                                                  17L, 18L, 22L, 23L, 24L, 25L, 26L, 28L, 31L, 34L, 35L, 36L, 37L, 
                                                                                                                                  38L, 39L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L
                                                                                                           ), class = "data.frame")

1 个答案:

答案 0 :(得分:2)

Markus是正确的,克服该问题的方法是定义自己的范围值:

    Predict(model1,test=seq(0,0.6,by=0.1))

  test       yhat        lower      upper
1  0.0 -0.4295911 -0.754044179 -0.1051379
2  0.1  0.6336235  0.027948982  1.2392981
3  0.2  0.7307858  0.175765821  1.2858057
4  0.3  0.6062680 -0.001284515  1.2138206
5  0.4  0.4817503 -0.453891190  1.4173919
6  0.5  0.3572326 -0.994418951  1.7088842
7  0.6  0.2327149 -1.562760195  2.0281900

所以:

out <- bind_rows(fortify(Predict(model,test=seq(0,0.6,by=0.01), fun=exp)), fortify(Predict(model1,test=seq(0,0.6,by=0.01), fun=exp)), .id = "model")
ggplot(as.data.frame(out), aes(x = test)) +
  geom_ribbon(aes(fill = model, ymin = lower, ymax = upper), alpha = .05) +
  geom_line(aes(y = yhat, col = model)) + 
  scale_color_jco(name="", labels = c("A", "B")) + 
  scale_fill_jco(name="", labels = c("A", "B")) +
  geom_segment(aes(x = 0, y = 1, xend = 0.55, yend = 1), lty="dashed", size=0.1, alpha=0.75)

给予

enter image description here