简体   繁体   English

应用于'NULL'类型的非(列表或向量)的is.na()是什么意思?

[英]What does is.na() applied to non-(list or vector) of type 'NULL' mean?

I want to select a Cox model with the forward procedure from a data.frame with no NA. 我想从没有NA的data.frame中选择具有正向过程的Cox模型。 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' 在is.na(fit $ coefficients)中:is.na()应用于'NULL'类型的非(列表或向量)

(three times) (三次)

I tried to change the upper model, I even tried upper = ~ 1 but the warning stays. 我试图改变上面的模型,我甚至试过upper = ~ 1但警告仍然存在。 I don't understand: I have no NAs and my vectors are all numerics (I checked it). 我不明白:我没有NAs,我的载体都是数字(我检查过)。 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? 我的代码出了什么问题?

The problem in this specific case 这个特定情况下的问题

The right hand side of your formula is 1 , which makes it a null model . 公式的右侧是1 ,这使它成为空模型 coxph calls coxph.fit , which (perhaps lazily) doesn't bother to return coefficients for null models. coxph调用coxph.fit ,它(可能是懒惰地)不会为返回null模型的系数而烦恼。

Later coxph calls extractAIC , which erroneously assumes that the model object contains an element named coefficients . 后来的coxph调用extractAIC ,它错误地假定模型对象包含一个名为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. is.na假定其输入参数是原子矢量或矩阵或列表或data.frame。 Other data types cause the warning. 其他数据类型会导致警告。 It happens with NULL , as you've seen: 正如您所见,它发生在NULL

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.: 你会得到一个带有公式,函数,表达式等的simliar警告:

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'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Error :: is.na()应用于&#39;NULL&#39;类型的非(列表或向量) - Error:: is.na() applied to non-(list or vector) of type 'NULL' 在is.na(e2)中:is.na()应用于类型为“ NULL”的非(列表或向量) - In is.na(e2) : is.na() applied to non-(list or vector) of type 'NULL' 循环错误:is.na()应用于类型为“ NULL”的非(列表或向量) - Error in loop: is.na() applied to non-(list or vector) of type 'NULL' 闪亮错误is.na()应用于类型为&#39;NULL&#39;的非(列表或向量) - Shiny error is.na() applied to non-(list or vector) of type 'NULL' 为什么 ggplot annotate 会抛出此警告:在 is.na(x): is.na() 应用于“表达式”类型的非(列表或向量) - Why does ggplot annotate throw this warning: In is.na(x) : is.na() applied to non-(list or vector) of type 'expression' 'function(x){.any(is.na(x))})]' 在我的 function 'descr,mol[.apply(descr,mol,2.function(x){!any(is .na(x))})]' - what does 'function(x){!any(is.na(x))})]' mean exactly in my function 'descr.mol[,apply(descr.mol,2, function(x){!any(is.na(x))})]' 如果数据框是列表列表,is.na(df)如何工作? - How does is.na(df) work if a dataframe is a list of lists? x [is.na(x)]在R中做什么? - What does x[is.na(x)] do in R? 在带有管道函数的向量中应用 is.na() 函数 - Applying is.na() function in a vector with pipe function 为什么 is.na() 在 R 中的空向量的第一个元素上返回 TRUE? - Why does is.na() return TRUE on the first element of an empty vector in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM