繁体   English   中英

R-运行Spearman相关性时p值不一致

[英]R - Inconsistent p-value in running Spearman correlation

我的问题是,当出于某种奇怪的原因计算运行相关性时,对于相同的估计/相关性值,我没有获得相同的p值。

我的目标是在同一data.frame(以下示例中的subject1和subject2)上的两个向量上计算正在运行的Spearman相关性。 另外,我的窗口(向量的长度)和步幅(每个窗口之间的跳跃/步长)是恒定的。 这样,当查看下面的公式时(来自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)

我的问题是,当我用估算值(Spearman rho; estimate ),窗口编号( fromIndex )绘制Results并为p值上色时,我应该在同一区域获得类似颜色的“隧道” /“路径”-我不。 例如,在下面的图片中,红色圆圈中相同高度的点应具有相同的颜色-但不同。 在此处输入图片说明

图形代码:

Results %>% 
  ggplot(aes(fromIndex, estimate, color = p.value)) + 
  geom_line()

到目前为止我发现的原因可能是:1.在小的样本或许多联系中,像Hmisc::rcorr()这样的函数往往不会给出相同的Hmisc::rcorr() 这就是为什么我使用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 ))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM