相関係数の検定 cor.test *機能 二変数間の相関関係の検定を行う。対象とするのは,ピアソンの積率相関係数,ケンドールの順位相関係数,スピアマンの順位相関係数である。 *使用法 cor.test(x, ...) cor.test(x, y, alternative = c("two.sided", "less", "greater"), method = c("pearson", "kendall", "spearman"), exact = NULL, conf.level = 0.95, ...) cor.test(formula, data, subset, na.action, ...) *引数 x, y データベクトル alternarive 対立仮説の種類 "two.sided", "less", "greater" のいずれか 最初の1文字だけでもよい 両側検定なら "two.sided"(デフォルト) 片側検定なら "less" または "greater" method 3 種の相関係数のどれを用いるかを "pearson", "kendall", "spearman" のいずれかで指定する 1 文字でもかまわない exact method="kendall" の場合にのみ指定できる,デフォルトでは NULL となっており, データの組数が 50 未満で同値がなければ正確な P 値を求める ヘルプには書かれていないが,プログラムソースを読むと,method="spearman" の場合も, n が 1290 以下なら,正確な p 値を返すことがわかる もっとも,同値がある場合には n がこれらより少なくても正確な P 値ではないというエラーメッセージが出る conf.lebel 信頼率 デフォルトでは 95% formula 「~ u + v」の形をしたモデル式 u, v は同じ長さを持つデータでないといけない data モデル式に出てくる u, v がデータフレームの変数の場合には,そのデータフレーム名 subset 観察データのサブセット na.action データに NA が含まれるときに適用される関数(注) デフォルトは getOption("na.action") により示されるもの(通常は na.omit 関数) 注:[[超訳:NAの扱い]] *戻り値 **関数への入力 $ alternarive: 帰無仮説の種類 **関数からの出力 $ statistic : 検定統計量 $ parameter : t 分布に従う検定統計量の場合はその自由度 $ p.value : P 値 $ estimate : 標本相関係数 $ conf.int : 母相関係数の信頼区間 $ null.value : 帰無仮説の下での母相関係数(常に 0 である。すなわち,無相関検定である) $ method : 検定の種別 $ data.name : データの記述 *例1 3 種類の相関係数について検定してみる > x <- c(3,2,5,4,6,5,7,5,8,7,9) > y <- c(1,3,4,2,6,5,8,6,5,2,5) > cor.test(x, y, method="p") # ピアソンの積率相関係数 Pearson's product-moment correlation data: x and y t = 1.7463, df = 9, p-value = 0.1147 alternative hypothesis: true correlation is not equal to 0 95 percent confidence interval: -0.1386361 0.8472623 sample estimates: cor 0.503077 > cor.test(x, y, method="s") # スピアマンの順位相関係数 Spearman's rank correlation rho data: x and y S = 110.7689, p-value = 0.1203 alternative hypothesis: true rho is not equal to 0 sample estimates: rho 0.4965048 Warning message: タイのため正確な p 値を計算することができません in: cor.test.default(x, y, method = "s") > cor.test(x, y, method="k") # ケンドールの順位相関係数 Kendall's rank correlation tau data: x and y z = 1.6063, p-value = 0.1082 alternative hypothesis: true tau is not equal to 0 sample estimates: tau 0.396059 Warning message: タイのため正確な p 値を計算することができません in: cor.test.default(x, y, method = "k") *例2 formula を用いてみる > x <- c(3,2,5,4,6,5,7,5,8,7,9) > y <- c(1,3,4,2,6,5,8,6,5,2,5) > df <- data.frame(dx=x, dy=y) > cor.test(~ dx + dy, df, method="p") # ピアソンの積率相関係数 Pearson's product-moment correlation data: dx and dy t = 1.7463, df = 9, p-value = 0.1147 alternative hypothesis: true correlation is not equal to 0 95 percent confidence interval: -0.1386361 0.8472623 sample estimates: cor 0.503077 > cor.test(~ dx + dy, df, method="s") # スピアマンの順位相関係数 Spearman's rank correlation rho data: dx and dy S = 110.7689, p-value = 0.1203 alternative hypothesis: true rho is not equal to 0 sample estimates: rho 0.4965048 Warning message: タイのため正確な p 値を計算することができません in: cor.test.default(x = c(3, 2, 5, 4, 6, 5, 7, 5, 8, 7, 9), y = c(1, > cor.test(~ dx + dy, df, method="k") # ケンドールの順位相関係数 Kendall's rank correlation tau data: dx and dy z = 1.6063, p-value = 0.1082 alternative hypothesis: true tau is not equal to 0 sample estimates: tau 0.396059 Warning message: タイのため正確な p 値を計算することができません in: cor.test.default(x = c(3, 2, 5, 4, 6, 5, 7, 5, 8, 7, 9), y = c(1,