plotting a cox proportional haz : Error in model.frame.default : object is not a matrix BUT data can't be a matrix??

Hi!

I'm trying to plot a cox proportional hazards graph and am getting the error that my model.frame.default is not a matrix. However, if I convert my data frame to a matrix, I can't run the first part of the code (the coxph bit)

model.frame.default (formula = Surv(deathday, censor) ~ dist + : 'data' must be a data.frame, not a matrix or an array. Please help?





plotcox <- coxph(Surv
                (deathday,censor) ~
                 dist+
                handling,
                pilot
)

plot(survfit(plotcox,conf.int=F))

combinations <- expand.grid(
  distance_to_vetch=levels(pilot$dist),
  handling=levels(pilot$handling)
  )

pilot
combinations

plot(survfit(plotcox, newdata = combinations), conf.int=F, 
lwd=c(1,2,1,2),
col = c(blue, blue, red,red))

structure(list(date_jmp = structure(c(2L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L), .Label = c(" 8/19/2020",
"8/19/20"), class = "factor"), pair = c(1L, 2L, 3L, 4L, 1L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 7L, 6L, 9L, 8L, 4L, 3L, 12L, 2L,
10L, 5L, 11L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L, 17L, 17L,
18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L, 23L, 23L, 24L,
24L, 25L, 25L, 26L, 26L), plant = c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 19L, 20L, 21L, 22L, 23L, 24L, 26L,
30L, 31L, 34L, 39L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L,
82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L,
95L, 96L, 97L, 98L, 99L, 100L), tag_colors = structure(c(7L,
3L, 5L, 1L, 8L, 4L, 6L, 2L, 7L, 3L, 5L, 1L, 4L, 1L, 5L, 4L, 8L,
2L, 6L, 3L, 4L, 6L, 3L, 2L, 10L, 10L, 10L, 10L, 9L, 9L, 10L,
10L, 10L, 10L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 9L, 9L, 10L,
10L, 9L, 9L, 10L, 10L, 9L, 9L), .Label = c("buff_red_blue", "buff_red_green",
"buff_yellow_blue", "buff_yellow_green", "gray_red_blue", "gray_red_green",
"gray_yellow_blue", "gray_yellow_green", "red", "yellow"), class = "factor"),
handling = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("root",
"sod"), class = "factor"), age = structure(c(1L, 1L, 2L,
2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L,
2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L,
2L, 2L, 1L, 1L), .Label = c("cot", "juv"), class = "factor"),
dist = structure(c(1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("f",
"n"), class = "factor"), open_leaves = c(NA, NA, 14L, NA,
10L, NA, 14L, 9L, NA, 12L, 28L, 13L, NA, NA, NA, 5L, 4L,
NA, 10L, NA, NA, 11L, NA, NA, NA, NA, NA, NA, 7L, NA, 12L,
13L, NA, NA, NA, NA, 8L, 7L, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 6L, 3L, 8L, NA), height = c(NA, NA, 24, NA, 20,
NA, 13, 17, NA, 20.5, 33, 21, NA, NA, NA, 9, 4.2, NA, 12.5,
NA, NA, 16, NA, NA, NA, NA, NA, NA, 14.5, NA, 17.5, 23, NA,
NA, NA, NA, 9.5, 11, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 2.5, 6, 9, NA), nodule_score = c(NA, NA, 54, NA, 10.5,
NA, 6, 26.5, NA, 0, 55.5, 49.5, NA, NA, NA, 6.5, 4, NA, 22.5,
NA, NA, 11.5, NA, NA, NA, NA, NA, NA, 0, NA, 10, 12, NA,
NA, NA, NA, 5.5, 5, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
8, 4, 5.5, NA), AGB_mg = c(NA, NA, 327, NA, 142, NA, 108,
206, NA, 141.1, 574, 303, NA, NA, NA, 47, 12, NA, 96, NA,
NA, 163, NA, NA, NA, NA, NA, NA, 90, NA, 149, 369, NA, NA,
NA, NA, 48, 55, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 12,
24, 53, NA), nod_agb_ratio = c(NA, NA, 0.17, NA, 0.07, NA,
0.06, 0.13, NA, 0, 0.1, 0.16, NA, NA, NA, 0.14, 0.33, NA,
0.23, NA, NA, 0.07, NA, NA, NA, NA, NA, NA, 0, NA, 0.07,
0.03, NA, NA, NA, NA, 0.11, 0.09, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 0.67, 0.17, 0.1, NA), deathday = c(98L, 98L,
124L, 21L, 124L, 18L, 124L, 124L, 6L, 124L, 124L, 124L, 69L,
10L, 47L, 124L, 124L, 6L, 124L, 18L, 47L, 124L, 14L, 47L,
21L, 56L, 35L, 47L, 124L, 35L, 124L, 124L, 35L, 98L, 124L,
56L, 124L, 124L, 69L, 21L, 69L, 62L, 35L, 14L, 98L, 56L,
80L, 80L, 124L, 124L, 124L, 47L), censor = c(1L, 1L, 0L,
1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L,
0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 1L,
1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
0L, 0L, 0L, 1L)), class = "data.frame", row.names = c(NA,
-52L))

thanks so much!!!

The key to unraveling an error like this is something that doesn't come naturally—closely reading the function signatures. (I resisted this myself for years).

R presents to the user as a functional language, best thought of as school algegra writ large: f(x) = y, where the three objects are x, what is at hand, y what is desired and f the transformation from x to y. The key is that f (which may be composite) at any point in the process may be looking for a different x than one might think.

In this problem, I have to speak in the abstract for lack of a reprex (do a search here for the FAQ) that permits reproducing the problem with representative data.

We have coxph, Surv, Survfit, expand.grid, and plot involved in the code provided. The first step is to examine the class of each of the return objects along the way to understand what they are. For example, unrelated to the provided code: data frame => matrix => list => ?

Look at each intermediate step to see what is being dealt with and determine which step is actually producing the error. It's not often what at first seems obvious.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.