首页 文章

在R中输出N和Pearson相关的显着性

提问于
浏览
0

我有400个商店部门,我在所有部门之间运行(Pearson)相关性 . 如何输出'N'(个案数)和显着性水平(p值)?

我正在使用cor函数 . 这是我当前的代码,工作正常:

numprod <- ncol(data) - 2; 
matrix <- as.matrix(data[ ,2:numprod]);
AllChannels <- cbind(matrix(nrow = numprod-1,"All channels"),cor(matrix, use="all.obs", method="pearson"));

在SPSS中,当您运行相关时,它会输出相关系数N和显着性 . 这是我想要的结果 .

谢谢大家!

卢卡斯

1 回答

  • 0

    如果它只是其中一个向量的长度,那么使用 length . 如果你想要相关系数的推理计算等于0,那么使用cor.test(如 ?cor 的帮助页面告诉你的那样 . )如果它是测试的自由度数,那么请仔细查看 ?cor.test .

    > cor.test(1:10,2:11)
    
        Pearson's product-moment correlation
    
    data:  1:10 and 2:11 
    t = 134217728, df = 8, p-value < 2.2e-16
    alternative hypothesis: true correlation is not equal to 0 
    95 percent confidence interval:
     1 1 
    sample estimates:
    cor 
      1
    

    cor.test的结果将是一个列表,因此使用 cbind 没有用 . Hmisc包有 rcorr

    install.packages("Hmisc")
    library(Hmisc)
    x <- c(-2, -1, 0, 1, 2)
    y <- c(4,   1, 0, 1, 4)
    z <- c(1,   2, 3, 4, NA)
    v <- c(1,   2, 3, 4, 5)
    rcorr(cbind(x,y,z,v))
    #   ========   Returns a list with three elements:
    > rcorr(cbind(x,y,z,v))
      x     y     z v
    x 1  0.00  1.00 1
    y 0  1.00 -0.75 0
    z 1 -0.75  1.00 1
    v 1  0.00  1.00 1
    
    n
      x y z v
    x 5 5 4 5
    y 5 5 4 5
    z 4 4 5 4
    v 5 5 4 5
    
    P
      x      y      z      v     
    x        1.0000 0.0000 0.0000
    y 1.0000        0.2546 1.0000
    z 0.0000 0.2546        0.0000
    v 0.0000 1.0000 0.0000
    

相关问题