is.na()应用于类型为"NULL"的非(列表或向量)是什么意思? [英] What does is.na() applied to non-(list or vector) of type 'NULL' mean?

查看:651
本文介绍了is.na()应用于类型为"NULL"的非(列表或向量)是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从没有NA的data.frame中选择具有正向过程的Cox模型.这是一些示例数据:

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:

在is.na(fit $ coefficients)中:is.na()应用于类型为"NULL"的非(列表或向量)

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

(三次)

我尝试更改上部模型,甚至尝试了upper = ~ 1,但警告仍然存在.我不明白:我没有NA,我的向量都是数字(我检查过). 我搜索了人们是否也遇到了同样的问题,但是我只能发现由于向量的名称或类别而引起的问题.

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.

我的代码怎么了?

推荐答案

在此特定情况下的问题

公式的右侧是1,这使其成为空模型. coxph调用coxph.fit,(懒惰地)不会为空模型返回系数.

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.

以后的coxph会调用extractAIC,这会错误地假定模型对象包含名为coefficients的元素.

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

is.na假定其输入参数是原子向量或矩阵或列表或data.frame.其他数据类型会引起警告.如您所见,它发生在NULL上:

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'

这篇关于is.na()应用于类型为"NULL"的非(列表或向量)是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆