axjack's blog

### axjack is said to be an abbreviation for An eXistent JApanese Cool Klutz ###

統計学入門 p.65 問3.4 ブートストラップ(途中)

データ

data.x <- c(71,68,66,67,70,71,70,73,72,65,66)
data.y <- c(69,64,65,63,65,62,65,64,66,59,62)

相関係数

> cor(data.x, data.y)
[1] 0.5580547

ブートストラップ

11組のデータからランダムに11個復元抽出し相関係数を計算する、ような関数を作る。

bs <- function(x){
  bx <- sample(data.x,size = 11,replace = T)
  by <- sample(data.y,size = 11,replace = T)
  return( cor(bx,by) )
}

上の関数を10,000回繰り返す。

data.r <- sapply(1:10000,bs)

結果

par(mfrow=c(2,1))
hist(data.r,breaks = seq(-1,1,0.05), main="Histogram of correlation",freq = F,xlab="range of r")
lines(density(data.r), col = "orange", lwd = 2)
boxplot(data.r,horizontal = T)

f:id:axjack:20181224110122p:plain

考察

相関係数が0になることもあるよってことなのだろうか?ブートストラップ法について調べる必要がある。

axjack is said to be an abbreviation for An eXistent JApanese Cool Klutz.