-1

I am trying to compute Spearman's Rho Trend test in r by doing this:

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

First, I calculate the Correlation coefficient and then I compute the test itself but it show a p-val=1, but I am not sure about it. Have some Idea about it?

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
)
Erincon
  • 389
  • 1
  • 7
  • 21

1 Answers1

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 

Note the p-value of 0.4829 above. Now let's try an alternative method for finding spearman's rho using the Hmisc package:

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 

Again note that they both have the same p-value of 0.4831. So the test is correct. Now that we know the correlation coefficient for the Spearman's test is right let's move on to the trend test.

The OP is worried code for p-value from trend test is wrong. Instead of writing our own code why don't we make use of a user written package from CRAN and check our results against that:

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 

Note that the partial correlation test's p-value of 1 is identical to that of the OP's method. There is nothing wrong with either approaches. The p-value of 1 is likely an artifact of rounding up from some value near 1. You can look at various posts at Cross Validated to get a better sense of what's going on.

And as an aside, you can use Pearson's correlation for the trend test as well, like in the following:

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 

Again, note a p-value of 1 (or at least rounded to 1). I think the results confirm that there is no time trend in your data...Let's check this another way using a Partial Mann-Kendall Test, like in the following:

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 

Note the p-value of 0.7677. Also not significant. Simply put, you must reject your alternative hypothesis that a true trend exists in your time series.

Community
  • 1
  • 1
Cyrus Mohammadian
  • 4,982
  • 6
  • 33
  • 62
  • Actually, I was not asking for the Correlation test... I am looking for a way to compute the Spearman's Rho Trend Test. Look that `Z` is my statistic – Erincon Sep 16 '16 at 16:14