What does is.na() applied to non-(list or vector) of type 'NULL' mean?
Asked Answered
W

1

13

I want to select a Cox model with the forward procedure from a data.frame with no NA. Here is some sample data:

test <- data.frame(
  x_1   = runif(100,0,1),
  x_2   = runif(100,0,5),
  x_3   = runif(100,10,20),
  time  = runif(100,50,200),
  event = c(rep(0,70),rep(1,30))
)

This table has no signification but if we try to build a model anyway :

modeltest <- coxph(Surv(time, event) ~1, test)
modeltest.forward <- step(
  modeltest, 
  data      = test, 
  direction = "forward", 
  scope     = list(lower = ~ 1, upper = ~ x_1 + x_2 + x_3)
)

The forward ends at the first step and says:

In is.na(fit$coefficients) : is.na() applied to non-(list or vector) of type 'NULL'

(three times)

I tried to change the upper model, I even tried upper = ~ 1 but the warning stays. I don't understand: I have no NAs and my vectors are all numerics (I checked it). I searched if people had the same issue but all I could find was problems due to the name or class of the vectors.

What's wrong with my code?

Weimer answered 20/6, 2013 at 9:9 Comment(7)
I forgot to say that if i try something like modeltest<-coxph(Surv(time, event) ~x_1,data=test), this works. The problem seems to be in the forward but i don't understand why neither how to solve it.Weimer
If I change coxph(Surv) to lm(event) or lm(time), it works. I would therefore come from the combination of forward and coxph ?Weimer
Hi @Vincent! Were you able to solve the problem? The same error is appearing with a cox model and I don't seem to understand why is it..Counterintelligence
Well yes and no : with my current version of R (3.0.3), I don't get errors running the above code but only warnings, and the step function works. The explanation is that a null cox model has no coefficients, not even an intercept, unlike a null linear model. So when the step function verifies if the models are ok (no NA in the estimations), it can't find any estimation for the null model which causes the error/warnings. Thus, the warning is ignorable without jeopardizing the outcome quality. If you get an error, maybe try to install a more recent version of R ?Weimer
Thank you! I will try to install a newer version. I was trying to use anova to compare the null model with others models but the error was appearing. One problem should be not having an intercept but that sounds a bit weird to me.Counterintelligence
I'm having the same problem with dredge function applied to a coxphmodel with several covariates. Do you conclude that this could be a bug? (A few days ago it worked just fine)Metacenter
I wouldn't say a bug, it's just that step (and i'm assuming dredge too) can't deal with a model that have no coefficient. Does the error remain if you set m.min=1 ?Weimer
C
20

The problem in this specific case

The right hand side of your formula is 1, which makes it a null model. coxph calls coxph.fit, which (perhaps lazily) doesn't bother to return coefficients for null models.

Later coxph calls extractAIC, which erroneously assumes that the model object contains an element named coefficients.

The general case

is.na assumes that its input argument is an atomic vector or a matrix or a list or a data.frame. Other data types cause the warning. It happens with NULL, as you've seen:

is.na(NULL)
## logical(0)
## Warning message:
## In is.na(NULL) : is.na() applied to non-(list or vector) of type 'NULL'

One common cause of this problem is trying to access elements of a list, or columns of a data frame that don't exist.

d <- data.frame(x = c(1, NA, 3))
d$y # "y" doesn't exist is the data frame, but NULL is returned
## NULL
is.na(d$y)
## logical(0)
## Warning message:
## In is.na(d$y) : is.na() applied to non-(list or vector) of type 'NULL'

You can protect against this by checking that the column exists before you manipulate it.

if("y" in colnames(d))
{
  d2 <- d[is.na(d$y), ]
}

The warning with other data types

You get a simliar warning with formulae, functions, expressions, etc.:

is.na(~ NA)
## [1] FALSE FALSE
## Warning message:
## In is.na(~NA) : is.na() applied to non-(list or vector) of type 'language'

is.na(mean)
## [1] FALSE
## Warning message:
## In is.na(mean) : is.na() applied to non-(list or vector) of type 'closure'

is.na(is.na)
## [1] FALSE
## Warning message:
## In is.na(is.na) : is.na() applied to non-(list or vector) of type 'builtin'

is.na(expression(NA))
## [1] FALSE
## Warning message:
## In is.na(expression(NA)) :
##   is.na() applied to non-(list or vector) of type 'expression'
Chloral answered 14/12, 2015 at 5:31 Comment(2)
The 'other data types' bit is really useful. Could you add an example which triggers ... of type 'symbol' too?Overdress
@Overdress Well is.na(as.symbol("x")) would do it, but I'm guessing that this has been caused by some non-standard evaluation somewhere. Consider creating a new question describing your problem, and linking to this question, stating that you have a special case of this more general issue.Chloral

© 2022 - 2024 — McMap. All rights reserved.