# 分割表の無作為化検定(Randomization test)をする関数2 random.test.2 <- function(table, times=10000){ #table:分割表, times:反復回数 sum.row <- apply(table,1,sum) sum.col <- apply(table,2,sum) test.table <- r2dtable(times, sum.row, sum.col) result <- list() for (i in 1:length(table)){ #分割表の測定項目分を繰り返す test <- table[[i]] #入力データを代入 for (j in 1:times){ #反復回数だけ繰り返す test <- c(test, test.table[[j]][[i]]) #検定用データの代入 } tmp.res <- (rank(test)[1])/(times+1) #入力データと検定用データの中での入力データの階級 result[i] <- 2 * min(tmp.res, 1-tmp.res) #両側検定のp値 } result <- matrix(result, nrow=nrow(table)) colnames(result) <- colnames(table) rownames(result) <- rownames(table) return(result) } # 使い方 data <- c(70,40,80, 60,50,100, 10,60,200, 110,70,30) #テストデータの作成 table <- matrix(data, nrow=3) colnames(table) <- c("bush", "forest", "grassland", "wetland") rownames(table) <- c("endemic", "invasive", "others") table #テストデータ random.test.2(table) # 参考:各列のカテゴリーの出現比率が全体のカテゴリーごとの出現比率との差を出すには次のようにする prop.row <- apply(table,1,sum) / sum(table) prop.each <- t(t(table) / apply(table,2,sum)) t(t(prop.each) - prop.row)