summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
t.test(cars$dist,mu=122)
##
## One Sample t-test
##
## data: cars$dist
## t = -21.683, df = 49, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 122
## 95 percent confidence interval:
## 35.65642 50.30358
## sample estimates:
## mean of x
## 42.98
Usually we test that the sample mean is within +/- 2(1.96)SE Here in R sample mean is taken and added 2 SE of the sample mean therefore reasoning is reverse : If teh true mean population is not in that interval the test will be significant
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
dim(cars)
## [1] 50 2
t.test(cars$dist,mu=43)
##
## One Sample t-test
##
## data: cars$dist
## t = -0.005488, df = 49, p-value = 0.9956
## alternative hypothesis: true mean is not equal to 43
## 95 percent confidence interval:
## 35.65642 50.30358
## sample estimates:
## mean of x
## 42.98
Note ths thge interval around the sample mean hasn’t change Only the p value and T score changes
Lets create a 100 vectors of cars$dist
library(boot)
library(psych)
##
## Attachement du package : 'psych'
## L'objet suivant est masqué depuis 'package:boot':
##
## logit
B=1000
n=nrow(cars)#nrow a df
B
## [1] 1000
n
## [1] 50
nrow(cars)
## [1] 50
boot.samples = matrix(sample(cars$dist, size = B * n, replace = TRUE),B,n)
head(boot.samples,3)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
## [1,] 16 54 68 14 32 14 46 93 10 92 84 2 2 48
## [2,] 26 42 32 28 84 18 4 32 52 4 60 66 36 80
## [3,] 85 64 48 32 80 80 36 56 32 40 4 40 40 34
## [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
## [1,] 26 24 80 22 10 36 28 22 20 64 32 56
## [2,] 46 52 84 60 26 46 80 76 85 4 26 76
## [3,] 85 46 40 68 4 93 80 56 70 17 32 4
## [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38]
## [1,] 42 85 26 2 20 32 84 50 70 26 32 34
## [2,] 36 60 46 26 32 93 24 52 34 16 36 54
## [3,] 64 54 93 22 10 34 32 60 32 2 54 17
## [,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49] [,50]
## [1,] 34 22 56 56 24 34 36 28 66 28 16 60
## [2,] 120 64 80 36 26 26 26 60 34 85 17 50
## [3,] 36 70 34 20 76 42 28 70 93 26 28 26
rowMeans(boot.samples)#there is 1000 samples of vectors
## [1] 39.16 47.16 45.78 47.94 41.24 42.78 42.14 48.38 43.52 37.92 45.32 40.82
## [13] 39.82 43.48 40.76 46.02 40.90 47.58 47.00 40.86 47.74 42.52 40.48 40.70
## [25] 42.20 37.68 47.72 41.46 40.56 40.52 44.88 39.90 43.86 44.34 40.12 46.84
## [37] 42.72 47.40 43.38 47.70 41.62 46.68 40.22 43.22 40.90 41.32 46.86 41.92
## [49] 42.54 39.82 46.76 48.54 36.60 44.56 45.30 48.56 39.92 40.66 41.16 38.44
## [61] 44.82 42.54 48.22 40.36 45.28 39.04 38.40 38.04 37.54 44.12 39.42 48.10
## [73] 44.22 39.54 46.54 38.90 40.20 39.80 40.88 49.92 39.80 41.02 39.94 44.36
## [85] 37.58 37.34 46.60 39.72 42.62 37.98 37.84 46.48 43.34 45.06 37.38 46.32
## [97] 47.34 39.32 42.02 40.12 50.12 42.50 40.44 40.16 43.84 43.60 37.40 44.52
## [109] 43.50 39.12 37.52 41.34 49.98 48.82 45.68 48.42 40.42 39.94 41.78 41.16
## [121] 40.86 41.04 41.54 39.84 43.34 40.28 38.16 41.88 42.90 41.88 39.34 49.58
## [133] 47.84 36.18 41.34 43.94 40.88 49.00 41.26 47.66 50.00 41.82 45.50 44.94
## [145] 45.00 40.44 41.32 37.72 38.76 46.84 31.26 41.20 45.78 45.38 39.84 39.24
## [157] 36.42 46.78 47.32 42.26 41.58 41.78 49.68 43.14 39.36 42.68 44.22 42.50
## [169] 37.88 49.64 44.92 42.94 46.28 43.98 47.44 47.76 44.82 44.22 44.02 44.86
## [181] 40.08 43.70 41.16 51.02 40.74 45.76 35.20 35.10 48.22 41.00 40.08 42.64
## [193] 42.84 46.02 42.86 39.90 42.98 42.06 36.40 43.86 35.44 48.56 41.18 35.94
## [205] 41.12 45.56 40.06 40.36 47.48 40.52 40.52 47.10 38.50 51.46 33.28 43.56
## [217] 42.20 43.08 40.92 44.88 35.80 41.94 43.80 43.08 43.40 41.18 40.06 46.94
## [229] 42.82 39.76 34.48 37.34 44.92 47.68 39.20 35.10 42.22 45.82 37.58 38.52
## [241] 45.96 40.44 49.46 41.52 45.62 47.28 43.00 48.30 42.20 44.72 42.08 47.48
## [253] 39.56 45.18 44.14 47.94 47.96 41.18 43.30 45.40 42.64 42.10 37.52 41.46
## [265] 40.92 39.14 43.56 44.00 43.50 43.54 44.66 44.06 36.88 37.50 41.04 43.26
## [277] 47.66 36.28 47.14 47.62 38.46 44.46 42.82 44.56 40.58 46.22 47.96 42.96
## [289] 48.16 37.82 41.56 45.96 37.40 45.60 44.08 44.54 42.66 48.18 47.36 42.50
## [301] 49.00 43.16 35.60 40.24 41.24 47.86 40.78 48.06 45.72 40.66 43.52 43.66
## [313] 44.92 43.28 41.46 38.56 40.32 37.60 47.12 41.72 48.72 38.60 43.64 36.30
## [325] 41.02 41.96 39.58 43.84 37.54 42.46 38.92 48.22 49.50 42.70 44.44 42.62
## [337] 39.06 38.68 43.38 43.98 36.36 39.44 39.72 41.32 47.44 46.64 36.74 40.58
## [349] 37.24 39.26 42.32 44.48 44.24 44.72 45.72 40.24 47.30 42.04 38.52 42.70
## [361] 38.80 38.68 48.92 33.82 50.44 48.32 33.38 39.80 38.28 48.18 39.90 50.40
## [373] 45.34 46.34 39.94 44.30 45.98 41.96 39.60 42.02 40.16 48.28 46.44 39.92
## [385] 45.12 43.62 39.58 43.96 37.82 44.16 46.26 40.76 38.62 40.46 38.64 35.48
## [397] 44.10 46.44 44.22 43.58 40.08 43.22 41.66 46.76 43.10 44.24 43.76 36.84
## [409] 41.10 42.38 41.60 45.86 49.10 44.50 50.78 40.36 40.88 43.98 44.30 38.62
## [421] 42.26 38.62 46.00 45.60 51.02 40.28 37.90 41.54 44.82 39.66 46.86 48.34
## [433] 42.90 44.62 43.68 41.40 41.10 42.50 43.82 41.10 43.48 37.36 37.46 43.70
## [445] 41.50 43.26 46.72 37.06 38.62 42.04 48.46 41.18 47.24 39.14 42.50 47.20
## [457] 46.60 48.02 41.54 46.42 39.14 41.94 46.74 49.86 48.10 41.72 44.16 39.36
## [469] 40.96 41.00 43.48 48.94 40.08 51.12 48.54 38.16 48.92 47.40 43.54 47.52
## [481] 45.58 45.48 41.36 41.58 41.90 44.28 49.16 44.46 42.38 44.50 44.82 42.34
## [493] 34.92 40.68 40.34 46.46 40.98 44.22 42.32 46.62 42.38 46.80 42.78 45.92
## [505] 43.96 41.06 43.84 41.64 50.26 42.40 41.08 41.30 40.22 40.96 42.58 50.04
## [517] 42.74 44.02 38.98 42.46 48.88 42.34 46.00 42.36 39.74 51.78 39.80 41.24
## [529] 43.58 53.44 41.38 43.02 43.92 36.74 46.72 42.58 47.08 48.06 36.76 40.76
## [541] 43.50 37.82 42.80 40.70 42.82 48.30 48.60 41.12 39.48 49.04 39.58 47.20
## [553] 42.98 47.26 36.94 39.12 44.88 47.98 39.40 40.18 37.40 48.54 51.32 38.64
## [565] 39.10 40.64 47.22 46.88 40.78 34.20 48.52 41.72 45.96 48.98 47.12 45.80
## [577] 44.42 40.36 43.10 39.08 42.38 40.44 44.24 43.56 37.10 41.32 42.62 43.64
## [589] 49.12 47.10 44.42 43.02 48.00 45.08 38.40 48.40 40.24 39.40 41.76 42.44
## [601] 47.30 44.96 42.92 38.30 46.42 42.58 39.68 46.08 42.96 40.22 39.00 40.20
## [613] 45.56 50.72 36.96 43.30 41.64 41.10 41.28 49.74 44.58 38.96 46.00 38.92
## [625] 43.74 43.88 46.26 45.62 43.70 45.70 43.66 44.88 40.24 42.70 42.76 42.96
## [637] 42.70 45.08 48.14 46.40 44.88 36.44 55.56 42.32 42.54 46.44 45.00 43.80
## [649] 40.38 45.12 43.24 55.28 40.84 42.68 46.90 48.20 45.34 43.40 44.90 42.76
## [661] 40.90 44.24 45.02 39.36 42.50 38.30 40.46 45.42 38.10 46.86 35.96 43.28
## [673] 36.52 44.78 38.26 40.56 39.98 41.90 44.40 38.92 44.00 41.58 37.80 39.42
## [685] 53.46 39.66 40.80 43.76 47.78 44.72 42.84 41.92 37.14 36.46 38.54 38.88
## [697] 37.48 43.30 48.58 43.02 37.60 41.32 42.92 41.58 48.26 40.62 47.70 43.70
## [709] 39.34 40.38 44.44 43.98 46.58 41.02 41.54 42.18 44.72 39.92 42.44 48.20
## [721] 42.60 43.36 44.00 40.02 39.42 42.30 46.12 41.44 45.94 38.50 45.38 39.70
## [733] 42.90 39.36 40.72 44.92 42.78 39.98 43.66 35.52 47.96 38.12 42.38 43.08
## [745] 41.70 41.56 48.10 47.88 45.74 37.28 43.08 39.16 42.50 40.72 47.76 40.66
## [757] 38.94 49.94 46.54 46.50 51.54 44.84 45.36 43.74 42.56 42.26 40.74 44.46
## [769] 47.10 42.84 39.20 43.34 39.98 41.36 40.84 48.80 46.70 43.18 43.06 47.30
## [781] 49.94 41.66 41.46 41.22 39.12 35.98 40.06 38.72 44.54 49.52 43.08 49.24
## [793] 42.82 41.12 46.02 45.84 45.56 39.70 39.84 38.30 43.08 37.60 44.50 40.66
## [805] 50.78 41.08 43.78 53.12 47.74 41.68 47.36 46.28 43.96 39.94 42.20 38.64
## [817] 44.60 38.94 47.90 36.56 42.02 40.78 42.32 45.96 45.16 45.84 40.72 50.36
## [829] 42.56 37.54 43.14 40.34 48.28 40.42 44.84 39.78 41.52 37.78 39.46 42.32
## [841] 43.16 45.16 47.36 41.10 41.68 39.14 42.60 34.28 42.66 40.62 47.90 42.16
## [853] 48.72 43.08 47.34 41.58 39.42 47.48 41.82 45.08 40.98 44.26 45.76 42.80
## [865] 43.18 41.86 42.74 39.82 43.72 46.78 45.50 44.54 39.86 43.76 50.68 43.24
## [877] 43.58 45.52 42.50 46.30 38.28 41.26 33.94 42.52 43.80 46.16 42.16 49.76
## [889] 42.54 46.32 39.30 37.86 43.04 40.30 44.38 46.14 42.14 47.76 45.78 41.96
## [901] 48.02 32.70 41.30 42.74 42.00 37.32 40.86 42.34 42.78 42.96 38.98 44.18
## [913] 40.48 42.20 43.18 42.72 45.12 42.28 46.04 42.76 45.52 47.28 45.18 39.38
## [925] 42.08 45.48 38.34 46.10 43.20 42.58 41.34 44.82 39.52 41.64 42.10 50.26
## [937] 42.52 40.84 50.26 44.66 34.10 44.14 45.32 45.62 44.34 46.58 44.26 41.82
## [949] 45.02 40.54 49.30 38.54 45.12 41.36 40.54 38.54 43.38 50.82 41.50 44.06
## [961] 42.14 49.02 42.26 43.20 42.16 44.62 48.00 44.96 40.76 43.06 42.18 38.68
## [973] 43.02 42.50 42.38 44.04 46.40 49.44 43.62 40.44 46.90 42.06 42.72 46.62
## [985] 44.68 52.44 39.06 42.10 40.46 42.00 46.08 44.26 42.08 48.74 39.62 42.08
## [997] 37.50 35.04 36.24 45.48
#dont get confused with column here
colMeans(boot.samples)#not 50 samples the matrix is built sample as rows!!!
## [1] 42.797 43.522 42.953 42.251 43.009 42.779 44.150 42.524 42.316 42.712
## [11] 42.721 43.529 44.191 42.566 42.821 43.759 43.179 42.479 42.038 43.360
## [21] 43.088 43.394 43.219 43.871 41.981 43.668 42.097 41.034 42.782 43.262
## [31] 43.015 43.328 43.204 43.150 44.881 42.416 42.496 42.733 43.545 44.632
## [41] 43.112 43.108 42.041 43.068 43.543 42.086 41.741 41.176 43.053 43.649
hist(rowMeans(boot.samples))#there is 1000 samples of vectors)
#take the sd
sd(rowMeans(boot.samples))#sd of mean = SE of a samples 1000 1/B sd of each sample
## [1] 3.645633
describe(rowMeans(boot.samples))
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 1000 42.96 3.65 42.72 42.92 3.62 31.26 55.56 24.3 0.14 -0.07
## se
## X1 0.12
1.96*3.42
## [1] 6.7032
mean(boot.samples)+6.70
## [1] 49.66058
mean(boot.samples)-6.70
## [1] 36.26058
hist(rowMeans(boot.samples))#there is 1000 samples of vectors)
abline(v=36.19)
abline(v=49.59)##this is the SE or 1000 mean samples
##this is the SE or 1000 mean samples
D=rowMeans(boot.samples)#there is 1000 samples of vectors
quantile(D,c(0.05,0.95))
## 5% 95%
## 37.359 48.981
hist(rowMeans(boot.samples))#there is 1000 samples of vectors)
abline(v=37.279,col=2)
abline(v=49.223,col=2)##this is the SE or 1000 mean samples
Now compare to T test interval
95 percent confidence interval: Gives you : 35.65642 50.30358
hist(rowMeans(boot.samples))#there is 1000 samples of vectors)
abline(v=37.279,col=2)
abline(v=48.76,col=2)##this is the SE or 1000 mean samples
abline(v=35.65,col=3)
abline(v=50.30,col=3)##this is the SE or 1000 mean samples
Check with boot pacakges:
library(boot)
my.mean=function(x,indices) {
return(mean(x[indices]))
}
time.boot=boot(cars$dist,my.mean,1000)
sd(time.boot$t)
## [1] 3.521167
boot.ci(time.boot)
## Warning in boot.ci(time.boot): les variances de bootstrap sont nécessaires pour
## les intervalles studentisés
## BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
## Based on 1000 bootstrap replicates
##
## CALL :
## boot.ci(boot.out = time.boot)
##
## Intervals :
## Level Normal Basic
## 95% (36.05, 49.85 ) (36.54, 50.22 )
##
## Level Percentile BCa
## 95% (35.74, 49.42 ) (35.74, 49.41 )
## Calculations and Intervals on Original Scale