Spearman 等級相關

R 中的 A/B 測試

Lauryn Burleigh

Data Scientist

Spearman 相關的假設

  • 等級排序-依位置排序
  • Spearman 高相關-兩變數位置相近
  • 連續或離散資料
    • 順序、區間或比率

四個長條由高到低排列,最高為 1,最低為 4。

R 中的 A/B 測試

單調關係

  • Spearman 相關(rho)-單調關係

    • 非持續單調遞增或遞減
  • 單調比線性限制更寬鬆

ggplot(data, aes(x = drownings, 
                 y = icecream)) +
  geom_point()

散佈圖顯示單調遞增關係:x 軸溺水人數增加時,y 軸冰淇淋先持平、後上升、再持平。

散佈圖顯示單調遞減關係:x 軸溺水人數增加時,y 軸冰淇淋先持平、後下降、再持平、再下降。

R 中的 A/B 測試

假設與樣本數

  • 虛無假設-時間與披薩享受度之間無單調關聯
library(pwr)
pwr.r.test(r = 0.3, power = 0.80, 
           sig.level = 0.05)
    approximate correlation power 
 calculation (arctangh transformation) 

              n = 84.07364
              r = 0.3
      sig.level = 0.05
          power = 0.8
    alternative = two.sided
R 中的 A/B 測試

Spearman 忽略群組

cor.test(~ enjoyment + time, 
         data = pizza, 
         method = "spearman", 
        exact = FALSE)
    Spearman's rank correlation rho
data:  time and enjoyment
S = 2, p-value = .003245
alternative hypothesis: true rho is 
not equal to 0
sample estimates:
      rho 
0.9984962 
samp <- length(pizza$time)
[1]  90
R 中的 A/B 測試

群組內的 Spearman

cor.test(~ enjoyment + time, 
         data = pizza, 
         subset = 
             (Topping == "Cheese"),
         method = "spearman",
         exact = FALSE)
    Spearman's rank correlation rho
data:  time and enjoyment
S = 1.2434e-14, p-value = 0.0003968
alternative hypothesis: true rho is 
not equal to 0
sample estimates:
rho 
  1 
ggplot(pizza, aes(x = enjoyment, 
                  y = time, 
                  color = Topping)) + 
  geom_point()

藍色 Cheese 資料在 x 軸左側呈現陡峭正斜率;粉紅色 Pepperoni 資料在右側為較緩的正斜率。

R 中的 A/B 測試

Spearman 檢定力分析

library(pwr)
pwr.r.test(r = 0.998, n = 90, 
           sig.level = 0.003)
     approximate correlation power 
  calculation (arctangh transformation) 

              n = 90
              r = 0.998
      sig.level = 0.003
          power = 1
    alternative = two.sided
R 中的 A/B 測試

參照輸出結果

rhotest <- cor.test(~ enjoyment + time, 
         data = pizza, 
         method = "spearman")
samp <- length(pizza$time)
library(pwr)
pwr.r.test(r = rhotest$estimate, 
           sig.level = rhotest$p.value,
           n = samp)
     approximate correlation power 
  calculation (arctangh transformation) 

              n = 90
              r = 0.998
      sig.level = 0.003
          power = 1
    alternative = two.sided
R 中的 A/B 測試

一起來練習吧!

R 中的 A/B 測試

Preparing Video For Download...