2銘柄の価格

株価の取得。

> YJ4528<-getSymbols("YJ4528", src="yahooj", auto.assign=FALSE, from='2013-01-01', to='2014-01-01')
> head(YJ4528)
           YJ4528.Open YJ4528.High YJ4528.Low YJ4528.Close YJ4528.Volume YJ4528.Adjusted
2013-01-04        4475        4500       4445         4500         27900            4500
2013-01-07        4515        4545       4500         4500          6300            4500
2013-01-08        4525        4560       4520         4550          5300            4550
2013-01-09        4530        4580       4505         4570         11500            4570
2013-01-10        4500        4590       4500         4575         10300            4575
2013-01-11        4605        4675       4605         4675          4700            4675
> YJ7261<-getSymbols("YJ7261", src="yahooj", auto.assign=FALSE, from='2013-01-01', to='2014-01-01')

> head(YJ7261)
           YJ7261.Open YJ7261.High YJ7261.Low YJ7261.Close YJ7261.Volume YJ7261.Adjusted
2013-01-04         184         186        181          185      86280000             925
2013-01-07         190         190        179          180      84120000             900
2013-01-08         176         177        168          171     122600000             855
2013-01-09         163         179        162          177     131470000             885
2013-01-10         185         198        184          195     190873000             975
2013-01-11         201         205        197          204     177049000            1020

前日との価格差を生成。

> prices<-cbind(YJ4528$YJ4528.Adjusted, YJ7261$YJ7261.Adjusted)
> head(prices)
           YJ4528.Adjusted YJ7261.Adjusted
2013-01-04            4500             925
2013-01-07            4500             900
2013-01-08            4550             855
2013-01-09            4570             885
2013-01-10            4575             975
2013-01-11            4675            1020
> price_changes<-apply(prices, 2, diff)
> head(price_changes)
           YJ4528.Adjusted YJ7261.Adjusted
2013-01-07               0             -25
2013-01-08              50             -45
2013-01-09              20              30
2013-01-10               5              90
2013-01-11             100              45
2013-01-15             120              25

プロットする。

> plot(price_changes$YJ4528.Adjusted, price_changes$YJ7261.Adjusted, xlab="4528 OnoSeiyaku price changes", ylab="7261 Matsuda price changes", main="Matsuda vs OnoSeiyaku", cex.main=0.8, cex.lab=0.8, cex.axis=0.8)
 plot(price_changes$YJ4528.Adjusted, price_changes$YJ7261.Adjusted,  でエラー: 
   引数 'x' の評価中にエラーが起きました (関数 'plot' に対するメソッドの選択時):  price_changes$YJ4528.Adjusted でエラー: 
  $ operator is invalid for atomic vectors

これでは駄目か。。。ということで下記で。

> plot(price_changes[,1], price_changes[,2], xlab="4528 OnoSeiyaku price changes", ylab="7261 Matsuda price changes", main="Matsuda vs OnoSeiyaku", cex.main=0.8, cex.lab=0.8, cex.axis=0.8)


横軸と縦軸の範囲が異なると見づらいので、揃える。

> plot(price_changes[,1], price_changes[,2], xlab="4528 OnoSeiyaku price changes", ylab="7261 Matsuda price changes", xlim=c(-700,700), ylim=c(-700,700),main="Matsuda vs OnoSeiyaku", cex.main=0.8, cex.lab=0.8, cex.axis=0.8)

線形回帰の結果とβは次の通り。

> ans<-lm(price_changes[,1] ~ price_changes[,2])
> beta<-ans$coefficients[2]
> ans

Call:
lm(formula = price_changes[, 1] ~ price_changes[, 2])

Coefficients:
       (Intercept)  price_changes[, 2]  
           12.9633              0.8618  

> beta
price_changes[, 2] 
         0.8618081 
> 

「Matsuda=β*OnoSeiyaku」の直線を加える。

> abline(0,beta)