Skip to content

Instantly share code, notes, and snippets.

@mcmtroffaes
Last active August 29, 2015 14:04
Show Gist options
  • Save mcmtroffaes/e42ce9ac8c350ce1b0c9 to your computer and use it in GitHub Desktop.
Save mcmtroffaes/e42ce9ac8c350ce1b0c9 to your computer and use it in GitHub Desktop.
standard error of a correlated sample
# Return start of xs as long as f evaluates to TRUE
TakeWhile <-
function(f, x, nomatch = NULL)
{
pos = Position(Negate(f), x, nomatch = 0L)
if (pos == 0L) {
## f is TRUE everywhere
if (length(x) > 0L)
x
else
nomatch
} else if (pos == 1L) {
## f is FALSE at first position
nomatch
} else {
## f is FALSE at some position > 1
x[1L:(pos - 1L)]
}
}
# Calculate standard error on a correlated sample
StdErrCorrelated <-
function(x) {
n = length(x)
# summing autocorrelations is error prone
# because we might be summing a lot of values very close to zero
# negative values are most problematic because we must take square root
# so as a very rough but always working approximation
# we sum the first non-negative elements
# under most circumstances, this gives a conservative approximation
# of the actual error
a = TakeWhile(
function(t) {t>0},
# do not go further than one 5th of sample
# see http://w3eos.whoi.edu/12.747/notes/lect06/l06s02.html
acf(x, plot=FALSE, type="covariance", lag.max=n %/% 5)$acf
)
1.96 * sqrt((2 * sum(a) - a[1]) / n)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment