CS计算机代考程序代写 # ST4060/ST6015/ST6040 CA1 2020-21

# ST4060/ST6015/ST6040 CA1 2020-21
# R script with solutions
# Eric Wolsztynski

# ——————————————————
# Question 1

set.seed(6040)
M = 1000
N = 1000
nu = numeric(M)
for(m in 1:M){
ib = sample(1:N, N, replace=TRUE)
nu[m] = length(unique(ib))
}
mean(nu)/N

# ——————————————————
# Question 2

set.seed(6015)
M = 1000
N = 100
a = 3
b = 2
means = numeric(M)
for(m in 1:M){
x = rgamma(N,shape=a,rate=b)
means[m] = mean(x)
}
mean(means)
a/b # theoretic value of E(X)
sd(means)

# ——————————————————
# Question 3

set.seed(4060)
B = 100
N = nrow(trees)
x = trees$Girth
y = trees$Height
ls0 = lm(y~x)$coef

cb = matrix(NA, nrow=B, ncol=2)
for(b in 1:B){
ib = sample(1:N, N, replace=TRUE)
xb = x[ib]
yb = y[ib]
lmb = lm(yb~xb)
cb[b,] = coef(lmb)
}

boxplot(cb[,2])
mean(cb[,2])
sd(cb[,2])
2*ls0[2] – quantile(cb[,2], probs=c(0.975,0.025))