-
-
Notifications
You must be signed in to change notification settings - Fork 624
Expand file tree
/
Copy pathbootstrap.R
More file actions
32 lines (26 loc) · 635 Bytes
/
bootstrap.R
File metadata and controls
32 lines (26 loc) · 635 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
## based on the code in online StatLearning(https://lagunita.stanford.edu/courses/HumanitiesSciences/StatLearning/Winter2016/info) course
library(ISLR)
library(boot)
alpha <- function(x, y)
{
vx = var(x)
vy = var(y)
cxy = cov(x, y)
(vy-cxy)/(vx+vy-2*cxy)
}
alpha(Portfolio$X, Portfolio$Y)
# [1] 0.5758321
## estimate std error
alpha.fn <- function(data, index)
{
with(data[index, ], alpha(X, Y))
}
alpha.fn(Portfolio, 1:100)
# [1] 0.5758321
set.seed(123)
alpha.fn(Portfolio, sample(1:100, 100, replace = TRUE))
# [1] 0.4896806
## use boot package
boot.out = boot(Portfolio, alpha.fn, R = 1000)
boot.out
plot(boot.out)