Monday, June 15, 2015

[8] Plot: Time Series

Before you read this section, please see the previous section.

Plot: Cumulative Total Return (x axis: month)

Create the following r code.


sample code(s)

gf <- MCTR_F("AAPL.txt",1,3,"")

plot.zoo(gf[,3],plot.type="single",col=1:1,ylim=c(-10,1100),lwd=3, xlab = "Month", ylab="Cumulative Total Return")

grid()

legend("topleft",col=1:1,lty=1,legend=c("Apple"))





Plot: Cumulative Total Return (x axis: date, yyyy/mm/dd format)

Run the following r code.


sample code(s)

gf <- MCTR_F("AAPL.txt",1,3,"")

# xts: time seris
gf2<- xts(
zoo(
subset(gf[,3],gf[,3]!="DUMMY"),

as.Date(
as.character(
subset(gf[,1],gf[,3]!="DUMMY")
),
format='%Y/%m/%d'
)
)

)

plot.zoo(gf2, plot.type="single",col=1:1,yaxp=c(0,1100,11),lwd=3, xlab = "Year", ylab="Cumulative Total Return")


grid()

legend("topleft",col=1:1,lty=1,legend=c("Apple"))




Plot: Cumulative Total Returns of two stocks (x axis: date, yyyy/mm/dd format)

read.table()
for tab-separated files

Create the following monthly price and total return tab-separated file for Microsoft.

MSFT.txt
Date Price Return Total Return
2007/01/31 3.3490 3.3490 
2007/02/28 -8.7168 -8.4023 
2007/03/30 -1.0650 -1.0650 
2007/04/30 7.4273 7.4273 
2007/05/31 2.5054 2.8372 
2007/06/29 -3.9756 -3.9756 
2007/07/31 -1.6288 -1.6288 
2007/08/31 -0.8969 -0.5463 
2007/09/28 2.5409 2.5409 
2007/10/31 24.9491 24.9491 
2007/11/30 -8.7205 -8.4291 
2007/12/31 5.9524 5.9524 
2008/01/31 -8.4270 -8.4270 
2008/02/29 -16.5648 -16.2390 
2008/03/31 4.3387 4.3387 
2008/04/30 0.4933 0.4933 
2008/05/30 -0.7013 -0.3345 
2008/06/30 -2.8602 -2.8602 
2008/07/31 -6.5067 -6.5067 
2008/08/29 6.1042 6.5314 
2008/09/30 -2.1986 -2.1986 
2008/10/31 -16.3357 -16.3357 
2008/11/28 -9.4492 -8.8492 
2008/12/31 -3.8576 -3.8576 
2009/01/30 -12.0370 -12.0370 
2009/02/27 -5.5556 -4.8769 
2009/03/31 13.7461 13.7461 
2009/04/30 10.2885 10.2885 
2009/05/29 3.1096 3.7696 
2009/06/30 13.7865 13.7865 
2009/07/31 -1.0517 -1.0517 
2009/08/31 4.8044 5.3822 
2009/09/30 4.3408 4.3408 
2009/10/30 7.8149 7.8149 
2009/11/30 6.0584 6.5180 
2009/12/31 3.6382 3.6382 
2010/01/29 -7.5459 -7.5459 
2010/02/26 1.7388 2.2053 
2010/03/31 2.1538 2.1538 
2010/04/30 4.2595 4.2595 
2010/05/28 -15.5068 -15.1227 
2010/06/30 -10.8140 -10.8140 
2010/07/30 12.1686 12.1686 
2010/08/31 -9.0856 -8.6073 
2010/09/30 4.3682 4.3682 
2010/10/29 8.8812 8.8812 
2010/11/30 -5.2785 -4.6913 
2010/12/31 10.5018 10.5018 
2011/01/31 -0.6628 -0.6628 
2011/02/28 -4.1298 -3.5609 
2011/03/31 -4.4771 -4.4771 
2011/04/29 2.0874 2.0874 
2011/05/31 -3.5108 -2.8812 
2011/06/30 3.9584 3.9584 
2011/07/29 5.3846 5.3846 
2011/08/31 -2.9197 -2.3070 
2011/09/30 -6.4286 -6.4286 
2011/10/31 6.9908 6.9908 
2011/11/30 -3.9429 -3.2245 
2011/12/30 1.4855 1.4855 
2012/01/31 13.7519 13.7519 
2012/02/29 7.4839 8.1946 
2012/03/30 1.6226 1.6226 
2012/04/30 -0.7441 -0.7441 
2012/05/31 -8.8240 -8.2204 
2012/06/29 4.7962 4.7962 
2012/07/31 -3.6613 -3.6613 
2012/08/31 4.5809 5.2751 
2012/09/28 -3.4393 -3.4393 
2012/10/31 -4.0995 -4.0995 
2012/11/30 -6.7449 -5.9531 
2012/12/31 0.3558 0.3558 
2013/01/31 2.7717 2.7717 
2013/02/28 1.2750 2.1056 
2013/03/28 2.8957 2.8957 
2013/04/30 15.7140 15.7140 
2013/05/31 5.4381 6.1613 
2013/06/28 -1.0172 -1.0172 
2013/07/31 -7.8304 -7.8304 
2013/08/30 4.8995 5.6481 
2013/09/30 -0.3593 -0.3593 
2013/10/31 6.3852 6.3852 
2013/11/29 7.6967 8.5174 
2013/12/31 -1.8883 -1.8883 
2014/01/31 1.1494 1.1494 
2014/02/28 1.2421 1.9996 
2014/03/31 6.9956 6.9956 
2014/04/30 -1.4394 -1.4394 
2014/05/30 1.3366 2.0386 
2014/06/30 1.8564 1.8564 
2014/07/31 3.5012 3.5012 
2014/08/29 5.2595 5.9097 
2014/09/30 2.0471 2.0471 
2014/10/31 1.2726 1.2726 
2014/11/28 1.8317 2.4794 
2014/12/31 -2.8446 -2.8446 
2015/01/30 -13.0248 -13.0248 
2015/02/27 8.5396 9.3117 
2015/03/31 -7.2862 -7.2862 
2015/04/30 19.6409 19.6409 
2015/05/29 -3.6595 -3.0318 


sample code(s)
# load the tab-separated file with the header in the first row

msft<- read.table("MSFT.txt", header=T, sep="\t")
msft
          Date Price.Return Total.Return
1   2007/01/31       3.3490       3.3490
2   2007/02/28      -8.7168      -8.4023
3   2007/03/30      -1.0650      -1.0650
4   2007/04/30       7.4273       7.4273
5   2007/05/31       2.5054       2.8372
6   2007/06/29      -3.9756      -3.9756
7   2007/07/31      -1.6288      -1.6288
8   2007/08/31      -0.8969      -0.5463
9   2007/09/28       2.5409       2.5409
10  2007/10/31      24.9491      24.9491
11  2007/11/30      -8.7205      -8.4291
12  2007/12/31       5.9524       5.9524
13  2008/01/31      -8.4270      -8.4270
14  2008/02/29     -16.5648     -16.2390
15  2008/03/31       4.3387       4.3387
16  2008/04/30       0.4933       0.4933
17  2008/05/30      -0.7013      -0.3345
18  2008/06/30      -2.8602      -2.8602
19  2008/07/31      -6.5067      -6.5067
20  2008/08/29       6.1042       6.5314
21  2008/09/30      -2.1986      -2.1986
22  2008/10/31     -16.3357     -16.3357
23  2008/11/28      -9.4492      -8.8492
24  2008/12/31      -3.8576      -3.8576
25  2009/01/30     -12.0370     -12.0370
26  2009/02/27      -5.5556      -4.8769
27  2009/03/31      13.7461      13.7461
28  2009/04/30      10.2885      10.2885
29  2009/05/29       3.1096       3.7696
30  2009/06/30      13.7865      13.7865
31  2009/07/31      -1.0517      -1.0517
32  2009/08/31       4.8044       5.3822
33  2009/09/30       4.3408       4.3408
34  2009/10/30       7.8149       7.8149
35  2009/11/30       6.0584       6.5180
36  2009/12/31       3.6382       3.6382
37  2010/01/29      -7.5459      -7.5459
38  2010/02/26       1.7388       2.2053
39  2010/03/31       2.1538       2.1538
40  2010/04/30       4.2595       4.2595
41  2010/05/28     -15.5068     -15.1227
42  2010/06/30     -10.8140     -10.8140
43  2010/07/30      12.1686      12.1686
44  2010/08/31      -9.0856      -8.6073
45  2010/09/30       4.3682       4.3682
46  2010/10/29       8.8812       8.8812
47  2010/11/30      -5.2785      -4.6913
48  2010/12/31      10.5018      10.5018
49  2011/01/31      -0.6628      -0.6628
50  2011/02/28      -4.1298      -3.5609
51  2011/03/31      -4.4771      -4.4771
52  2011/04/29       2.0874       2.0874
53  2011/05/31      -3.5108      -2.8812
54  2011/06/30       3.9584       3.9584
55  2011/07/29       5.3846       5.3846
56  2011/08/31      -2.9197      -2.3070
57  2011/09/30      -6.4286      -6.4286
58  2011/10/31       6.9908       6.9908
59  2011/11/30      -3.9429      -3.2245
60  2011/12/30       1.4855       1.4855
61  2012/01/31      13.7519      13.7519
62  2012/02/29       7.4839       8.1946
63  2012/03/30       1.6226       1.6226
64  2012/04/30      -0.7441      -0.7441
65  2012/05/31      -8.8240      -8.2204
66  2012/06/29       4.7962       4.7962
67  2012/07/31      -3.6613      -3.6613
68  2012/08/31       4.5809       5.2751
69  2012/09/28      -3.4393      -3.4393
70  2012/10/31      -4.0995      -4.0995
71  2012/11/30      -6.7449      -5.9531
72  2012/12/31       0.3558       0.3558
73  2013/01/31       2.7717       2.7717
74  2013/02/28       1.2750       2.1056
75  2013/03/28       2.8957       2.8957
76  2013/04/30      15.7140      15.7140
77  2013/05/31       5.4381       6.1613
78  2013/06/28      -1.0172      -1.0172
79  2013/07/31      -7.8304      -7.8304
80  2013/08/30       4.8995       5.6481
81  2013/09/30      -0.3593      -0.3593
82  2013/10/31       6.3852       6.3852
83  2013/11/29       7.6967       8.5174
84  2013/12/31      -1.8883      -1.8883
85  2014/01/31       1.1494       1.1494
86  2014/02/28       1.2421       1.9996
87  2014/03/31       6.9956       6.9956
88  2014/04/30      -1.4394      -1.4394
89  2014/05/30       1.3366       2.0386
90  2014/06/30       1.8564       1.8564
91  2014/07/31       3.5012       3.5012
92  2014/08/29       5.2595       5.9097
93  2014/09/30       2.0471       2.0471
94  2014/10/31       1.2726       1.2726
95  2014/11/28       1.8317       2.4794
96  2014/12/31      -2.8446      -2.8446
97  2015/01/30     -13.0248     -13.0248
98  2015/02/27       8.5396       9.3117
99  2015/03/31      -7.2862      -7.2862
100 2015/04/30      19.6409      19.6409
101 2015/05/29      -3.6595      -3.0318


Run the following r code.

sample code(s)

hf <- MCTR_F("MSFT.txt",1,3,"")

# xts: time seris
hf2<- xts(
zoo(
subset(hf[,3],hf[,3]!="DUMMY"),

as.Date(
as.character(
subset(hf[,1],hf[,3]!="DUMMY")
),
format='%Y/%m/%d'
)
)

)

# col= 1 black, 2 read, 3 blue
plot.zoo(hf2, plot.type="single",col=2:2,ylim=c(-50,100),lwd=3, xlab = "Year", ylab="Cumulative Total Return")


grid()

legend("topleft",col=1:1,lty=1,legend=c("Microsoft"))





sample code(s)
# combine two data

cbind(gf2, hf2)

> cbind(gf2, hf2)
                      x         x.1
2007-01-31    1.0490000   3.3490000
2007-02-28   -0.2711041  -5.3346930
2007-03-30    9.5120014  -6.3428785
2007-04-30   17.6331924   0.6133168
2007-05-31   42.8465732   3.4679179
2007-06-29   43.8472134  -0.6455527
2007-07-31   55.3040686  -2.2638379
2007-08-31   63.2248867  -2.7977706
2007-09-28   80.8934910  -0.3279591
2007-10-31  123.8920547  24.5393180
2007-11-30  114.7807675  14.0417744
2007-12-31  133.4748560  20.8299969
2008-01-31   59.5473776  10.6476531
2008-02-29   47.3597130  -7.3204193
2008-03-31   69.1418363  -3.2993303
2008-04-30  105.0328882  -2.8223059
2008-05-30  122.4774964  -3.1473653
2008-06-30   97.3595646  -5.9175444
2008-07-31   87.3524479 -12.0392075
2008-08-29   99.8230015  -6.2941363
2008-09-30   33.9697328  -8.3543534
2008-10-31   26.8150792 -23.3253113
2008-11-28    9.2289981 -30.1104079
2008-12-31    0.6009995 -32.8064688
2009-01-30    6.2351585 -40.8945541
2009-02-27    5.2686310 -43.7770676
2009-03-31   23.9037052 -36.0486071
2009-04-30   48.3143459 -29.4689680
2009-05-29   60.0776016 -26.8102303
2009-06-30   67.8805843 -16.7199227
2009-07-31   92.5858910 -17.5957792
2009-08-31   98.2671748 -13.1606193
2009-09-30  118.4698069  -9.3910954
2009-10-30  122.1827012  -2.3101001
2009-11-30  135.6316423   4.0573275
2009-12-31  148.3873256   7.8431412
2010-01-29  126.3824441  -0.2945944
2010-02-26  141.1833283   1.9042089
2010-03-31  176.9918170   4.0990218
2010-04-30  207.7437255   8.5331196
2010-05-28  202.7813580  -7.8800185
2010-06-30  196.4753306 -17.8418733
2010-07-30  203.2174761  -7.8443794
2010-08-31  186.5389988 -15.7764902
2010-09-30  234.4526175 -12.0974388
2010-10-29  254.7612494  -4.2906366
2010-11-30  266.7486320  -8.7806499
2010-12-31  280.1973043   0.7990238
2011-01-31  299.9519761   0.1309279
2011-02-28  316.3240102  -3.4346344
2011-03-31  310.7810723  -7.7579623
2011-04-29  312.6936690  -5.8325020
2011-05-31  309.9826843  -8.5456560
2011-06-30  295.6496897  -4.9255272
2011-07-29  360.2533492   0.1938528
2011-08-31  353.5939435  -2.1176194
2011-09-30  349.4567132  -8.4100861
2011-10-31  377.1086385  -2.0072184
2011-11-30  350.4940873  -5.1669956
2011-12-30  377.3683121  -3.7582513
2012-01-31  438.0470756   9.4768177
2012-02-29  539.3672585  18.4480050
2012-03-30  606.6824010  20.3699423
2012-04-30  588.3305657  19.4742696
2012-05-31  580.9640520   9.6530067
2012-06-29  588.3545548  14.9121842
2012-07-31  619.8963372  10.7049044
2012-08-31  687.4586085  16.5446988
2012-09-28  689.6658550  12.5363770
2012-10-31  604.6922813   7.9229482
2012-11-30  596.0978542   1.4981872
2012-12-31  532.9353271   1.8593178
2013-01-31  441.7331441   4.6825525
2013-02-28  427.9465773   6.8867483
2013-03-28  429.4538648   9.9818679
2013-04-30  429.5973468  27.2644186
2013-05-31  441.5079911  35.1055612
2013-06-28  377.4459713  33.7312674
2013-07-31  444.8732786  23.2595743
2013-08-30  490.5184028  30.2213983
2013-09-30  477.8334770  29.7535128
2013-10-31  533.5285346  38.0385341
2013-11-29  577.9173449  49.7958282
2013-12-31  583.9521651  46.9672336
2014-01-31  510.2925687  48.6564750
2014-02-28  545.3685238  51.6290098
2014-03-31  558.2455620  62.2363688
2014-04-30  623.6725379  59.9011385
2014-05-30  680.6400400  63.1608832
2014-06-30  702.2341048  66.1898018
2014-07-31  725.2830929  72.0084391
2014-08-29  789.2499602  82.1736219
2014-09-30  774.0677956  85.9028981
2014-10-31  836.9657142  88.2686984
2014-11-28  936.2512861  92.9366325
2014-12-31  861.7541448  87.4483570
2015-01-30  920.8289314  63.0335834
2015-02-27 1023.6733630  78.2147816
2015-03-31  988.4214822  65.2296962
2015-04-30  994.7190889  97.6822956
2015-05-29 1044.3230003  91.6889637

sample code(s)
# compare two data

zf2 <- cbind(gf2, hf2)

# col= 1 black, 2 read, 3 green
plot.zoo(zf2, plot.type="single",col=1:2,ylim=c(-50,1100),lwd=3, xlab = "Year", ylab="Cumulative Total Return")


grid()

legend("topleft",col=1:2,lty=1,legend=c("Apple","Microsoft"))


sample code(s)
# two data plus spread between the two

zf3 <- cbind(gf2, hf2, gf2 - hf2)

# col= 1 black, 2 read, 3 green
plot.zoo(zf3, plot.type="single",col=1:3,ylim=c(-50,1100),lwd=3, xlab = "Year", ylab="Cumulative Total Return")


grid()

legend("topleft",col=1:3,lty=1,legend=c("Apple","Microsoft", "(Apple - Microsoft)"))


No comments:

Post a Comment