残差・残差平方和・残差分散

Equation
\hat{y}_{t}=\alpha+\beta x_{t}
残差=y_{t}-\hat{y}_{t}

残差平方和(RSS)=\sum^{n}_{t=1}(y_{t}-\hat{y}_{t})^{2}

残差分散=\frac{RSS}{n-p-1},\;p:説明変数の個数

R
サンプル:2013年の日経平均株価終値(日次)と東京市場におけるドル円為替レート(日次)
nikkeidailyusdjpydailyresidualsusdnikkei

> library(lmtest)

> plot(dataset$nikkei,main="Nikkei.Close.Daily.2013/1/4-2013/12/30",ylab="JPY" ,type="l")
> plot(dataset$usdjpy,main="USDJPY.Daily.2013/1/4-2013/12/30",ylab="JPY/USD" ,type="l")

> adf.test(dataset$nikkei)

        Augmented Dickey-Fuller Test

data:  dataset$nikkei
Dickey-Fuller = -2.5335, Lag order = 6, p-value = 0.3514
alternative hypothesis: stationary

> adf.test(diff(dataset$nikkei))

        Augmented Dickey-Fuller Test

data:  diff(dataset$nikkei)
Dickey-Fuller = -5.5277, Lag order = 6, p-value = 0.01
alternative hypothesis: stationary

 警告メッセージ: 
In adf.test(diff(dataset$nikkei)) : p-value smaller than printed p-value

> adf.test(dataset$usdjpy)

        Augmented Dickey-Fuller Test

data:  dataset$usdjpy
Dickey-Fuller = -2.4894, Lag order = 6, p-value = 0.3699
alternative hypothesis: stationary

> adf.test(diff(dataset$usdjpy))

        Augmented Dickey-Fuller Test

data:  diff(dataset$usdjpy)
Dickey-Fuller = -6.4031, Lag order = 6, p-value = 0.01
alternative hypothesis: stationary

 警告メッセージ: 
In adf.test(diff(dataset$usdjpy)) : p-value smaller than printed p-value

> ols <- lm(dataset$nikkei~dataset$usdjpy)
> summary(ols)

Call:
lm(formula = dataset$nikkei ~ dataset$usdjpy)

Residuals:
     Min       1Q   Median       3Q      Max 
-1481.80  -338.96    21.89   414.37   996.79 

Coefficients:
                 Estimate Std. Error t value Pr(>|t|)    
(Intercept)    -21191.218    849.552  -24.94   <2e-16 ***
dataset$usdjpy    355.847      8.688   40.96   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 511.9 on 243 degrees of freedom
Multiple R-squared:  0.8735,    Adjusted R-squared:  0.8729 
F-statistic:  1677 on 1 and 243 DF,  p-value: < 2.2e-16

> zansa <- ols$residuals
> plot(zansa,type="l",main="Residuals.Ols of USDJPY-Nikkei")
> adf.test(zansa)

        Augmented Dickey-Fuller Test

data:  zansa
Dickey-Fuller = -4.1629, Lag order = 6, p-value = 0.01
alternative hypothesis: stationary

 警告メッセージ: 
In adf.test(zansa) : p-value smaller than printed p-value
> dw <- dwtest(dataset$nikkei~dataset$usdjpy)
> dw

        Durbin-Watson test

data:  dataset$nikkei ~ dataset$usdjpy
DW = 0.2523, p-value < 2.2e-16
alternative hypothesis: true autocorrelation is greater than 0

参考文献
森村英典[他](1984).『統計・OR活用事典』.東京書籍.485pp.
豊田利久[他](2010).『基本統計学(第3版)』.東洋経済新報社.261pp.

アプリケーション
R Core Team (2013). R: A language and environment for statistical computing.
R Foundation for Statistical Computing, Vienna, Austria.
URL http://www.R-project.org/.