varest <- function(x,m=100) { # ************************************************************************* # # FILE:varest.r # # Date Created: April 21, 2004 # Author: Robert Gray # # Contents: Estimates standard error in mean of a correlated sequence x # by using variation in means of blocks of size m. Assumes # that adjacent blocks are correlated, but blocks at greater lags # are not. # # Taken from BIO 248cd Advanced Statistical Computing Course Notes # written by Robert Gray (page 301) # # Revision History # Date Name Changes/Reasons # # ************************************************************************* # # Input: # # x: time series data # m: block size (default m = 100) # # Output: # # mean: sample mean of x # se: standard error of mean # rho: first order autocorrelation of block means # # ************************************************************************* J = floor(length(x)/m) if (J*m < length(x)) x <- x[-(1:(length(x)-J*m))] ind <- rep(1:J, rep(m,J)) Jx <- tapply(x,ind,mean) mx <- mean(Jx) rho <- cor(Jx[-1],Jx[-length(Jx)]) c(mean=mx,se=sqrt(var(Jx)*(1+2*rho)/J),rho=rho) }