
# File "Rjack3".

numreps = 500

n = 30

g = 5

estlist = jacklist = NULL

truncmean = function(x) {
    len = length(x)
    ord = order(x)
    s = 0
    for (ii in (g+1):(len-g))
	s = s + x[ord[ii]]
    return(s/(len-2*g))
}

for (rep in 1:numreps) {

    # Generate some data.
    x = rnorm(n)

    estimate = truncmean(x)

    # Compute the jackknife estimate of variance.
    thetam = rep(0,n)
    for (i in 1:n) {
	if (i==1)
	    xm = x[2:n]
	else if (i==n)
	    xm = x[1:(n-1)]
	else
	    xm = c(x[1:(i-1)], x[(i+1):n])
        thetam[i] = truncmean(xm)
    }

    thetadot = mean(thetam)

    jackvarest = (n-1)/n * sum((thetam - thetadot)^2)

    jacklist = c(jacklist, jackvarest)
    estlist = c(estlist, estimate)

}

truevarest = var(estlist)

print(jacklist)
print(mean(jacklist))
print(truevarest)

