What does is.na () mean when applied to a non (list or vector) of type "NULL"?

I want to select a Cox model using the forward procedure from a data.frame file without NA. Here are 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 does not matter, 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 front end in the first step and says:

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

(three times)

I tried changing the upper model, I even tried upper = ~ 1 , but the warning remains. I do not understand: I do not have NS, and my vectors are all numerical (I checked). I searched if people had the same problem, but all I could find was problems due to the name or class of vectors.

What is wrong with my code?

+6
source share
1 answer

Problem in this particular case

The right side of your formula is 1 , which makes it zero. coxph calls coxph.fit , which (perhaps lazily) does not honor the return of coefficients for null models.

coxph calls extractAIC , which mistakenly assumes that the model object contains an element called coefficients .

General case

is.na assumes that its input argument is an atomic vector or matrix or list or data.frame file. A warning triggers other data types. This happens with NULL , as you saw:

 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 an attempt to access list items or columns in a data frame that do not 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 establishing that the column exists before manipulating it.

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

Warning with other data types

You get a simliar warning with formulas, 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' 
+10
source

Source: https://habr.com/ru/post/947716/


All Articles