簡體   English   中英

使用Spearman檢驗將R中的組與兩組進行相關

[英]correlation by groups in R with two groups using spearman test

在我的數據集中,我必須按組進行關聯

我寫

require(plyr)
func <- function(terr)
{
  return(data.frame(COR = cor(terr$Killed, terr$Terr..Attacks,terr$GDP.capita)))
}

ddply(terr, .(Macro.Region,Religion), func)

然后我得到了錯誤

Error in cor(terr$Killed, terr$Terr..Attacks, terr$GDP.capita) : 
  invalid 'use' argument

怎么了,如何正確進行分析

terr=structure(list(Macro.Region = structure(c(5L, 4L, 4L, 3L, 4L, 
6L, 1L, 2L, 4L, 3L, 6L, 5L, 4L, 4L, 3L, 4L, 6L, 1L, 2L, 4L, 3L, 
6L), .Label = c("Arab Countries", "Asia", "Eastern Europe and post-Soviet", 
"Latin America", "Sub-Saharan Africa", "Western States"), class = "factor"), 
    Killed = c(0L, 0L, 0L, 6L, 0L, 0L, 1L, 76L, 0L, 0L, 36L, 
    0L, 0L, 0L, 6L, 0L, 0L, 1L, 76L, 0L, 0L, 36L), Terr..Attacks = c(2L, 
    0L, 2L, 2L, 0L, 9L, 3L, 88L, 0L, 0L, 6L, 2L, 0L, 2L, 2L, 
    0L, 9L, 3L, 88L, 0L, 0L, 6L), Religion = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 2L, 2L, 1L, 1L, 1L), .Label = c("Christianity", "Islam"
    ), class = "factor"), GDP.capita = c(6813L, 26198L, 20677L, 
    9098L, NA, 49882L, 51846L, 4207L, 17508L, 18616L, 46301L, 
    6813L, 26198L, 20677L, 9098L, NA, 49882L, 51846L, 4207L, 
    17508L, 18616L, 46301L)), class = "data.frame", row.names = c(NA, 
-22L))

解決方案這里spearman在R中按組進行相關不適合,因為我有兩個組和三個var

您可以使用purrr函數keep來嘗試tidyverse以限制樣本量足夠的組,並map以計算成對相關性。

library(tidyverse)
terr %>% 
 split(list(.$Macro.Region, .$Religion)) %>% 
 keep(~nrow(.) > 3) %>% 
 map(~.x %>% 
         select(Killed,GDP.capita,Terr..Attacks) %>% 
         cor(cbind.data.frame(.), use = "complete.obs"))
$`Eastern Europe and post-Soviet.Christianity`
              Killed GDP.capita Terr..Attacks
Killed             1         -1             1
GDP.capita        -1          1            -1
Terr..Attacks      1         -1             1

$`Latin America.Christianity`
              Killed GDP.capita Terr..Attacks
Killed            NA         NA            NA
GDP.capita        NA  1.0000000    -0.1543897
Terr..Attacks     NA -0.1543897     1.0000000

$`Western States.Christianity`
              Killed GDP.capita Terr..Attacks
Killed             1         -1            -1
GDP.capita        -1          1             1
Terr..Attacks     -1          1             1

嘗試使用Hmiscrcorr函數來檢索相應的pvalues

library(Hmisc)
terr %>% 
  split(list(.$Macro.Region, .$Religion)) %>% 
  keep(~nrow(.) > 4) %>% 
  map(~rcorr(cbind(.$Killed, .$GDP.capita, .$Terr..Attacks)))
$`Latin America.Christianity`
     [,1]  [,2]  [,3]
[1,]    1   NaN   NaN
[2,]  NaN  1.00 -0.15
[3,]  NaN -0.15  1.00

n
     [,1] [,2] [,3]
[1,]    8    6    8
[2,]    6    6    6
[3,]    8    6    8

P
     [,1] [,2]   [,3]  
[1,]                   
[2,]             0.7703
[3,]      0.7703  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM