Wednesday, June 17, 2015

[10] Quartile Excess Return

Quartile Factor Return

Run the following code.

sample code(s)
# TSV[,3] date (mm/dd/yyyy)
# In this case, 4/30/2015 in date TSV2 means monthly return of May (not April).
# The following code extracts monthly date of May 2015.
TSV2_2015_5 <- subset(TSV2[,],TSV2[,3]=="4/30/2015")

# Extract data within each Alpha quartile 1, 2, 3, and 4.
TSV2_2015_5_Alpha1 <- subset(TSV2_2015_5[,],TSV2_2015_5[,10]=="1")
TSV2_2015_5_Alpha2 <- subset(TSV2_2015_5[,],TSV2_2015_5[,10]=="2")
TSV2_2015_5_Alpha3 <- subset(TSV2_2015_5[,],TSV2_2015_5[,10]=="3")
TSV2_2015_5_Alpha4 <- subset(TSV2_2015_5[,],TSV2_2015_5[,10]=="4")

# average monthly total returns of each quartile
mean(TSV2_2015_5_Alpha1[,6])
mean(TSV2_2015_5_Alpha2[,6])
mean(TSV2_2015_5_Alpha3[,6])
mean(TSV2_2015_5_Alpha4[,6])

[1] 4.950676
[1] 5.175095
[1] 4.85193
[1] 4.811


Tuesday, June 16, 2015

[9] Plot: Time Series (Quartile Excess Return)

Create the following r code.


QTLR_F.r

# Portfolio Management with the R Programming Language
# http://pmwr.blogspot.com
#
QTLR_F <- function(
                df,   # data flame
                rtn,  # column: monthly total return
                tmh,  # column: mm/dd/yyyy (month end date)
                qtl,  # column: alpha score quartile (from 1:highest to 4:lowest)
                QQQ  # specify one quartile; 1, 2, 3, or 4
                )
{
  # aggregate (column_data_to_aggregate, column_data_aggregation_unit, FUN=mean:calc average, na.rm=TRUE:remove_NA)
  all.agg_mean <<- aggregate(df[,rtn],df[c(tmh)],FUN=mean,na.rm=TRUE) # average monthly total returns of the whole investment universe
  
  quartile.agg_mean.name <<- c("dmy")


  if(length(qtl) == 1){ # if qtl specifies only one column

    quartile <<- subset(df,df[,qtl] == QQQ)       # extract all the data with a specified quartile (1, 2, 3, or 4)
    quartile.agg_mean.name[1] <<- names(df)[qtl]  # alpha score column name like "ALPHA"
    
  }
  else {
  
    printf("Error: specify only one quartile.")
       
  }


  quartile.agg_mean <<- aggregate(quartile[,rtn],quartile[c(tmh)],FUN=mean,na.rm=TRUE)  # average monthly total returns for a specified quartile group


  tmp.all <-  xts(
                zoo(
                  subset(all.agg_mean[,2],all.agg_mean[,2]!= "DUMMY"),  # all.agg_mean[,2]: monthly total returns of the whole investment universe
                  as.Date(as.character(subset(all.agg_mean[,1],all.agg_mean[,2]!="DUMMY")), # all.agg_mean[,1]: mm/dd/yyyy
                  format='%m/%d/%Y')
                )
              )


  tmp.quartile <- xts(
                zoo(
                  subset(quartile.agg_mean[,2],quartile.agg_mean[,2]!="DUMMY"), # quartile.agg_mean[,2]: monthly total returns of the specified quartile
                  as.Date(as.character(subset(quartile.agg_mean[,1],quartile.agg_mean[,2]!="DUMMY")), # quartile.agg_mean[,1]: mm/dd/yyyy
                  format='%m/%d/%Y')
                )
              )


  tmp.comb <<- merge(tmp.all,tmp.quartile)
  tmp.comb.a <- tmp.comb[,2] - tmp.comb[,1] # TR_MEAN(specified quartile) - TR_MEAN(whole universe)
  tmp.comb.b <- tmp.comb.a


  for(i1 in 1:nrow(tmp.comb.b)){
    if(is.na(tmp.comb.b[i1,])==TRUE) tmp.comb.b[i1,]=0  # If a monthly total return for a specified quartile is not available, then it is regarded as zero.
  }
  
  quartile.agg_mean.dif <<-  tmp.comb.b
  quartile.agg_mean.dif.cum <<- cumsum(quartile.agg_mean.dif)


}              




Plot: Cumulative Quartile Excess Return (x axis: yyyy format)

Create the following monthly tab-separated file for the investment universe (TOPIX, Japan Equity index in this case).

sample code(s)
source ("setup.r")
source ("QTLR_F.r")

# JP_EQ_ALPHA_SCORE.txt is a tab-separated text file which contains (1) monthly total returns of each stock and (2) times series monthly data from 1/29/1999 to 5/29/2015. Details to follow.
TSV <- read.delim("JP_EQ_ALPHA_SCORE.txt")

# Only stocks with the market cap ranking <= 800 (1 is the largest.)
TSV2 <- subset(TSV,TSV[,22]<=800)

names(TSV)

 [1] "Identifier"                        
 [2] "Name"                              
 [3] "Period..Formatted."                
...
 [6] "Universe.Returns"                
...            
[10] "ALPHA"                            
...                  
[22] "MKT.CAP.RANK"                      
...


sample code(s)
# 1:5 show first to fifth row.
#
# c() 1 SEDOL code
# c() 2 Name
# c() 6 monthly total absolute return (%)
# c() 3 mm/dd/yyyy
# c() 10 quartile of Alpha score (1 the highest/most attractive to 4 the lowest/least attractive)
# c() 22 market cap ranking (1 is the largest)

TSV[1:5,c(1,2,6,3,10,22)]

> TSV[1:5,c(1,2,6,3,10,22)]
  Identifier                                     Name Universe.Returns Period..Formatted. ALPHA MKT.CAP.RANK
1   CASH_JPY                             Japanese Yen         0.000000          1/29/1999    NA           NA
2     686803                           SUZUTAN CO LTD        -2.013421          1/29/1999     1         1239
3     687012                      Mitsui Sumitomo NPV        -6.500000          1/29/1999     3          124
4     648390  Kanto Natural Gas Development Co., Ltd.        -6.185567          1/29/1999     3          713
5     689476 Tokio Marine and Fire Insurance Co. Ltd.        -3.787881          1/29/1999     4           22



sample code(s)
TSV2[1:5,c(1,2,6,3,10,22)]

ntifier                                     Name Universe.Returns Period..Formatted. ALPHA MKT.CAP.RANK
3     687012                      Mitsui Sumitomo NPV        -6.500000          1/29/1999     3          124
4     648390  Kanto Natural Gas Development Co., Ltd.        -6.185567          1/29/1999     3          713
5     689476 Tokio Marine and Fire Insurance Co. Ltd.        -3.787881          1/29/1999     4           22
6     600739                        Aderans Co., Ltd.         6.727278          1/29/1999     4          329
7     601004              Aica Kogyo Company, Limited         0.815213          1/29/1999     1          795




Run the following r code.

sample code(s)

QTLR_F(TSV2,6,3,10,1)
gf <- quartile.agg_mean.dif.cum

QTLR_F(TSV2,6,3,10,2)
gf <- cbind(gf,quartile.agg_mean.dif.cum)

QTLR_F(TSV2,6,3,10,3)
gf <- cbind(gf,quartile.agg_mean.dif.cum)

QTLR_F(TSV2,6,3,10,4)
gf <- cbind(gf,quartile.agg_mean.dif.cum)

plot.zoo(gf,plot.type="single",col=1:10,ylim=c(-150,150),lwd=3)
grid()

legend("topleft",col=1:10,lty=1,legend=c("Q1","Q2","Q3","Q4"))




Plot: Cumulative Quartile Excess Return Spread (x axis: yyyy format)

Run the following r code.

sample code(s)


QTLR_F(TSV2,6,3,10,1)
gf1 <- quartile.agg_mean.dif.cum

QTLR_F(TSV2,6,3,10,4)
gf4 <- quartile.agg_mean.dif.cum

gf14sprd <- cbind(gf1, gf4, gf1 - gf4)

plot.zoo(gf14sprd,plot.type="single",col=1:10,ylim=c(-200,200),lwd=3)

legend("topleft",col=1:10,lty=1,legend=c("Q1","Q2","Q1-Q4")




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)"))


[7] User-Defined Functions in Portfolio Management

Before you read this section, please (1) create a csv (AAPL.csv) or tab-separated file (AAPL.txt) for monthly total returns of Apple and then (2) load the file into "aapl" by using read.table(), as you can see in the previous section.

Total Return

Create the following r code.

TR_F.r
# TR_F(FILE, column_date, column_total_return, separator)
#
# arguments:
#   FILE  tab-separated or csv iput data file
#   dt    data column number for yyyymmdd
#   tr    monthly total return number (%) (e.g., 1.23 in input data file or output means 1.23%)
#   sp    separator
#           ""    tab-separated
#           "csv" csv (comma separated value)


TR_F <- function(FILE,dt,tr,sp){

  i <- 1
  ctr <- 0
  OUT <- NULL


  if(sp=="csv"){
    IN = read.table(FILE, header=T, sep=",")
  } else {
    IN = read.table(FILE, header=T, sep="\t")
  }

  
  while(i<=nrow(IN)){
    ctr <- (1 + ctr) * (1 + (IN[i,tr]/100)) - 1
    i <- i+1
  }

  OUT <- ctr*100
  return(OUT)
  
}


sample code(s)
# Total return is 1,044.323%.

source("TR_F.r") TR_F("AAPL.txt",1,3,"")

[1] 1044.323


sample code(s)
TR_F("AAPL.csv",1,3,"csv")

[1] 1044.323


Monthly and Cumulative Total Return

Create the following r code.

MCTR_F.r
# MCTR_F(FILE, column_date, column_total_return, separator)
#
# arguments:
#   FILE  tab-separated or csv iput data file
#   dt    data column number for yyyymmdd
#   tr    monthly total return number (%) (e.g., 1.23 in input data file or output means 1.23%)
#   sp    separator
#           ""    tab-separated
#           "csv" csv (comma separated value)


MCTR_F <- function(FILE,dt,tr,sp){

  i <- 1
  ctr <- 0
  df <- NULL


  if(sp=="csv"){
    IN = read.table(FILE, header=T, sep=",")
  } else {
    IN = read.table(FILE, header=T, sep="\t")
  }

  
  while(i<=nrow(IN)){
    ctr <- (1 + ctr) * (1 + (IN[i,tr]/100)) - 1
    
    # cumulative monthly returns
    df <- rbind(df, data.frame(IN[i,dt],IN[i,tr],ctr*100))
   
    i <- i+1
    
  }

  return(df)
  
}




sample code(s)
source("MCTR_F.r")

MCTR_F("AAPL.txt",1,3,"")

     IN.i..dt. IN.i..tr.    ctr...100
1   2007/01/31    1.0490    1.0490000
2   2007/02/28   -1.3064   -0.2711041
3   2007/03/30    9.8097    9.5120014
4   2007/04/30    7.4158   17.6331924
5   2007/05/31   21.4339   42.8465732
6   2007/06/29    0.7005   43.8472134
7   2007/07/31    7.9646   55.3040686
8   2007/08/31    5.1002   63.2248867
9   2007/09/28   10.8247   80.8934910
10  2007/10/31   23.7701  123.8920547
11  2007/11/30   -4.0695  114.7807675
12  2007/12/31    8.7038  133.4748560
13  2008/01/31  -31.6640   59.5473776
14  2008/02/29   -7.6389   47.3597130
15  2008/03/31   14.7816   69.1418363
16  2008/04/30   21.2195  105.0328882
17  2008/05/30    8.5082  122.4774964
18  2008/06/30  -11.2901   97.3595646
19  2008/07/31   -5.0705   87.3524479
20  2008/08/29    6.6562   99.8230015
21  2008/09/30  -32.9558   33.9697328
22  2008/10/31   -5.3405   26.8150792
23  2008/11/28  -13.8675    9.2289981
24  2008/12/31   -7.8990    0.6009995
25  2009/01/30    5.6005    6.2351585
26  2009/02/27   -0.9098    5.2686310
27  2009/03/31   17.7024   23.9037052
28  2009/04/30   19.7013   48.3143459
29  2009/05/29    7.9313   60.0776016
30  2009/06/30    4.8745   67.8805843
31  2009/07/31   14.7160   92.5858910
32  2009/08/31    2.9500   98.2671748
33  2009/09/30   10.1896  118.4698069
34  2009/10/30    1.6995  122.1827012
35  2009/11/30    6.0531  135.6316423
36  2009/12/31    5.4134  148.3873256
37  2010/01/29   -8.8591  126.3824441
38  2010/02/26    6.5380  141.1833283
39  2010/03/31   14.8470  176.9918170
40  2010/04/30   11.1021  207.7437255
41  2010/05/28   -1.6125  202.7813580
42  2010/06/30   -2.0827  196.4753306
43  2010/07/30    2.2741  203.2174761
44  2010/08/31   -5.5005  186.5389988
45  2010/09/30   16.7215  234.4526175
46  2010/10/29    6.0722  254.7612494
47  2010/11/30    3.3790  266.7486320
48  2010/12/31    3.6670  280.1973043
49  2011/01/31    5.1959  299.9519761
50  2011/02/28    4.0935  316.3240102
51  2011/03/31   -1.3314  310.7810723
52  2011/04/29    0.4656  312.6936690
53  2011/05/31   -0.6569  309.9826843
54  2011/06/30   -3.4960  295.6496897
55  2011/07/29   16.3285  360.2533492
56  2011/08/31   -1.4469  353.5939435
57  2011/09/30   -0.9121  349.4567132
58  2011/10/31    6.1523  377.1086385
59  2011/11/30   -5.5783  350.4940873
60  2011/12/30    5.9655  377.3683121
61  2012/01/31   12.7111  438.0470756
62  2012/02/29   18.8311  539.3672585
63  2012/03/30   10.5284  606.6824010
64  2012/04/30   -2.5969  588.3305657
65  2012/05/31   -1.0702  580.9640520
66  2012/06/29    1.0853  588.3545548
67  2012/07/31    4.5822  619.8963372
68  2012/08/31    9.3850  687.4586085
69  2012/09/28    0.2803  689.6658550
70  2012/10/31  -10.7607  604.6922813
71  2012/11/30   -1.2196  596.0978542
72  2012/12/31   -9.0738  532.9353271
73  2013/01/31  -14.4094  441.7331441
74  2013/02/28   -2.5449  427.9465773
75  2013/03/28    0.2855  429.4538648
76  2013/04/30    0.0271  429.5973468
77  2013/05/31    2.2490  441.5079911
78  2013/06/28  -11.8303  377.4459713
79  2013/07/31   14.1225  444.8732786
80  2013/08/30    8.3772  490.5184028
81  2013/09/30   -2.1481  477.8334770
82  2013/10/31    9.6386  533.5285346
83  2013/11/29    7.0066  577.9173449
84  2013/12/31    0.8902  583.9521651
85  2014/01/31  -10.7697  510.2925687
86  2014/02/28    5.7474  545.3685238
87  2014/03/31    1.9953  558.2455620
88  2014/04/30    9.9396  623.6725379
89  2014/05/30    7.8720  680.6400400
90  2014/06/30    2.7662  702.2341048
91  2014/07/31    2.8731  725.2830929
92  2014/08/29    7.7509  789.2499602
93  2014/09/30   -1.7073  774.0677956
94  2014/10/31    7.1960  836.9657142
95  2014/11/28   10.5965  936.2512861
96  2014/12/31   -7.1891  861.7541448
97  2015/01/30    6.1424  920.8289314
98  2015/02/27   10.0746 1023.6733630
99  2015/03/31   -3.1372  988.4214822
100 2015/04/30    0.5786  994.7190889
101 2015/05/29    4.5312 1044.3230003

[6] Sourcing Files

read.table()
for csv files

Create the following monthly price and total return csv file for Apple.

AAPL.csv
Date,Price Return,Total Return,
2007/01/31,1.0490 ,1.0490 ,
2007/02/28,-1.3064 ,-1.3064 ,
2007/03/30,9.8097 ,9.8097 ,
2007/04/30,7.4158 ,7.4158 ,
2007/05/31,21.4339 ,21.4339 ,
2007/06/29,0.7005 ,0.7005 ,
2007/07/31,7.9646 ,7.9646 ,
2007/08/31,5.1002 ,5.1002 ,
2007/09/28,10.8247 ,10.8247 ,
2007/10/31,23.7701 ,23.7701 ,
2007/11/30,-4.0695 ,-4.0695 ,
2007/12/31,8.7038 ,8.7038 ,
2008/01/31,-31.6640 ,-31.6640 ,
2008/02/29,-7.6389 ,-7.6389 ,
2008/03/31,14.7816 ,14.7816 ,
2008/04/30,21.2195 ,21.2195 ,
2008/05/30,8.5082 ,8.5082 ,
2008/06/30,-11.2901 ,-11.2901 ,
2008/07/31,-5.0705 ,-5.0705 ,
2008/08/29,6.6562 ,6.6562 ,
2008/09/30,-32.9558 ,-32.9558 ,
2008/10/31,-5.3405 ,-5.3405 ,
2008/11/28,-13.8675 ,-13.8675 ,
2008/12/31,-7.8990 ,-7.8990 ,
2009/01/30,5.6005 ,5.6005 ,
2009/02/27,-0.9098 ,-0.9098 ,
2009/03/31,17.7024 ,17.7024 ,
2009/04/30,19.7013 ,19.7013 ,
2009/05/29,7.9313 ,7.9313 ,
2009/06/30,4.8745 ,4.8745 ,
2009/07/31,14.7160 ,14.7160 ,
2009/08/31,2.9500 ,2.9500 ,
2009/09/30,10.1896 ,10.1896 ,
2009/10/30,1.6995 ,1.6995 ,
2009/11/30,6.0531 ,6.0531 ,
2009/12/31,5.4134 ,5.4134 ,
2010/01/29,-8.8591 ,-8.8591 ,
2010/02/26,6.5380 ,6.5380 ,
2010/03/31,14.8470 ,14.8470 ,
2010/04/30,11.1021 ,11.1021 ,
2010/05/28,-1.6125 ,-1.6125 ,
2010/06/30,-2.0827 ,-2.0827 ,
2010/07/30,2.2741 ,2.2741 ,
2010/08/31,-5.5005 ,-5.5005 ,
2010/09/30,16.7215 ,16.7215 ,
2010/10/29,6.0722 ,6.0722 ,
2010/11/30,3.3790 ,3.3790 ,
2010/12/31,3.6670 ,3.6670 ,
2011/01/31,5.1959 ,5.1959 ,
2011/02/28,4.0935 ,4.0935 ,
2011/03/31,-1.3314 ,-1.3314 ,
2011/04/29,0.4656 ,0.4656 ,
2011/05/31,-0.6569 ,-0.6569 ,
2011/06/30,-3.4960 ,-3.4960 ,
2011/07/29,16.3285 ,16.3285 ,
2011/08/31,-1.4469 ,-1.4469 ,
2011/09/30,-0.9121 ,-0.9121 ,
2011/10/31,6.1523 ,6.1523 ,
2011/11/30,-5.5783 ,-5.5783 ,
2011/12/30,5.9655 ,5.9655 ,
2012/01/31,12.7111 ,12.7111 ,
2012/02/29,18.8311 ,18.8311 ,
2012/03/30,10.5284 ,10.5284 ,
2012/04/30,-2.5969 ,-2.5969 ,
2012/05/31,-1.0702 ,-1.0702 ,
2012/06/29,1.0853 ,1.0853 ,
2012/07/31,4.5822 ,4.5822 ,
2012/08/31,8.9200 ,9.3850 ,
2012/09/28,0.2803 ,0.2803 ,
2012/10/31,-10.7607 ,-10.7607 ,
2012/11/30,-1.6865 ,-1.2196 ,
2012/12/31,-9.0738 ,-9.0738 ,
2013/01/31,-14.4094 ,-14.4094 ,
2013/02/28,-3.0934 ,-2.5449 ,
2013/03/28,0.2855 ,0.2855 ,
2013/04/30,0.0271 ,0.0271 ,
2013/05/31,1.5708 ,2.2490 ,
2013/06/28,-11.8303 ,-11.8303 ,
2013/07/31,14.1225 ,14.1225 ,
2013/08/30,7.6649 ,8.3772 ,
2013/09/30,-2.1481 ,-2.1481 ,
2013/10/31,9.6386 ,9.6386 ,
2013/11/29,6.3838 ,7.0066 ,
2013/12/31,0.8902 ,0.8902 ,
2014/01/31,-10.7697 ,-10.7697 ,
2014/02/28,5.1219 ,5.7474 ,
2014/03/31,1.9953 ,1.9953 ,
2014/04/30,9.9396 ,9.9396 ,
2014/05/30,7.2718 ,7.8720 ,
2014/06/30,2.7662 ,2.7662 ,
2014/07/31,2.8731 ,2.8731 ,
2014/08/29,7.2176 ,7.7509 ,
2014/09/30,-1.7073 ,-1.7073 ,
2014/10/31,7.1960 ,7.1960 ,
2014/11/28,10.1204 ,10.5965 ,
2014/12/31,-7.1891 ,-7.1891 ,
2015/01/30,6.1424 ,6.1424 ,
2015/02/27,9.6449 ,10.0746 ,
2015/03/31,-3.1372 ,-3.1372 ,
2015/04/30,0.5786 ,0.5786 ,
2015/05/29,4.0991 ,4.5312 ,


sample code(s)
# load the csv file with the header in the first row

aapl <- read.table("AAPL.csv", header=T, sep=",")
aapl
        Date Price.Return Total.Return
1   20061229      -7.4405      -7.4405
2   20070131       1.0490       1.0490
3   20070228      -1.3064      -1.3064
4   20070330       9.8097       9.8097
5   20070430       7.4158       7.4158
6   20070531      21.4339      21.4339
7   20070629       0.7005       0.7005
8   20070731       7.9646       7.9646
9   20070831       5.1002       5.1002
10  20070928      10.8247      10.8247
11  20071031      23.7701      23.7701
12  20071130      -4.0695      -4.0695
13  20071231       8.7038       8.7038
14  20080131     -31.6640     -31.6640
15  20080229      -7.6389      -7.6389
16  20080331      14.7816      14.7816
17  20080430      21.2195      21.2195
18  20080530       8.5082       8.5082
19  20080630     -11.2901     -11.2901
20  20080731      -5.0705      -5.0705
21  20080829       6.6562       6.6562
22  20080930     -32.9558     -32.9558
23  20081031      -5.3405      -5.3405
24  20081128     -13.8675     -13.8675
25  20081231      -7.8990      -7.8990
26  20090130       5.6005       5.6005
27  20090227      -0.9098      -0.9098
28  20090331      17.7024      17.7024
29  20090430      19.7013      19.7013
30  20090529       7.9313       7.9313
31  20090630       4.8745       4.8745
32  20090731      14.7160      14.7160
33  20090831       2.9500       2.9500
34  20090930      10.1896      10.1896
35  20091030       1.6995       1.6995
36  20091130       6.0531       6.0531
37  20091231       5.4134       5.4134
38  20100129      -8.8591      -8.8591
39  20100226       6.5380       6.5380
40  20100331      14.8470      14.8470
41  20100430      11.1021      11.1021
42  20100528      -1.6125      -1.6125
43  20100630      -2.0827      -2.0827
44  20100730       2.2741       2.2741
45  20100831      -5.5005      -5.5005
46  20100930      16.7215      16.7215
47  20101029       6.0722       6.0722
48  20101130       3.3790       3.3790
49  20101231       3.6670       3.6670
50  20110131       5.1959       5.1959
51  20110228       4.0935       4.0935
52  20110331      -1.3314      -1.3314
53  20110429       0.4656       0.4656
54  20110531      -0.6569      -0.6569
55  20110630      -3.4960      -3.4960
56  20110729      16.3285      16.3285
57  20110831      -1.4469      -1.4469
58  20110930      -0.9121      -0.9121
59  20111031       6.1523       6.1523
60  20111130      -5.5783      -5.5783
61  20111230       5.9655       5.9655
62  20120131      12.7111      12.7111
63  20120229      18.8311      18.8311
64  20120330      10.5284      10.5284
65  20120430      -2.5969      -2.5969
66  20120531      -1.0702      -1.0702
67  20120629       1.0853       1.0853
68  20120731       4.5822       4.5822
69  20120831       8.9200       9.3850
70  20120928       0.2803       0.2803
71  20121031     -10.7607     -10.7607
72  20121130      -1.6865      -1.2196
73  20121231      -9.0738      -9.0738
74  20130131     -14.4094     -14.4094
75  20130228      -3.0934      -2.5449
76  20130328       0.2855       0.2855
77  20130430       0.0271       0.0271
78  20130531       1.5708       2.2490
79  20130628     -11.8303     -11.8303
80  20130731      14.1225      14.1225
81  20130830       7.6649       8.3772
82  20130930      -2.1481      -2.1481
83  20131031       9.6386       9.6386
84  20131129       6.3838       7.0066
85  20131231       0.8902       0.8902
86  20140131     -10.7697     -10.7697
87  20140228       5.1219       5.7474
88  20140331       1.9953       1.9953
89  20140430       9.9396       9.9396
90  20140530       7.2718       7.8720
91  20140630       2.7662       2.7662
92  20140731       2.8731       2.8731
93  20140829       7.2176       7.7509
94  20140930      -1.7073      -1.7073
95  20141031       7.1960       7.1960
96  20141128      10.1204      10.5965
97  20141231      -7.1891      -7.1891
98  20150130       6.1424       6.1424
99  20150227       9.6449      10.0746
100 20150331      -3.1372      -3.1372
101 20150430       0.5786       0.5786
102 20150529       4.0991       4.5312


read.table()
for tab-separated files

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

AAPL.txt
Date Price Return Total Return
2007/01/31 1.0490 1.0490 
2007/02/28 -1.3064 -1.3064 
2007/03/30 9.8097 9.8097 
2007/04/30 7.4158 7.4158 
2007/05/31 21.4339 21.4339 
2007/06/29 0.7005 0.7005 
2007/07/31 7.9646 7.9646 
2007/08/31 5.1002 5.1002 
2007/09/28 10.8247 10.8247 
2007/10/31 23.7701 23.7701 
2007/11/30 -4.0695 -4.0695 
2007/12/31 8.7038 8.7038 
2008/01/31 -31.6640 -31.6640 
2008/02/29 -7.6389 -7.6389 
2008/03/31 14.7816 14.7816 
2008/04/30 21.2195 21.2195 
2008/05/30 8.5082 8.5082 
2008/06/30 -11.2901 -11.2901 
2008/07/31 -5.0705 -5.0705 
2008/08/29 6.6562 6.6562 
2008/09/30 -32.9558 -32.9558 
2008/10/31 -5.3405 -5.3405 
2008/11/28 -13.8675 -13.8675 
2008/12/31 -7.8990 -7.8990 
2009/01/30 5.6005 5.6005 
2009/02/27 -0.9098 -0.9098 
2009/03/31 17.7024 17.7024 
2009/04/30 19.7013 19.7013 
2009/05/29 7.9313 7.9313 
2009/06/30 4.8745 4.8745 
2009/07/31 14.7160 14.7160 
2009/08/31 2.9500 2.9500 
2009/09/30 10.1896 10.1896 
2009/10/30 1.6995 1.6995 
2009/11/30 6.0531 6.0531 
2009/12/31 5.4134 5.4134 
2010/01/29 -8.8591 -8.8591 
2010/02/26 6.5380 6.5380 
2010/03/31 14.8470 14.8470 
2010/04/30 11.1021 11.1021 
2010/05/28 -1.6125 -1.6125 
2010/06/30 -2.0827 -2.0827 
2010/07/30 2.2741 2.2741 
2010/08/31 -5.5005 -5.5005 
2010/09/30 16.7215 16.7215 
2010/10/29 6.0722 6.0722 
2010/11/30 3.3790 3.3790 
2010/12/31 3.6670 3.6670 
2011/01/31 5.1959 5.1959 
2011/02/28 4.0935 4.0935 
2011/03/31 -1.3314 -1.3314 
2011/04/29 0.4656 0.4656 
2011/05/31 -0.6569 -0.6569 
2011/06/30 -3.4960 -3.4960 
2011/07/29 16.3285 16.3285 
2011/08/31 -1.4469 -1.4469 
2011/09/30 -0.9121 -0.9121 
2011/10/31 6.1523 6.1523 
2011/11/30 -5.5783 -5.5783 
2011/12/30 5.9655 5.9655 
2012/01/31 12.7111 12.7111 
2012/02/29 18.8311 18.8311 
2012/03/30 10.5284 10.5284 
2012/04/30 -2.5969 -2.5969 
2012/05/31 -1.0702 -1.0702 
2012/06/29 1.0853 1.0853 
2012/07/31 4.5822 4.5822 
2012/08/31 8.9200 9.3850 
2012/09/28 0.2803 0.2803 
2012/10/31 -10.7607 -10.7607 
2012/11/30 -1.6865 -1.2196 
2012/12/31 -9.0738 -9.0738 
2013/01/31 -14.4094 -14.4094 
2013/02/28 -3.0934 -2.5449 
2013/03/28 0.2855 0.2855 
2013/04/30 0.0271 0.0271 
2013/05/31 1.5708 2.2490 
2013/06/28 -11.8303 -11.8303 
2013/07/31 14.1225 14.1225 
2013/08/30 7.6649 8.3772 
2013/09/30 -2.1481 -2.1481 
2013/10/31 9.6386 9.6386 
2013/11/29 6.3838 7.0066 
2013/12/31 0.8902 0.8902 
2014/01/31 -10.7697 -10.7697 
2014/02/28 5.1219 5.7474 
2014/03/31 1.9953 1.9953 
2014/04/30 9.9396 9.9396 
2014/05/30 7.2718 7.8720 
2014/06/30 2.7662 2.7662 
2014/07/31 2.8731 2.8731 
2014/08/29 7.2176 7.7509 
2014/09/30 -1.7073 -1.7073 
2014/10/31 7.1960 7.1960 
2014/11/28 10.1204 10.5965 
2014/12/31 -7.1891 -7.1891 
2015/01/30 6.1424 6.1424 
2015/02/27 9.6449 10.0746 
2015/03/31 -3.1372 -3.1372 
2015/04/30 0.5786 0.5786 
2015/05/29 4.0991 4.5312 


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

aapl <- read.table("AAPL.txt", header=T, sep="\t")
aapl
          Date Price.Return Total.Return
1   2007/01/31       1.0490       1.0490
2   2007/02/28      -1.3064      -1.3064
3   2007/03/30       9.8097       9.8097
4   2007/04/30       7.4158       7.4158
5   2007/05/31      21.4339      21.4339
6   2007/06/29       0.7005       0.7005
7   2007/07/31       7.9646       7.9646
8   2007/08/31       5.1002       5.1002
9   2007/09/28      10.8247      10.8247
10  2007/10/31      23.7701      23.7701
11  2007/11/30      -4.0695      -4.0695
12  2007/12/31       8.7038       8.7038
13  2008/01/31     -31.6640     -31.6640
14  2008/02/29      -7.6389      -7.6389
15  2008/03/31      14.7816      14.7816
16  2008/04/30      21.2195      21.2195
17  2008/05/30       8.5082       8.5082
18  2008/06/30     -11.2901     -11.2901
19  2008/07/31      -5.0705      -5.0705
20  2008/08/29       6.6562       6.6562
21  2008/09/30     -32.9558     -32.9558
22  2008/10/31      -5.3405      -5.3405
23  2008/11/28     -13.8675     -13.8675
24  2008/12/31      -7.8990      -7.8990
25  2009/01/30       5.6005       5.6005
26  2009/02/27      -0.9098      -0.9098
27  2009/03/31      17.7024      17.7024
28  2009/04/30      19.7013      19.7013
29  2009/05/29       7.9313       7.9313
30  2009/06/30       4.8745       4.8745
31  2009/07/31      14.7160      14.7160
32  2009/08/31       2.9500       2.9500
33  2009/09/30      10.1896      10.1896
34  2009/10/30       1.6995       1.6995
35  2009/11/30       6.0531       6.0531
36  2009/12/31       5.4134       5.4134
37  2010/01/29      -8.8591      -8.8591
38  2010/02/26       6.5380       6.5380
39  2010/03/31      14.8470      14.8470
40  2010/04/30      11.1021      11.1021
41  2010/05/28      -1.6125      -1.6125
42  2010/06/30      -2.0827      -2.0827
43  2010/07/30       2.2741       2.2741
44  2010/08/31      -5.5005      -5.5005
45  2010/09/30      16.7215      16.7215
46  2010/10/29       6.0722       6.0722
47  2010/11/30       3.3790       3.3790
48  2010/12/31       3.6670       3.6670
49  2011/01/31       5.1959       5.1959
50  2011/02/28       4.0935       4.0935
51  2011/03/31      -1.3314      -1.3314
52  2011/04/29       0.4656       0.4656
53  2011/05/31      -0.6569      -0.6569
54  2011/06/30      -3.4960      -3.4960
55  2011/07/29      16.3285      16.3285
56  2011/08/31      -1.4469      -1.4469
57  2011/09/30      -0.9121      -0.9121
58  2011/10/31       6.1523       6.1523
59  2011/11/30      -5.5783      -5.5783
60  2011/12/30       5.9655       5.9655
61  2012/01/31      12.7111      12.7111
62  2012/02/29      18.8311      18.8311
63  2012/03/30      10.5284      10.5284
64  2012/04/30      -2.5969      -2.5969
65  2012/05/31      -1.0702      -1.0702
66  2012/06/29       1.0853       1.0853
67  2012/07/31       4.5822       4.5822
68  2012/08/31       8.9200       9.3850
69  2012/09/28       0.2803       0.2803
70  2012/10/31     -10.7607     -10.7607
71  2012/11/30      -1.6865      -1.2196
72  2012/12/31      -9.0738      -9.0738
73  2013/01/31     -14.4094     -14.4094
74  2013/02/28      -3.0934      -2.5449
75  2013/03/28       0.2855       0.2855
76  2013/04/30       0.0271       0.0271
77  2013/05/31       1.5708       2.2490
78  2013/06/28     -11.8303     -11.8303
79  2013/07/31      14.1225      14.1225
80  2013/08/30       7.6649       8.3772
81  2013/09/30      -2.1481      -2.1481
82  2013/10/31       9.6386       9.6386
83  2013/11/29       6.3838       7.0066
84  2013/12/31       0.8902       0.8902
85  2014/01/31     -10.7697     -10.7697
86  2014/02/28       5.1219       5.7474
87  2014/03/31       1.9953       1.9953
88  2014/04/30       9.9396       9.9396
89  2014/05/30       7.2718       7.8720
90  2014/06/30       2.7662       2.7662
91  2014/07/31       2.8731       2.8731
92  2014/08/29       7.2176       7.7509
93  2014/09/30      -1.7073      -1.7073
94  2014/10/31       7.1960       7.1960
95  2014/11/28      10.1204      10.5965
96  2014/12/31      -7.1891      -7.1891
97  2015/01/30       6.1424       6.1424
98  2015/02/27       9.6449      10.0746
99  2015/03/31      -3.1372      -3.1372
100 2015/04/30       0.5786       0.5786
101 2015/05/29       4.0991       4.5312



names()

Show the column names.

sample code(s)
names(aapl)
[1] "Date"         "Price.Return" "Total.Return"

subset()

Extract data which meet specified criteria.


sample code(s)
# Extract months with (monthly total return) <= -10
aapl_d <- subset(aapl,aapl[,3]<=-10)
aapl_d
         Date Price.Return Total.Return
13 2008/01/31     -31.6640     -31.6640
18 2008/06/30     -11.2901     -11.2901
21 2008/09/30     -32.9558     -32.9558
23 2008/11/28     -13.8675     -13.8675
70 2012/10/31     -10.7607     -10.7607
73 2013/01/31     -14.4094     -14.4094
78 2013/06/28     -11.8303     -11.8303
85 2014/01/31     -10.7697     -10.7697

sample code(s)
# Extract months with (monthly total return) >= +10
aapl_u <- subset(aapl,aapl[,3]>=+10)
aapl_u 
         Date Price.Return Total.Return
5  2007/05/31      21.4339      21.4339
9  2007/09/28      10.8247      10.8247
10 2007/10/31      23.7701      23.7701
15 2008/03/31      14.7816      14.7816
16 2008/04/30      21.2195      21.2195
27 2009/03/31      17.7024      17.7024
28 2009/04/30      19.7013      19.7013
31 2009/07/31      14.7160      14.7160
33 2009/09/30      10.1896      10.1896
39 2010/03/31      14.8470      14.8470
40 2010/04/30      11.1021      11.1021
45 2010/09/30      16.7215      16.7215
55 2011/07/29      16.3285      16.3285
61 2012/01/31      12.7111      12.7111
62 2012/02/29      18.8311      18.8311
63 2012/03/30      10.5284      10.5284
79 2013/07/31      14.1225      14.1225
95 2014/11/28      10.1204      10.5965
98 2015/02/27       9.6449      10.0746