2

How can I resolve the error of undefined columns selected? I have loaded the survminer and the survival packages.

   cox4<- coxph(formula= Surv(LOS,Mortality)~ BNPcat+HF_dx+Age+Sex+BMI+
           Admit_QTc+FI02_comb+Admit_Lactate,data=DF)
    ggforest(cox4)
> cox4
Call:
coxph(formula = Surv(LOS, Mortality) ~ BNPcat + HF_dx + Age + 
    Sex + BMI + Admit_QTc + FI02_comb + Admit_Lactate, data = DF)

                    coef  exp(coef)   se(coef)      z      p
BNPcat1        0.9030311  2.4670696  0.9888592  0.913 0.3611
HF_dx1         0.8819918  2.4157065  0.8519562  1.035 0.3005
Age            0.0479344  1.0491018  0.0284893  1.683 0.0925
Sex1           0.3792915  1.4612490  0.7020620  0.540 0.5890
BMI            0.0250854  1.0254027  0.0319454  0.785 0.4323
Admit_QTc     -0.0006391  0.9993611  0.0060206 -0.106 0.9155
FI02_comb      0.0204957  1.0207072  0.0101321  2.023 0.0431
Admit_Lactate  0.2433651  1.2755342  0.1112036  2.188 0.0286

Likelihood ratio test=21.33  on 8 df, p=0.006311
n= 71, number of events= 13 
   (62 observations deleted due to missingness)
> ggforest(cox4)
Error in `[.data.frame`(cbind(allTermsDF, coef[inds, ]), , c("var", "level",  : 
  undefined columns selected
In addition: Warning message:
In .get_data(model, data = data) :
  The `data` argument is not provided. Data will be extracted from model fit.
ABC
  • 21
  • 1
  • 3

3 Answers3

0

It's difficult to confirm the source of the error without seeing either the data or the model summary, but if we do this:

library(survival)
library(survminer)

data <- data.frame(LOS = rlnorm(1000, rep(0:1, each = 500)),
                   mortality = rbinom(100, 1, rep(c(0.5, 0.05), each = 500)),
                   condition = rep(c("Sick", "Well"), each = 500))

cox4 <- coxph(Surv(LOS, mortality) ~ condition, data = data)

We get the following error when we try to plot:

ggforest(cox4)
#> Warning in .get_data(model, data = data): The `data` argument is not provided.
#> Data will be extracted from model fit.
#> Error in .get_data(model, data = data): The `data` argument should be provided
#> either to ggsurvfit or survfit.

This is a strange error. It seems to occur because your data frame is called data, which is used as a parameter name in ggforest. We can make the error go away if we specifically pass data = data to ggforest:

ggforest(cox4, data = data)

or change the name of your data frame:

df <- data

cox4 <- coxph(Surv(LOS, mortality) ~ condition, data = df)

ggforest(cox4)
#> Warning in .get_data(model, data = data): The `data` argument is not provided.
#> Data will be extracted from model fit.

Created on 2020-07-12 by the reprex package (v0.3.0)

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Thank you for your response! Unfortunately, when I try with the code you have above, I get the same column error (just renamed dataframe to ABC). > > cox4 <- coxph(Surv(LOS, mortality) ~ condition, data = ABC) > ggforest(cox4) Error in `[.data.frame`(cbind(allTermsDF, coef[inds, ]), , c("var", "level", : undefined columns selected In addition: Warning message: In .get_data(model, data = data) : The `data` argument is not provided. Data will be extracted from model fit. – ABC Jul 12 '20 at 16:10
  • @ABC did you try `ggforest(cox4, data = ABC)`? – Allan Cameron Jul 12 '20 at 16:19
  • Yes -- then I get just the undefined column error: > ggforest(cox4,data=ABC) Error in `[.data.frame`(cbind(allTermsDF, coef[inds, ]), , c("var", "level", : undefined columns selected – ABC Jul 12 '20 at 16:25
  • @ABC and if you run the model with just a single variable do you get the same error? If not, is there one particular variable that breaks it? – Allan Cameron Jul 12 '20 at 16:33
  • I did try this -- if run my own model with just 1 variable, it gives the same error, similar as with your code above with the variable "condition". If it helps, I am running R 4.0.2, survminer 0.4.7, and survival 3.2-3. – ABC Jul 12 '20 at 16:47
0

I encountered the same problem. And I solved it by replacing the R package of "broom" with a previous version.

  • 1
    It would probably help future viewers of the answer if the relevant versions of `broom` are stated and an explanation of how or why this answered the original question. – Peter Jul 21 '20 at 16:57
  • 1
    How do I install an older version of broom? – ABC Jul 23 '20 at 02:42
0

I experienced the same Problem when using as.factor() within the coxph-function:

cox0x<-coxph(formula=Surv(SurvTime.full,state.full=="1") ~  as.factor(PreSur), data=data1)
ggforest(cox0x, data=data1)

Error in `[.data.frame`(x, i, j) : undefined columns selected

Of note, my variable PreSur already was a factor when used in coxph. However, when avoiding as.factor() in my case solved the Problem:

cox0x<-coxph(formula=Surv(SurvTime.full,state.full=="1") ~  PreSur, data=data1)
ggforest(cox0x, data=data1)

enter image description here

However, I am aware that this is not the solution to ABC's problem, who does not use as.factor() in the code. It might be helpful to others though....