gpt4 book ai didi

r - R创建缺少值的p值矩阵

转载 作者:行者123 更新时间:2023-12-03 07:52:29 26 4
gpt4 key购买 nike

我有一个缺少许多NA的数据框。我想用此链接所示的p值矩阵创建一个相关矩阵:Link

我创建了这样的相关矩阵:

as.data.frame(round(cor(df, use = "pairwise.complete.obs", method = c("spearman")), 1))

现在,我试图创建一个矩阵,该矩阵显示每个相关性的p值。我已将此代码成功用于其他数据帧,其中包含较少的NA。
cor.mtest <- function(mat) {
mat <- as.matrix(mat)
n <- ncol(mat)
p.mat<- matrix(NA, n, n)
diag(p.mat) <- 0
for (i in 1:(n - 1)) {
for (j in (i + 1):n) {
tmp <- cor.test(mat[, i], mat[, j])
p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
}
}
colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
p.mat
}

p.mat <- cor.mtest(df)

但是现在我得到一个错误:

Error in cor.test.default(mat[, i], mat[, j]) : not enough finite observations



我还尝试将“Hmisc”包用于rcorr功能。但是该软件包无法正确加载。任何想法如何解决这个问题?
structure(list(V1 = c(21L, 18L, 11L, 20L, 17L, 18L, 20L, 23L, 
10L, 25L, 11L, 24L, 13L, 17L, 30L, 12L, 24L, 27L, 19L, 24L, 14L,
14L, 10L, 21L, 12L, 14L, 19L, 19L, 16L, 15L, 25L, 15L, 20L, 18L,
21L, 9L, 18L, 10L, 21L, 17L, 15L, 6L, 21L, 27L, 16L, 15L, 20L,
12L, 20L, 11L, 17L, 14L, 22L, 14L, 18L, 17L, 19L, 18L, 16L, 13L,
11L, 19L, 14L, 9L, 13L, 13L, 8L, 7L, 29L, 14L, 16L, 13L, 8L,
28L, 12L, 33L, 20L, 13L, 12L, 14L, 16L, 15L, 23L, 19L, 20L, 23L,
21L, 14L, 12L, 30L, 11L, 12L, 14L, 13L, 15L, 13L, 6L, 15L, 19L,
15L, 18L, 23L, 19L, 11L, 18L, 9L, 18L, 17L, 15L, 8L, 13L, 8L,
20L, 17L, 25L, 11L, 25L, 19L, 13L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62 = c(28.8, 19.5, 26,
29.8, 13, 7.1, 22.6, 11, 21.2, 0.1, 31.7, 7.2, 5.3, 18.4, -1.4,
0.9, 3.2, 5, 31.9, 8.7, 7.9, 30.6, 7.9, 17.2, 24.7, 26.1, 22,
29, -6.3, 30.9, 5.7, 11.7, 28.1, 22.9, 12.2, 29.7, 2.7, 5.5,
19.7, 17.8, 24, 28.6, 24.4, 20, 29.1, 13.7, 8.7, 12, 8.8, 10.4,
9.7, 10, 19.6, -0.5, 25.6, 17.9, 14.2, 12, 3.6, 2.9, 5.9, 26.7,
8.7, 20.9, 0.8, 10.5, 14.3, 19.5, -0.3, 28.8, 26.5, 4.9, -0.5,
23.8, -1.3, 12.1, 2.4, 17.2, 22.1, 23.5, 17, -0.9, 19.3, 4.9,
20.1, 12.2, 10.8, 31.6, 26.1, 2.5, 26.7, 7.5, 8.2, 11.8, 22.3,
28.3, 21.4, 25.4, -0.4, 11.4, 27, 9.3, 23.6, 19.9, 23.5, 19.2,
6.7, 18.9, 2.8, 28, 9.6, 15.2, 13.1, 0, 22.7, 5.7, 3, 4.7, 9.9,
21.9, -1.6, 19, 11, 17.2, 12.9, 27.4, 21.5, 14.3, 4.5, 6.1, 23.1,
-0.1, 5.1, 18.7, 3.7, 10.1, 22.6, 16.1, 7.9, 0.9, 30.8, 2.6,
30.3, 25.9, 20.5, 5.2, 26.9, 22.9, 24.8, 19.6, 10.7, 14.9, 21.9,
24.5, 21, 11.3, 1.5, 17.6, -8.8, 5.3, -1.2, 29.1, 22.6, 6.7,
24.6, 22.2, 1.9, 12.8, 19.6, 20.5, 15, 2.9, 27.2, 16.5, -1.4,
17.1, 8.2, 16, 4.2, 6.6, 19.8, -4.8, 21.7, 27.7, 4.3, 0.4, 25.4,
27.2, 28.7, 17.9, 22.7, 8.9, 22.1, 16.3, 5.4, 15.3, 9.9, 30.2,
14.7, 14.2), V73 = c(NA, NA, NA, -0.09275986, NA, NA, 0.52943606,
NA, NA, NA, 0.39573934, NA, NA, 0.06665112, NA, NA, NA, NA, 0.09889552,
NA, NA, 0.52411667, NA, NA, 0.0786277, 0.39117113, NA, 0.30804176,
NA, 0.4984171, NA, NA, 0.69054695, 0.61838979, NA, 0.49298138,
NA, NA, NA, NA, NA, 0.44718356, NA, 0.24114516, 0.00855375, NA,
NA, NA, NA, NA, NA, NA, NA, NA, 0.31341432, NA, NA, NA, NA, NA,
NA, 0.38816502, NA, 0.69810769, NA, NA, NA, 0.46607416, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.39012246, NA, NA, NA, NA,
0.42507386, NA, NA, -0.26830461, NA, NA, 0.29439447, NA, NA,
NA, 0.18582551, -0.00246774, 0.33244636, 0.26097549, NA, NA,
0.56932173, NA, 0.33573443, NA, NA, NA, NA, NA, NA, 0.74612433,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 0.02980432, NA, NA, NA, NA, NA, NA, 0.60470877,
NA, NA, 0.29230953, NA, -0.11296095, 0.09783287, NA, NA, 0.32181372,
NA, NA, NA, NA, NA, NA, 0.3255947, 0.4099077, NA, NA, NA, NA,
NA, NA, 0.42345733, 0.29293533, NA, 0.52832981, NA, NA, NA, NA,
NA, NA, NA, 0.55373453, NA, NA, NA, NA, NA, NA, NA, 0.4070331,
NA, 0.30780722, 0.59547858, NA, NA, 0.66333634, NA, 0.38209532,
NA, NA, NA, NA, NA, NA, NA, NA, 0.35778449, NA, NA), V77 = c(NA,
NA, 0.45406227, NA, 0.87348132, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 0.78536916, NA, -0.01870051, NA, NA, NA, NA,
NA, NA, -0.00150528, NA, NA, NA, NA, -0.49992833, NA, NA, NA,
NA, NA, NA, NA, -0.12002325, -0.16249647, NA, 0.51132754, NA,
NA, NA, -0.20643247, 0.59529347, NA, 0.32442411, NA, NA, NA,
NA, NA, NA, NA, 0.80611793, NA, NA, NA, NA, NA, NA, NA, 0.75247001,
0.65079036, NA, NA, 0.29773326, -0.2164507, NA, NA, 0.36336748,
NA, NA, NA, NA, 0.49664945, NA, NA, NA, 0.35610758, NA, NA, NA,
0.3734933, NA, 0.58752714, NA, NA, NA, -0.38266847, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.28871445, NA, 0.05455121,
NA, NA, NA, NA, NA, NA, 0.0408944, NA, NA, NA, NA, NA, NA, 0.87592639,
NA, NA, NA, NA, NA, NA, NA, 0.28923257, NA, NA, NA, -0.16730842,
NA, -0.122933, 0.25704385, NA, NA, NA, NA, NA, NA, NA, 0.92475694,
NA, NA, NA, 0.15886697, 0.51925536, NA, NA, 0.25372613, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.89195925,
NA, NA, NA, -0.60877514, NA, 0.33866615, NA, NA, 0.60955791,
NA, NA, NA, NA, NA, NA, NA, NA, NA, -0.05461735, NA, NA, 0.33697054,
NA, -0.12079077, -0.14805299, -0.24541818, NA, 0.36340054, NA
), V81 = c(NA, NA, -0.08490089, NA, 0.0555794, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, -0.22856711, NA, -0.57790508,
NA, NA, NA, NA, NA, NA, 0.04856018, NA, NA, NA, NA, -0.38039271,
NA, NA, NA, NA, NA, NA, NA, -0.63132241, -0.35266074, NA, 0.01961822,
NA, NA, NA, -0.34551275, -0.39085104, NA, -0.27725445, NA, NA,
NA, NA, NA, NA, NA, -0.21599455, NA, NA, NA, NA, NA, NA, NA,
-0.19924471, -0.18365343, NA, NA, -0.53484587, -0.32543563, NA,
NA, -0.19992419, NA, NA, NA, NA, -0.18500223, NA, NA, NA, -0.12990151,
NA, NA, NA, -0.39083879, NA, -0.59264661, NA, NA, NA, 0.13154274,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, -0.23261324,
NA, -0.03944042, NA, NA, NA, NA, NA, NA, -0.22193873, NA, NA,
NA, NA, NA, NA, -0.20022085, NA, NA, NA, NA, NA, NA, NA, 0.08615186,
NA, NA, NA, -0.74607469, NA, 0.23032189, 0.0449706, NA, NA, NA,
NA, NA, NA, NA, -0.04848046, NA, NA, NA, -0.6370161, -0.02900035,
NA, NA, -0.23145663, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 0.14884929, NA, NA, NA, 0.22450133,
NA, 0.24769837, NA, NA, -0.29667428, NA, NA, NA, NA, NA, NA,
NA, NA, NA, -0.03071992, NA, NA, 0.07786378, NA, 0.23027039,
-0.20214392, -0.3032353, NA, -0.47432158, NA), V89 = c(0.0834995,
0.00066815, NA, NA, NA, NA, NA, NA, 0.02511399, NA, NA, NA, 0.052432,
NA, NA, NA, -0.14814967, NA, NA, NA, NA, NA, -0.33114922, 0.34514567,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.19468406, NA, NA, NA,
-0.38972029, NA, NA, NA, NA, NA, NA, NA, NA, NA, -0.23425484,
NA, -0.11003854, NA, -0.26367322, NA, NA, 0.29238575, 0.07886438,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, -0.15248164, NA, -0.15641155, NA, NA, -0.08752716, NA,
NA, NA, NA, 0.34809891, NA, NA, NA, NA, NA, NA, NA, -0.27401719,
NA, NA, NA, NA, NA, -0.32273288, NA, 0.02669399, NA, 0.0727079,
0.08290143, NA, -0.16476099, NA, NA, NA, NA, -0.1029079, -0.11614262,
NA, NA, -0.14913232, NA, -0.29380582, -0.537503, 0.11869562,
NA, NA, NA, -0.17315201, NA, 0.10272535, 0.0932595, 0.0793467,
-0.0845297, NA, NA, NA, -0.02889606, NA, NA, NA, NA, NA, 0.15552849,
0.04599214, NA, 0.19864881, NA, NA, NA, NA, NA, -0.11474285,
NA, NA, NA, 0.10901186, NA, NA, NA, 0.13339891, NA, 0.07056403,
NA, NA, NA, NA, NA, NA, NA, -0.25760406, 0.2062942, -0.00981489,
0.3282743, 0.06509166, NA, NA, NA, -0.26049214, NA, -0.13281234,
NA, 0.32791015, -0.13518787, NA, NA, NA, NA, NA, NA, NA, NA,
0.05660112, NA, NA, 0.12368526, -0.15672689, NA, -0.42175072,
NA, NA, NA, NA, NA, -0.22635573), V90 = c(-0.04245051, 0.3507695,
NA, NA, NA, NA, NA, NA, 0.32893767, NA, NA, NA, -0.35288827,
NA, NA, NA, -0.02734148, NA, NA, NA, NA, NA, -0.01271804, -0.26617777,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, -0.37528838, NA, NA,
NA, 0.14921273, NA, NA, NA, NA, NA, NA, NA, NA, NA, -0.46296948,
NA, -0.20223671, NA, 0.12754582, NA, NA, 0.05006781, 0.22653775,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, -0.26092513, NA, 0.54215354, NA, NA, -0.23136087, NA,
NA, NA, NA, -0.04596987, NA, NA, NA, NA, NA, NA, NA, 0.14239809,
NA, NA, NA, NA, NA, 0.11650203, NA, 0.17058915, NA, -0.18403288,
0.10295627, NA, -0.15530088, NA, NA, NA, NA, -0.45405281, -0.10929859,
NA, NA, 0.14782657, NA, -0.15852471, -0.05266618, -0.18175069,
NA, NA, NA, -0.11917474, NA, 0.16136416, -0.14499177, -0.17504283,
0.13272865, NA, NA, NA, -0.17429991, NA, NA, NA, NA, NA, -0.22030747,
0.29022488, NA, 0.05889091, NA, NA, NA, NA, NA, 0.30446594, NA,
NA, NA, 0.23796595, NA, NA, NA, 0.14051101, NA, -0.05704354,
NA, NA, NA, NA, NA, NA, NA, 0.25256272, -0.14193822, 0.06924969,
0.00445279, 0.29815696, NA, NA, NA, 0.25643083, NA, 0.35649173,
NA, -0.25180143, -0.05787895, NA, NA, NA, NA, NA, NA, NA, NA,
0.03069952, NA, NA, -0.18662018, -0.15144552, NA, 0.06595208,
NA, NA, NA, NA, NA, 0.32091592)), .Names = c("V1", "V2", "V3",
"V6", "V40", "V29", "V56", "V62", "V73", "V77", "V81", "V89",
"V90"), class = "data.frame", row.names = c(NA, -200L))

最佳答案

发生此错误的原因是,每对数据中至少需要3个非NA。
为了解决这个问题,当您发现这样的错误时,可能需要设置p值= NA。您可以使用以下功能:

cor.mtest <- function(mat) {
mat <- as.matrix(mat)
n <- ncol(mat)
p.mat<- matrix(NA, n, n)
diag(p.mat) <- 0
for (i in 1:(n - 1)) {
for (j in (i + 1):n) {
error <- try(tmp <- cor.test(mat[, i], mat[, j]),
silent =T)
if (class(error) == "try-error") {
p.mat[i, j] <- NA
} else {
p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
}
}
}
colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
p.mat
}

关于r - R创建缺少值的p值矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60512043/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com