1

変数間の相関関係を実行していますが、その一部にはデータが欠落しているため、各相関関係のサンプル サイズは異なる可能性があります。印刷と要約を試みましたが、どちらも相関ごとに n がどれだけ大きいかを示していません。これは、どこにも答えが見つからない、かなり単純な問題です。

4

3 に答える 3

3

このような..?

x <- c(1:100,NA)
length(x)
length(x[!is.na(x)])

このように自由度を取得することもできます...

y <- c(1:100,NA)
x <- c(1:100,NA)

cor.test(x,y)$parameter

しかし、正確な助けを得るために、相関関係をどのように推定しているかのコードを示すのが最善だと思います。

于 2013-01-01T23:54:26.427 に答える
1

行列の列間でペアワイズ サンプル サイズを見つける方法の例を次に示します。データフレームの(特定の)数値列に適用する場合は、それに応じてそれらを組み合わせ、結果のオブジェクトを強制的に行列にして、関数を適用します。

# Example matrix:
xx <- rnorm(3000)
# Generate some NAs
vv <- sample(3000, 200)
xx[vv] <- NA
# reshape to a matrix
dd <- matrix(xx, ncol = 3)
# find the number of NAs per column
apply(dd, 2, function(x) sum(is.na(x)))
# tack on some column names
colnames(dd) <- paste0("x", seq(3))

# Function to find the number of pairwise complete observations 
# among all pairs of columns in a matrix. It returns a data frame
# whose first two columns comprise all column pairs

pairwiseN <- function(mat)
{
    u <- if(is.null(colnames(mat))) paste0("x", seq_len(ncol(mat))) else colnames(mat)
    h <- expand.grid(x = u, y = u)

    f <- function(x, y)
           sum(apply(mat[, c(x, y)], 1, function(z) !any(is.na(z))))
    h$n <- mapply(f, h[, 1], h[, 2])
    h
}

# Call it
pairwiseN(dd)

機能は簡単に改善できます。たとえば、h <- expand.grid(x = u[-1], y = u[-length(u)])計算回数を減らすように設定したり、3 列のデータ フレームの代わりに nxn 行列を返したりすることができます。

于 2013-01-02T04:07:15.840 に答える
-1

あなたの変数がaおよびという名前のベクトルである場合、b何かsum(is.na(a) | is.na(b))助けになりますか?

于 2013-01-01T23:55:07.723 に答える