正如 Paul Hiemstra 在回答中所指出的,如果没有缺失值,则无法知道相关性会更高还是更低。然而,对于某些应用程序,对不匹配缺失值的观察到的相关性进行惩罚可能是合适的。例如,如果我们比较两个单独的编码器,当且仅当编码器 A 也说“NA”时,我们可能希望编码器 B 说“NA”,而且我们希望它们的非 NA 值相关。
在这些假设下,惩罚不匹配缺失值的一种简单方法是计算完整案例的相关性,然后乘以根据其 NA 状态匹配的观测值的比例。然后可以将惩罚项定义为:1 - mean((is.na(coderA) & !is.na(coderB)) | (!is.na(coderA) & is.na(coderB)))。下面是一个简单的例子。
fun = function(x1, x2, idx_rm) {
temp = x2
# remove 'idx_rm' points from x2
temp[idx_rm] = NA
# calculate correlations
r_full = round(cor(x1, x2, use = 'pairwise.complete.obs'), 2)
r_NA = round(cor(x1, temp, use = 'pairwise.complete.obs'), 2)
penalty = 1 - mean((is.na(temp) & !is.na(x1)) |
(!is.na(temp) & is.na(x1)))
r_pen = round(r_NA * penalty, 2)
# plot
plot(x1, temp, main = paste('r_full =', r_full,
'; r_NA =', r_NA,
'; r_pen =', r_pen),
xlim = c(-4, 4), ylim = c(-4, 4), ylab = 'x2')
points(x1[idx_rm], x2[idx_rm], col = 'red', pch = 16)
regr_full = as.numeric(summary(lm(x2 ~ x1))$coef[, 1])
regr_NA = as.numeric(summary(lm(temp ~ x1))$coef[, 1])
abline(regr_full[1], regr_full[2])
abline(regr_NA[1], regr_NA[2], lty = 2)
}
运行一个简单的模拟来说明缺失值和惩罚的可能影响:
set.seed(928)
x1 = rnorm(20)
x2 = x1 * rnorm(20, mean = 1, sd = .8)
# A case when NA's artifically inflate the correlation,
# so penalization makes sense:
myfun(x1, x2, idx_rm = c(13, 19))
# A case when NA's DEflate the correlation,
# so penalization may be misleading:
myfun(x1, x2, idx_rm = c(6, 14))
# When there are a lot of NA's, penalization is much stronger
myfun(x1, x2, idx_rm = 7:20)
# Some NA's in x1:
x1[1:5] = NA
myfun(x1, x2, idx_rm = c(6, 14))