Last active
March 24, 2016 19:58
-
-
Save rmflight/62879686cd2df22c27e0 to your computer and use it in GitHub Desktop.
example of why pairwise.complete.obs is useful
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # use case is -omics data where some samples have missing data, can | |
| # be coded as NA or 0, but can always set 0 to NA for my use cases | |
| # want the pairwise correlations, where each pairwise comparison | |
| # removes NA in either of the pairs being compared | |
| set.seed(1234) | |
| data_matrix <- matrix(rnorm(200, 0, 1), nrow = 20) | |
| na_locs <- sample(200, 10) | |
| data_matrix[na_locs] <- NA | |
| str(data_matrix) | |
| sum(is.na(data_matrix)) | |
| cor(data_matrix) | |
| comp_cor <- cor(data_matrix, use = "complete.obs") | |
| comp_cor[1,2] | |
| pc_cor <- cor(data_matrix, use = "pairwise.complete.obs") | |
| pc_cor[1,2] | |
| cor(data_matrix[,1], data_matrix[,2]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| > # use case is -omics data where some samples have missing data, can | |
| > # be coded as NA or 0, but can always set 0 to NA for my use cases | |
| > set.seed( .... [TRUNCATED] | |
| > data_matrix <- matrix(rnorm(200, 0, 1), nrow = 20) | |
| > na_locs <- sample(200, 10) | |
| > data_matrix[na_locs] <- NA | |
| > str(data_matrix) | |
| num [1:20, 1:10] -1.207 0.277 1.084 -2.346 0.429 ... | |
| > sum(is.na(data_matrix)) | |
| [1] 10 | |
| > cor(data_matrix) | |
| [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] | |
| [1,] 1 NA NA NA NA NA NA NA NA NA | |
| [2,] NA 1 NA NA NA NA NA NA NA NA | |
| [3,] NA NA 1.00000000 NA NA NA NA NA -0.33585598 -0.05725705 | |
| [4,] NA NA NA 1 NA NA NA NA NA NA | |
| [5,] NA NA NA NA 1 NA NA NA NA NA | |
| [6,] NA NA NA NA NA 1 NA NA NA NA | |
| [7,] NA NA NA NA NA NA 1 NA NA NA | |
| [8,] NA NA NA NA NA NA NA 1 NA NA | |
| [9,] NA NA -0.33585598 NA NA NA NA NA 1.00000000 0.08487323 | |
| [10,] NA NA -0.05725705 NA NA NA NA NA 0.08487323 1.00000000 | |
| > comp_cor <- cor(data_matrix, use = "complete.obs") | |
| > comp_cor[1,2] | |
| [1] -0.6769361 | |
| > pc_cor <- cor(data_matrix, use = "pairwise.complete.obs") | |
| > pc_cor[1,2] | |
| [1] -0.415614 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment