【问题标题】:cor.test ,"not enough finite observations"cor.test "没有足够的有限观察"
【发布时间】:2016-12-25 14:20:54
【问题描述】:

我使用两种不同的方式来使用cor.test,一种在我自己的函数中,另一种直接使用cor.test。当我在自己的函数中使用cor.test时,出现了错误,怎么会发生?

这是好的

cor.test(x=cust_new$maintain_cust/cust_new$ttl_cust,
         y=cust_new$ttl_cust,alternative="two.sided",
         method="pearson",conf.level=0.95)

以下将给出错误:

“没有足够的有限观察”

cor_result<-function(x,y,data){
  a<-cor.test(x=as.numeric(data$x)/as.numeric(data$y),
              y=as.numeric(data$y),
              alternative="two.sided",method="spearman",
              conf.level=0.95)
  r<-a$estimate
  p<-a$p.value
  c<-data.frame(r=r,p=p)
  return(c)
}

d<-cor_result(x=maintain_cust,y=ttl_cust,data=cust_new)

以下将给出错误:

'y' 必须是数值向量"

cor_result<-function(x,y,data){
  a<-cor.test(x=data$x/data$y,y=data$y,
            alternative="two.sided",method="spearman",conf.level=0.95)
  r<-a$estimate
  p<-a$p.value
  c<-data.frame(r=r,p=p)
  return(c)
}

d<-cor_result(x=maintain_cust,y=ttl_cust,data=cust_new)

dput(cust_new),一些样本

structure(list(data_month = structure(c(16953, 16983, 17014, 
17045, 17075, 17106, 16953, 16983, 17014, 17045), class = "Date"), 
    ttl_cust = c(383L, 580L, 735L, 850L, 952L, 1062L, 2418L, 
    2492L, 2515L, 2550L), maintain_cust = c(179L, 266L, 355L, 
    413L, 448L, 508L, 935L, 1026L, 1091L, 1143L)), row.names = c(NA, 
10L), class = "data.frame", .Names = c("data_month", "ttl_cust", 
"maintain_cust"))

【问题讨论】:

  • 能否请您使用dput(cust_new) 并将结果粘贴到您的问题中,以便我们重现您的结果?
  • 您根本没有在 cor_result 函数中使用 xy
  • 我使用 x 和 y.x=as.numeric(data$x)/as.numeric(data$y), y=as.numeric(data$y),

标签: r


【解决方案1】:

您没有正确地将向量(即数据框列)传递给函数。考虑传递要使用双括号引用的数据框列的字符串文字(如果列是数字类型,则可能不需要as.numeric()):

cor_result<-function(x, y, data){ 
   a<-cor.test(x=as.numeric(data[[x]])/as.numeric(data[[y]]),y=as.numeric(data[[y]]),
               alternative="two.sided", method="spearman", conf.level=0.95) 
   r<-a$estimate 
   p<-a$p.value 
   c<-data.frame(r=r,p=p) 
   return(c) 
}

d<-cor_result(x="maintain_cust", y="ttl_cust", data=cust_new)

或者没有 data 参数:

cor_result<-function(x, y){ 
   a<-cor.test(x=(x/y),y=y,
               alternative="two.sided", method="spearman", conf.level=0.95) 
   r<-a$estimate 
   p<-a$p.value 
   c<-data.frame(r=r,p=p) 
   return(c) 
}

d<-cor_result(x=cust_new$maintain_cust, y=cust_new$ttl_cust)

【讨论】:

    【解决方案2】:

    从根本上讲,我认为这是对引用数据集中列的方式的混淆。特别是,当使用$ 时,对$ 之后的符号进行索引,字面意义上的。当您在第一个函数中引用 data$xdata$y 时,R 会在您的 data 对象中查找名为“x”和“y”的列。这些在您的数据框中不存在,因此返回 NULL(如果在这种情况下 R 抛出错误可能会更好,但是哦,好吧......)

    • 在您的第一个函数中使用as.numeric()as.numeric(NULL) 返回 numeric(0)(零长度数字向量)。因此,cor.test 试图计算两个零长度对象之间的相关性,并且可以理解地抛出“没有足够的有限观察”错误。 (尝试cor.test(numeric(0),numeric(0)) 进行复制。)
    • 在您的第二个函数中,您没有转换为数字,因此您改为执行 cor.test(NULL,NULL),这会给出“必须是数字向量”错误。

    那你能做什么?

    • @Parfait 的建议 #1:将xy 作为字符串传递并使用[[-indexing 而不是$-indexing
    • @Parfait 的建议 #2:将 xy 作为对象传递(即不要在 data 中查找它们)

    如果您真的想 (1) 使用 data 参数并 (2) 将值作为符号传递,那么正确地做事就会变得更棘手。

    • 最简单的方法是调用deparse(substitute(x)) 来检索用作字符串的符号的名称,然后使用[[-indexing
    • 否则你可以使用eval,如果你小心使用的话。例如:
    f <- function(a,b,data=dd) {
       eval(substitute(a/b,list(a=quote(x),b=quote(y))),envir=dd)
    }
    dd <- data.frame(x=1,y=2)
    ## set x and y to other values in the global env 
    ##   so we can see that we got the right ones ...  
    x <- 3
    y <- 4
    f(x,y)
    ## 0.5
    

    【讨论】:

      猜你喜欢
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-05
      • 1970-01-01
      • 2023-01-20
      相关资源
      最近更新 更多