0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

一般化逆行列を使って重回帰分析

Posted at

一般化逆行列を使って重回帰分析をする

パッケージが完備されている R では実際に使う場面は少ないが,以下のようにして(一般化逆行列を使って)重回帰分析の偏回帰係数を求めることもできる。
ふ〜〜ん,どこがうれしいんだろう?

library(MASS)

df = structure(list(x1 = c(25.43, 18.56, 28.66, 13.52, 30.52, 30.16, 
    29.3, 31.28, 31.46, 29.21, 22.7, 34.47, 37.38, 13.38, 16.98), 
    x2 = c(78.11, 76.34, 79.05, 72.24, 77.86, 77.88, 79.26, 79.38, 
    79.14, 77.63, 76.8, 78.88, 78.9, 73.97, 75.59), x3 = c(34.35, 
    35.76, 44, 35.26, 41.97, 50.14, 35.15, 49.57, 46.8, 37.07, 
    37.95, 53.9, 54.16, 42.15, 30.25), x4 = c(98.88, 100.48, 
    103.21, 97.97, 97.32, 98.35, 98.55, 97.74, 101.98, 102.35, 
    88.83, 100.21, 104.02, 92.01, 92.59), y = c(53.28, 56.91, 
    54.52, 50.13, 59.89, 53.43, 57.35, 55.13, 53.71, 36.1, 47.18, 
    59.9, 67.05, 42.44, 32.98)), class = "data.frame", row.names = c(NA, -15L))

ans = lm(y ~ x1 + x2 + x3 + x4, data=df)
coef(ans)
# (Intercept)          x1          x2          x3          x4 
# -55.0725370   0.1097721   0.4557835   0.4658911   0.5025229 
ginv(cbind(rep(1, nrow(df)), as.matrix(df[,1:4]))) %*% df[,5]
#             [,1]
# [1,] -55.0725370
# [2,]   0.1097721
# [3,]   0.4557835
# [4,]   0.4658911
# [5,]   0.5025229
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?