-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exercise #13.R.R
145 lines (114 loc) · 2.74 KB
/
Exercise #13.R.R
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
## boot 함수
library(boot)
install.packages("bootstrap")
library(bootstrap)
## 표준오차에 대한 붓스트랩 추정
data(law)
data(law82)
cor(law$LSAT, law$GPA)
cor(law82$LSAT, law82$GPA)
B = 200
n = nrow(law)
R = numeric(B)
# 직접 붓스트랩 구현
for (b in 1:B) {
i = sample(1:n, size=n, replace=T)
LSAT = law$LSAT[i]
GPA = law$GPA[i]
R[b] = cor(LSAT, GPA)
}
print(se.R <- sd(R))
hist(R, prob=TRUE)
# boot 함수 이용
r = function(x, i) {
cor(x[i,1], x[i,2])
}
obj = boot(data=law, statistic=r, R=2000) # R=B붓스트랩 반복수
obj
## 편의에 대한 붓스트랩 추정
theta.hat = cor(law$LSAT, law$GPA)
B = 2000
n = nrow(law)
theta.b = numeric(B)
for (b in 1:B) {
i = sample(1:n, size=n, replace=T)
LSAT = law$LSAT[i]
GPA = law$GPA[i]
theta.b[b] = cor(LSAT, GPA)
}
bias = mean(theta.b) - theta.hat
bias
## interval estimation for sd
dice = c(1,2,3,2,6,6,5,1,1,1,4,2,4,1,4,5,6,6,3,2,
5,6,4,1,2,3,2,2,5,3,5,6,1,4,4,4,3,5,5,1,
6,1,3,3,2,5,2,2,1,4)
sd(dice)
boot.sample = matrix(0, 200, 50)
std = rep(0, 200)
set.seed(12345)
for(n in 1:200) {
boot.sample[n,] = sample(dice, replace=T, 50)
std[n] = sd(boot.sample[n,])
}
summary(std)
hist(std, xlim=c(1.2,2.2), ylim=c(0,50), xlab="bootst.sd")
quantile(std, probs=c(0.025, 0.975))
## jackknife for patch data
data(law, package = "bootstrap")
n = nrow(law) # 데이터 수
y = law$LSAT # old - plasibo
z = law$GPA # new - old
y
z
theta.hat = cor(y, z) # 기준
theta.hat
# 편의
theta.jack = numeric(n)
for (i in 1:n)
theta.jack[i] = cor(y[-i], z[-i]) # 잭나이프 적용
bias = (n-1) * (mean(theta.jack) - theta.hat)
bias
theta.jack
# 표준오차
se = sqrt((n-1)*mean((theta.jack-mean(theta.jack))^2))
print(se)
# ==========================================
# 문항 2=======================================
aircondit
## boot 함수
library(boot)
library(bootstrap)
## 표준오차에 대한 붓스트랩 추정
B = 200
n = nrow(aircondit)
R = numeric(B)
x1 <- rexp(12)
meanx1 <- mean(x1)
meanx1
mean(aircondit$hours)
# 직접 붓스트랩 구현
for (b in 1:B) {
i = sample(1:n, size = n, replace = T)
hours = aircondit$hours[i]
R[b] = 1/mean(rexp(12))
}
print(se.R <- sd(R))
hist(R, prob=TRUE)
# boot 함수 이용
r = function(x, i) {
1 / x[i,1]
}
obj = boot(data=aircondit, statistic=r, R=2000) # R=B붓스트랩 반복수
obj
## 편의에 대한 붓스트랩 추정
theta.hat = 1/ mean(aircondit$hours)
B = 2000
n = nrow(aircondit)
theta.b = numeric(B)
for (b in 1:B) {
i = sample(1:n, size=n, replace=T)
hour = aircondit$hours[i]
theta.b[b] = 1/mean(rexp(12))
}
bias = mean(theta.b) - theta.hat
bias