Finding the correlation coefficient of two stocks

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Finding the correlation coefficient of two stocks

veepsirtt
This post has NOT been accepted by the mailing list yet.
Hi ,

library(tseries)
years <- 2
n <- 50
today <- Sys.Date()
   
s1 <- get.hist.quote(instrument = "^BSESN", start = format(today-365*years-2*n),quote = "Close")

s2 <- get.hist.quote(instrument = "BHEL.BO", start = format(today-365*years-2*n),quote = "Close")

corr <- cor(s1,s2)
print(corr)

While Running the above commands i got the following errors

corr <- cor(s1,s2)
Error in cor(s1, s2) : incompatible dimensions

so I am no table to find the correlation coefficient of the above stocks.
Kindly help me
thanks
veepsirtt
Reply | Threaded
Open this post in threaded view
|

Re: Finding the correlation coefficient of two stocks

Dieter Menne
veepsirtt wrote
corr <- cor(s1,s2)
Error in cor(s1, s2) : incompatible dimensions
Check lenghts of your series.


cor(c(1,2),c(1,2,3))

#Error in cor(c(1, 2), c(1, 2, 3)) : incompatible dimensions

Dieter

Reply | Threaded
Open this post in threaded view
|

Re: Finding the correlation coefficient of two stocks

veepsirtt
This post has NOT been accepted by the mailing list yet.
Hi Dieter Menne,

Yahoo historical stock price contains " Zeros and NA" for my symbols "^BSESN","BHEL.BO"  .
I want to remove  " Zeros and NA" so that the dimensions of both the series  is the same.
How to do it ?.
thanks
veepsirtt
Reply | Threaded
Open this post in threaded view
|

Re: Finding the correlation coefficient of two stocks

Pete Brecknock
In reply to this post by Dieter Menne
Dieter is correct, the lengths of the 2 series are different

Try ....

s = merge(s1,s2)

corr = cor(s[,"Close.s1"],s[,"Close.s2"],use="pairwise.complete.obs")

print(corr)

HTH

Pete
Reply | Threaded
Open this post in threaded view
|

Re: Finding the correlation coefficient of two stocks

Arun.stat
This post has NOT been accepted by the mailing list yet.
In reply to this post by Dieter Menne
Consult RMetrics group on what is the correct way to estimate correlation between two TS (most notably integrated time series). You might want to look into my latest post on this regards:

http://r.789695.n4.nabble.com/Finding-Correlation-on-Spreads-td3234774.html

Thanks,
Reply | Threaded
Open this post in threaded view
|

Re: Finding the correlation coefficient of two stocks

veepsirtt
This post has NOT been accepted by the mailing list yet.
In reply to this post by Pete Brecknock
 Hi Pete Brecknock,

Your code works fine for me .
thanks a lot
veepsirtt