[R] Seeking help with code

Stephanie Tsalwa stsalwa at hotmail.com
Thu Dec 7 23:51:55 CET 2017


Assuming the days of raining during half a year of all states(provinces) of a country is normally distributed (mean=ì, standard deviation=ó) with sigma (ó) equals to 2. We now have 10 data points here: 26.64, 30.65, 31.27, 33.04, 32.56, 29.10, 28.96, 26.44, 27.76, 32.27. Try to get the 95% level of CI for ì, using parametric Bootstrap method with bootstrap size B=8000.

my code - what am i doing wrong

#set sample size n, bootstrap size B
n = 10
b = 8000


set a vector of days of rain into "drain"
drain = c(26.64, 30.65, 31.27, 33.04, 32.56, 29.10, 28.96, 26.44, 27.76, 32.27)

#calculate mean of the sample for days of rain
mdr=mean(drain)
mdr

#calculate the parameter of the exponential distribution
lambdahat = 1.0/mdr
lambdahat

#draw the bootstrap sample from Exponential
x = rexp(n*b, lambdahat)
x

bootstrapsample = matrix(x, nrow=n, ncol=b)
bootstrapsample

# Compute the bootstrap lambdastar
lambdastar = 1.0/colMeans(bootstrapsample)
lambdastar

# Compute the differences
deltastar = lambdastar - lambdahat
deltastar

# Find the 0.05 and 0.95 quantile for deltastar
d = quantile(deltastar, c(0.05,0.95))
d

# Calculate the 95% confidence interval for lambda.
ci = lambdahat - c(d[2], d[1])
ci


	[[alternative HTML version deleted]]



More information about the R-help mailing list