2016-12-25 902 views
-2

我使用两种不同的方式来使用cor.test,一种在我自己的功能中,另一种则直接使用cor.test。当我在自己的功能中使用cor.test时,它出现了一个错误,它怎么会发生?cor.test,“没有足够的有限观测值”

这是OK

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")) 
+2

你能请使用'dput(cust_new)'并将结果粘贴到您的问题中,以便我们可以重现您的结果? – G5W

+1

你的'cor_result'函数中根本不使用'x'和'y'。 –

+0

我使用x和yx = as.numeric(数据$ x)/as.numeric(数据$ y), y = as.numeric(数据$ y), – shan

回答

0

在根我认为这是一个关于混乱参考数据集中的列的方式。特别是在使用$时,暗示$后面的符号的字面意思是。当您在第一个函数中引用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),它会给出“必须是数字向量”错误。

那么你能做什么?

  • @芭菲的建议1:通过xy字符串和使用[[ -indexing而不是$ -indexing
  • @芭菲的建议#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 
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) 

或者不数据说法:

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) 
+0

您的回答完全解决了我的问题,谢谢~~ – shan

+0

大!很高兴我能帮上忙。请接受答案(勾选标记到旁边)以确认解决方案。 – Parfait

相关问题