結果変数が呼び出されoutcome
、データ フレームが呼び出されたと仮定するとdf
、最初に調整された正方形を返すように関数をカスタマイズできます。その後、combn
関数を適用します。これが機能するには、結果 (因子の場合) を数値に変換する必要があることに注意してください。-df$outcome <- as.numeric(as.character(df$outcome))
R.squared <- function(y, x, z){
summary(lm(y ~ x+z, df))$adj.r.squared
}
combn(ncol(df[,-1]), 2, function(i) R.squared(df$outcome, df[,i[1]], df[,i[2]]))
#[1] 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 -0.97583296 -0.61915873 -1.31151020 -1.51437504
#[14] -1.51135538 0.79397030 -1.21025638 -1.46657250 0.98277557 -0.53936636 -0.63855221 -0.02568424 0.78512289 0.71934837 -0.31817844 -0.14891020 0.68253538
#[27] -1.05545863 0.85541926 0.67673403 -1.09460547 -1.70138478 0.75931881 0.98464144 -1.55739495 -0.05148017 -1.26050288 0.70467265 0.68822770 -1.24740025
#[40] 0.99877169 -1.78165575 -1.21522704 0.77518005 0.98376700 -1.53121019
ご覧のとおり、正しい結果が 45 個得られます (10C2 = 45)。
データ
dput(df)
structure(list(outcome = structure(c(2L, 1L, 1L, 2L), .Label = c("0",
"1"), class = "factor"), X1 = c(-0.086580111257948, 1.3225244296403,
0.63970203781302, 1.17478656505647), X2 = c(0.116290308776141,
-2.93084636363391, 0.67750806223535, 1.11777194347258), X3 = c(1.38404752146435,
1.2839408555363, -0.976479813387477, 0.990836347961829), X4 = c(-1.53428156591653,
-1.81700160188474, 0.35563308328848, 0.863904683601422), X5 = c(-0.0805126064587461,
-0.962480324796481, 0.112310964386636, -0.257651852496691), X6 = c(1.48342629539586,
0.677600299153581, -0.718621221409107, -0.547872283010696), X7 = c(1.52752065946695,
-0.039941426401065, 0.384087275444754, 2.23916461213194), X8 = c(1.753974300534,
1.22050988486485, 2.61512874217525, 1.76150083091101), X9 = c(-0.786009592713507,
-0.176356977987529, 0.0947058204731415, 0.127134850846526), X10 = c(0.510517865869084,
-1.24821415198133, 0.963011806720543, 0.307956641660821)), .Names = c("outcome",
"X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8", "X9", "X10"), row.names = c(NA,
-4L), class = "data.frame")