重回帰分析の標準化残差 Last modified: Sep 04, 2008
目的
重回帰分析の回帰診断を行うときに必要な,標準化残差を計算する
使用法
std.res(x, y)
引数
x 説明変数だけのデータ行列(行がケース,列が変数。従属変数は加えてはいけない)
y 従属変数のデータベクトル
ソース
インストールは,以下の 1 行をコピーし,R コンソールにペーストする
source("http://aoki2.si.gunma-u.ac.jp/R/src/stdres.R", encoding="euc-jp")
# 重回帰分析の標準化残差
std.res <- function( x, # 説明変数だけのデータ行列
y) # 従属変数のデータベクトル
{
ans <- lm(y ~ x) # 重回帰分析を行う
x <- cbind(1, as.matrix(x))
s2 <- sum(ans$residuals^2)/(nrow(x)-ncol(x))
h <- sqrt(s2*(1-rowSums((x %*% solve(t(x)%*%x))* x)))
retv <- cbind(y, ans$fitted.values, ans$residuals, ans$residuals/h)
colnames(retv) <- c("y","fitted","residual","std.residual")
return(retv)
}
使用例
x <- matrix(c( # 5ケース,3変数のデータ行列例(ファイルから読んでも良い)
1, 6, 4,
3, 2, 5,
4, 3, 3,
2, 1, 3,
5, 4, 7,
3, 4, 2
), byrow=TRUE, ncol=3)
y <- c(4, 2, 6, 5, 4, 3)
std.res(x, y)
出力結果例
y fitted residual std.residual
1 4 3.469512 0.5304878 1.1790177
2 2 3.695122 -1.6951220 -1.0641738
3 6 4.567073 1.4329268 0.9432276
4 5 3.993902 1.0060976 0.8703883
5 4 3.682927 0.3170732 0.3939394
6 3 4.591463 -1.5914634 -1.1790177
直前のページへ戻る
E-mail to Shigenobu AOKI