0

I have a long vector of financial time series information, price and volume. I expand the price vector based on the volume vector so I essentially get a price per share traded, and so only one vector is analyzed. I would like to split this vector into lengths of 100 (could be any number) each, and apply the Mann-Kendall trend analysis on each of these new vectors. Below is the code I have for this.

I am getting an error from the Mann Kendall test, as this split method is producing outputs as a list.

Is there a way to create a new vector and run the Mann Kendall test on each vector, with the output from each test easily acessible?

library(Kendall)

priceexp <- rep(price, volume)

max <- 100
xx <- seq_along(priceexp)
d1 <- split(priceexp, ceiling(xx/max))

for(i in (1:length(d1))) {
    MannKendall(d1[i])
}

A sample of the two vectors for input:

"price","volume"
125,4020
125,1100
125,191
124.8,329
125.5,400
125.6,100
125.7,600
125.2,686
125.2,898
125.2,1416
125.6,150
125.6,500
125.6,200
125.6,41
125.5,400
125.7,300
125.7,14
125.7,1200
125.7,300
125.7,686
125.8,1000
125.8,1700
125.8,144
125.8,225
125.9,500
125.9,446
126,500
126,225
126,500
126,250
126,28
126,340
126,600
125.9,275
125.9,323
125.9,152
125.8,1931
125.9,196
125.9,571
125.8,214
125.8,300
125.7,353
125.8,432
125.8,1356
126,400
126,2133
126,300
126,190
126,376
125.8,186
126,750
126,431
126,1403
126,39
125.9,259
126.1,900
126.1,307
126.1,124
126.1,750
126.2,100
126.2,117
126,200
126,94
126,453
126,149
126,661
126,600
126,549
126,315
126,318
126,297
125.9,300
125.9,454
125.9,370
125.8,114
125.8,1100
125.8,7344
125.8,2656
125.8,333
126,120
125.9,878
125.9,462
125.9,899
125.9,45
125.7,2000
125.7,889
125.7,4611
125.7,2500
125.9,652
125.9,1610
125.9,332
125.9,750
125.9,627
125.9,473
125.9,182
125.9,32
125.9,1305
125.9,98
125.9,330
125.9,373
125.9,636
125.9,1291
125.9,1675
125.9,1029
125.9,314
125.9,400
125.9,699
125.9,300
125.8,300
125.8,7
126,659
125.9,750
126,441
126,2000
126,86
126,300
126,1300
125.9,243
125.9,456
125.9,64
126,400
126,2000
125.9,319
125.9,423
125.8,447
125.8,387
125.8,352
125.8,200
125.8,1123
125.8,379
125.8,300
125.8,600
125.8,61
125.8,340
125.8,200
Morten
  • 223
  • 1
  • 5
  • 15
  • 2
    Please don't cross post, particularly without clarifying your original question (hence why you didn't get a response there): http://www.talkstats.com/showthread.php/38538-Split-Vector-and-apply-Mann-Kendall-test – Tyler Rinker Feb 11 '13 at 21:03
  • @TylerRinker I think the question is clear enough. Generally I ask my questions here at stackoverflow, but was on talkstats earlier and decided to post. Having no response but many views, I thought it was good to spread out the possibility of it getting answered. But thank you, in the future I'll stick to one place. – Morten Feb 11 '13 at 21:58
  • check out: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Tyler Rinker Feb 11 '13 at 23:02

1 Answers1

1

Using lapply for example, here I do it only for the first 5 elements.

   lapply(d1[1:5],MannKendall)
WARNING: Error exit, tauk2. IFAULT =  12
WARNING: Error exit, tauk2. IFAULT =  12
WARNING: Error exit, tauk2. IFAULT =  12
WARNING: Error exit, tauk2. IFAULT =  12
WARNING: Error exit, tauk2. IFAULT =  12
$`1`
tau = 1, 2-sided pvalue =1

$`2`
tau = 1, 2-sided pvalue =1

$`3`
tau = 1, 2-sided pvalue =1

$`4`
tau = 1, 2-sided pvalue =1

$`5`
tau = 1, 2-sided pvalue =1

EDIT

The result of MannKendall is a list, you can unlist it,

   do.call(rbind,lapply(d1[1:5],function(x)unlist(MannKendall(x))))

  tau sl S D varS
1   1  1 0 0    0
2   1  1 0 0    0
3   1  1 0 0    0
4   1  1 0 0    0
5   1  1 0 0    0
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • Great, it works. When I try to write the output of the Kendall function to a table using 'write.table' I get this error message: Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class '"Kendall"' into a data.frame Would you happen to know if I can get this to be read into a more workable format, matrix/ table/ data.frame? – Morten Feb 11 '13 at 22:46
  • Could there be multiple reasons for `WARNING: Error exit, tauk2. IFAULT = 12`? I was getting this message and found it was small sample sizes ([based on this post from A.I. McLeod](http://r.789695.n4.nabble.com/Bug-in-Kendall-for-n-4-tp872310p872313.html), although for `Kendall` and not `MannKendall`). – Roland Jan 01 '18 at 00:11
  • Yes this is MannKendall response for `x` vectors shorted than 4. – geotheory Feb 10 '20 at 15:16