r - R - 运行 Spearman 相关的 p 值不一致
问题描述
我的问题是当我出于某种奇怪的原因计算运行相关性时,对于相同的估计/相关值,我没有得到相同的 p 值。
我的目标是计算同一 data.frame 中两个向量的运行 Spearman 相关性(下例中的 subject1 和 subject2)。此外,我的窗口(向量的长度)和步幅(每个窗口之间的跳跃/步长)是恒定的。因此,当查看下面的公式时(来自wiki),我应该得到相同的临界 t,因此对于相同的 Spearman 相关性,相同的 p 值。这是因为n
状态相同(窗口大小相同)并且r
相同。但是,我的最终 p 值不同。
#Needed pkgs
require(tidyverse)
require(pspearman)
require(gtools)
#Sample data
set.seed(528)
subject1 <- rnorm(40, mean = 85, sd = 5)
set.seed(528)
subject2 <- c(
lag(subject1[1:21]) - 10,
rnorm(n = 6, mean = 85, sd = 5),
lag(subject1[length(subject1):28]) - 10)
df <- data.frame(subject1 = subject1,
subject2 = subject2) %>%
rowid_to_column(var = "Time")
df[is.na(df)] <- subject1[1] - 10
rm(subject1, subject2)
#Function for Spearman
psSpearman <- function(x, y)
{
out <- pspearman::spearman.test(x, y,
alternative = "two.sided",
approximation = "t-distribution") %>%
broom::tidy()
return(data.frame(estimate = out$estimate,
statistic = out$statistic,
p.value = out$p.value )
}
#Running correlation along the subjects
dfRunningCor <- running(df$subject1, df$subject2,
fun = psSpearman,
width = 20,
allow.fewer = FALSE,
by = 1,
pad = FALSE,
align = "right") %>%
t() %>%
as.data.frame()
#Arranging the Results into easy to handle data.frame
Results <- do.call(rbind.data.frame, dfRunningCor) %>%
t() %>%
as.data.frame() %>%
rownames_to_column(var = "Win") %>%
gather(CorValue, Value, -Win) %>%
separate(Win, c("fromIndex", "toIndex")) %>%
mutate(fromIndex = as.numeric(substring(fromIndex, 2)),
toIndex = as.numeric(toIndex, 2)) %>%
spread(CorValue, Value) %>%
arrange(fromIndex) %>%
select(fromIndex, toIndex, estimate, statistic, p.value)
我的问题是,当我绘制Results
带有估计值(Spearman rho; estimate
)、窗口编号(fromIndex
)并且我为 p 值着色时,我应该在同一区域获得相同颜色的“隧道”/“路径” - 我没有吨。例如,在下图中,红色圆圈中相同高度的点应该具有相同的颜色 - 但不是。
图表代码:
Results %>%
ggplot(aes(fromIndex, estimate, color = p.value)) +
geom_line()
到目前为止,我发现这可能是由于: 1. 像这样的函数Hmisc::rcorr()
在小样本或许多关系中往往不会给出相同的 p.value。这就是为什么我使用pspearman::spearman.test
我在这里读到的内容来解决这个问题的原因。2. 样本量小 - 我尝试使用更大的样本量。我仍然遇到同样的问题。3. 我尝试四舍五入我的 p 值 - 我仍然遇到同样的问题。
谢谢您的帮助!
编辑。
可能是ggplot的“伪”着色吗?难道ggplot
只是在下一点之前插入“最后一个”颜色吗?这就是为什么我从第 5 点到第 6 点得到“浅蓝色”,而从第 7 点到第 8 点得到“深蓝色”?
解决方案
p.value
您为变量获得的结果与该estimate
值一致。您可以按如下方式检查:
Results$orderestimate <- order(-abs(Results$estimate))
Results$orderp.value <- order(abs(Results$p.value))
identical(Results$orderestimate ,Results$orderp.value)
我认为您不应该p.value
在图表中包含颜色,这是不必要的视觉干扰,而且很难解释。
如果我是你,我只会显示p.value
并且可能包括一个点来指示estimate
变量的符号。
p <- Results %>%
ggplot(aes(fromIndex, p.value)) +
geom_line()
# If you want to display the sign of the estimate
Results$estimate.sign <- as.factor(sign(Results$estimate))
p+geom_point( aes(color = estimate.sign ))
推荐阅读
- javascript - 计时器未正常运行 - Javascript
- python - 有没有办法进行矩阵逐张量乘法?
- sql-server - SQL-计算一个项目使用多长时间的价格-每月价格、每周价格和每小时价格
- c - 由于 getchar 理解 c 循环
- visual-studio-code - 注释空行
- perl - 如何在有向图中找到从源到汇的所有路径?
- encryption - 在 JavaScript 中为 RSA 生成非常大的素数
- html - Vue js - 带有 v-for 的图像映射区域
- mysql - 使用 MAX() 函数的相等值的附加条件
- ios - 自定义字体在故事板中可见但在模拟器中不可见