R cor.test:“没有足够的有限观察”。 [英] R cor.test : "not enough finite observations"

查看:770
本文介绍了R cor.test:“没有足够的有限观察”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试创建一个R函数,用于计算指定列与数据框的所有数字列的corr.test相关性。这是我的代码:

I'm currently trying to create an R function computing the corr.test correlation of a specified column with all the numeric columns of a dataframe. Here's my code :

#function returning only numeric columns
only_num <- function(dataframe)
{
  nums <- sapply(dataframe, is.numeric)
  dataframe[ , nums]
}

#function returning a one-variable function computing the cor.test correlation of the variable
#with the specified column

function_generator <- function(column)
  {
    function(x)
    {
      cor.test(x, column, na.action = na.omit)
    } 
  }

data_analysis <- function(dataframe, column)
  {
  DF <- only_num(dataframe)

  fonction_corr <- function_generator(column)

  sapply(DF, fonction_corr)

  }

data_analysis(40, 6, m, DF$Morphine)

当我在最后一行调用 data_analysis时,出现以下错误:

When I call "data_analysis" at the last line, I get the following error :

cor.test.default(x,column,na.action中的错误= na.omit):
的有限观测量不足离子

"Error in cor.test.default(x, column, na.action=na.omit) : not enough finite observations"

这是什么意思?我应该改变什么?我有点卡住...

What could it mean? What should I change? I'm kind of stuck...

谢谢。

结账

推荐答案

在某些情况下,cor.test返回的错误是不足的有限观测。如果看一下cor.test.default的源代码,您将看到:

"Not enough finite obervations" is an error returned by cor.test under certain circumstances. If you take a look a the cor.test.default source code, you'll see :

OK <- complete.cases(x, y)
x <- x[OK]
y <- y[OK]
n <- length(x)

cor.test从您的向量中删除NA值
[...]

cor.test removes NA values from you vectors [...]

if (method = "pearson") {
    if (n < 3L) 
        stop("not enough finite obervations")

[...]

else {
    if (n<2)
        stop("not enough finite obervations")

如果向量不包含足够的非NA值(小于3),则该函数将返回错误。

If your vectors do not contain enough non-NA values (less than 3), the function will return the error.

在其中包含所有列您的数据框包含足够的非NA值,然后再使用cor.test。

Make all of the columns in your dataframe contain enough non-NA values before you use cor.test.

我希望这会很有用。

这篇关于R cor.test:“没有足够的有限观察”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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