如何在r中计算Spearman的Rho趋势测试

时间:2016-09-14 13:57:24

标签: r statistics trend

我试图通过这样做来计算Spearman的rho Trend测试:

test=cor.test(timeseries,1:n,method="spearman")
D=test$p.value
Z=D*sqrt((n-2)/(1-D^2))
pt(Z,n-2)

首先,我计算相关系数然后我计算测试本身,但它显示p-val = 1,但我不确定。 有一些想法吗?

DATA:

c(1.87475, 2.84528, 5.83542, 10.8843, 9.48484, 10.0332, 7.16305, 
9.15246, 8.68957, 11.2653, 8.76863, 4.0485, 2.8528, 3.06203, 
3.5859, 7.12124, 9.51561, 9.22085, 7.81098, 6.73397, 8.58113, 
9.28353, 7.92128, 5.04925, 2.2428, 5.99153, 4.70723, 11.9191, 
12.9951, 10.2102, 7.5542, 8.72667, 8.2311, 10.2064, 7.785, 5.25303, 
3.96196, 5.74908, 5.26922, 12.4321, 11.7044, 7.14123, 7.08435, 
6.12911, 8.59358, 10.3117, 5.85105, 3.75807, 2.01575, 2.96886, 
5.07536, 11.4144, 9.51971, 7.5381, 8.22469, 7.20163, 8.34905, 
8.94548, 6.15442, 6.34244, 4.3739, 6.79443, 4.20708, 7.69045, 
9.65889, 10.433, 8.44214, 8.89671, 10.4557, 10.1401, 8.95852, 
3.38102, 2.17563, 2.27429, 4.9568, 7.20546, 9.2164, 7.46578, 
7.55419, 8.58356, 8.98944, 9.16541, 6.9009, 3.97315, 3.09716, 
4.49597, 4.27927, 10.046, 9.51468, 9.45756, 6.27443, 7.19989, 
7.55478, 11.4173, 6.34512, 3.20048, 2.3346, 3.33158, 4.66627, 
8.50802, 9.19616, 6.36604, 9.81922, 7.56599, 9.23924, 11.0332, 
7.07433, 3.9774, 1.9611, 3.76258, 2.31831, 8.63655, 8.92169, 
9.55649, 8.78941, 10.7815, 9.44618, 10.553, 10.0151, 5.71694, 
3.93641, 4.37566, 5.11101, 5.75034, 9.61361, 9.02001, 6.91823, 
8.05951, 9.45761, 9.05592, 6.20638, 3.86533, 3.66949, 3.73916, 
5.74735, 10.0382, 9.1558, 7.4962, 7.15291, 6.52209, 6.69322, 
10.9919, 7.80923, 4.86516, 1.92499, 2.95135, 5.23056, 7.86658, 
9.41144, 8.60926, 7.72269, 6.09726, 8.25809, 7.08823, 8.36145, 
2.95296, 2.54423, 2.73502, 3.29865, 7.45659, 9.55943, 7.027, 
7.93852, 7.54321, 8.27874, 6.41406, 7.52643, 4.27732, 4.21357, 
2.9732, 6.63207, 9.51842, 10.0635, 7.22504, 7.09003, 6.64774, 
9.19645, 8.34171, 8.48366, 4.19497, 3.01118, 4.07612, 6.98124, 
9.93061, 10.1801, 8.25671, 7.2361, 8.35503, 8.14476, 8.91099, 
9.02474, 3.48103, 1.85194, 1.59392, 5.19668, 9.30066, 9.28724, 
10.0509, 9.0745, 8.85791, 8.15646, 9.7758, 6.412, 5.88048, 3.58275, 
5.99374, 6.65659, 8.27649, 11.0066, 9.92089, 8.5654, 8.75503, 
8.3944, 9.63843, 7.50948, 5.10784, 3.81126, 5.71426, 3.25419, 
7.82241, 8.74966, 8.39693, 5.91505, 4.89234, 8.0544, 6.70593, 
7.04475, 1.57748, 1.64281, 4.33555, 5.11418, 9.60619, 11.1262, 
8.80311, 9.4623, 8.70307, 8.50093, 9.58583, 7.89205, 6.20712, 
4.8317, 8.51534, 5.2159, 10.6988, 8.93205, 9.88138, 6.85288, 
7.71208, 10.5172, 10.4221, 8.69456, 6.54457, 3.46086, 5.82024, 
5.669, 8.66004, 11.7918, 9.41083, 8.77455, 8.43954, 10.7036, 
9.47817, 7.23946, 5.07723, 2.59331, 2.09574, 6.07388, 6.69522, 
10.5635, 8.25802, 8.29898, 6.15296, 9.65629, 9.25886, 8.72526, 
6.65301, 2.71164, 2.57094, 6.10028, 9.82986, 9.97061, 8.72147, 
7.78037, 7.16719, 8.05045, 8.41613, 6.90927, 5.00483, 1.58178, 
3.20776, 5.53643, 9.84159, 9.91302, 9.99696, 7.65637, 7.49268, 
8.97894, 10.0529, 8.63098, 5.56845, 2.38601, 3.09946, 4.22309, 
9.87019, 11.8278, 8.0656, 8.48857, 7.04588, 10.0083, 9.65277, 
8.26655, 4.26367, 4.21172, 4.45347, 5.1601, 10.0207, 10.7059, 
8.62925, 6.36322, 7.66119, 7.69737, 11.5594, 9.92093, 4.50141
)

1 个答案:

答案 0 :(得分:1)

#Figure out length of vector supplied by OP and create new vector
dn<-1:length(d) #if d refers to data supplied by OP

#Run the test
cor.test(d,dn,method="spearman") 

    Spearman's rank correlation rho

data:  d and dn
S = 5447000, p-value = 0.4829
alternative hypothesis: true rho is not equal to 0
sample estimates:
       rho 
0.03909714 

请注意上面0.4829的p值。现在让我们尝试一种使用Hmisc包找到spearman的rho的替代方法:

rcorr(d,dn,type="spearman")
     x    y
x 1.00 0.04
y 0.04 1.00

n= 324 


P
  x      y     
x        0.4831
y 0.4831 

再次注意,它们都具有相同的p值0.4831。所以测试是正确的。现在我们知道Spearman测试的相关系数是正确的,让我们继续进行趋势测试。

OP担心趋势测试的p值代码是错误的。我们没有编写自己的代码,为什么不使用CRAN的用户编写的包,并检查我们的结果:

library(trend)

partial.cor.trend.test(d,1:length(d), "spearman")
    Spearman's partial correlation trend test

data:  t AND  data  .  1:length(data)
t = 0, df = 322, p-value = 1
alternative hypothesis: true correlation is not equal to 0
sample estimates:
r(tdata.1:length(data)) 
                      0 

请注意,部分相关性测试的p值1与OP的方法相同。两种方法都没有错。 p值为1可能是从接近1的某个值向上舍入的人为因素。您可以在posts处查看各种Cross Validated以更好地了解正在发生的事情。

另外,您也可以将Pearson的相关性用于趋势测试,如下所示:

partial.cor.trend.test(d,dn, "pearson")

    Pearson's partial correlation trend test

data:  t AND  data  .  dn
t = 0, df = 322, p-value = 1
alternative hypothesis: true correlation is not equal to 0
sample estimates:
r(tdata.dn) 
          0 

再次注意,p值为1(或至少舍入为1)。我认为结果证实您的数据没有时间趋势......让我们使用Partial Mann-Kendall测试以另一种方式检查,如下所示:

partial.mk.test(d,dn)

    Partial Mann-Kendall trend test

data:  t AND  data  .  dn
Z = -0.2954, p-value = 0.7677
alternative hypothesis: true trend exists in series
sample estimates:
           S         varS 
   -575.1424 3790753.7082 

请注意0.7677的p值。也不重要。简而言之,你必须拒绝你的时间序列中存在真实趋势的替代假设。