R version 2.9.2 (2009-08-24)
Copyright (C) 2009 The R Foundation for Statistical Computing
ISBN 3-900051-07-0

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

>
>
> # I merged the common dates with this python script
> # http://www-stat.stanford.edu/~jtaylo/courses/stats202/R/chap2_data/merge.py
>
> merged = read.table("http://www-stat.stanford.edu/~jtaylo/courses/stats202/data/merged_indices.csv", sep=',', header=T)
>
> # this will be a scatter plot because I didn't specify type='l'
>
> plot(merged$dow, merged$sp500)
>
>

>


>
> # I merged the common dates with this python script
> # http://www-stat.stanford.edu/~jtaylo/courses/stats202/R/chap2_data/merge.py
> # and added the Yahoo stock price
>
> merged = read.table("http://www-stat.stanford.edu/~jtaylo/courses/stats202/data/merged_indices.csv", sep=',', header=T)
>
> # this will be a scatter plot because I didn't specify type='l'
>
> plot(merged$dow, merged$sp500, pch=23, col='orange')
>
>

>


> # often the log-returns are used rather than the raw score
> # note: there are 850 rows in the merged data
>
> dow_r = log(merged$dow[2:850] / merged$dow[1:849])
> sp_r = log(merged$sp500[2:850] / merged$sp500[1:849])
>
> plot(dow_r, sp_r, pch=23, col='orange')
>

>


>
> # What if we want to see how the DJI influences Yahoo?
>
> yahoo_r = log(merged$yahoo[2:850] / merged$yahoo[1:849])
> plot(dow_r, yahoo_r, pch=23, col='blue')
>
>

>


>
> # Yahoo had a really great day while the rest of the market
> # was sort of flat -- what day was it?
>
> print(merged$date[which.max(yahoo_r)+1])
[1] 2008-02-01
850 Levels: 2006-01-03 2006-01-04 2006-01-05 2006-01-06 ... 2009-05-19
>
> # Take a look: http://www.microsoft.com/presspass/press/2008/feb08/02-01corpnewspr.mspx
>
>

>
> proc.time()
user system elapsed
0.652 0.028 0.775
R script