[R] timeseries: R/S (rescaled range) analysis

Christoph Helwig Christoph.Helwig at gmx.net
Thu Jun 21 19:47:52 CEST 2001


Jeff Haferman wrote:
> 
> Has anyone written utilities to do rescaled range analysis in R?

What do you mean by R/S analysis? Testing for long memory, estimating H?
If you want to do tests, have a look at the functions below. They are
quite `raw' but were just written for my personal usage. So don´t be to
critical.

Hope that helps.

Christoph

## rs.test calculates the statistic of the modified R/S test
##
## x: time series
## q: number of lags included for calculation of covariances
##
## significance level: 0.05,     0.1
## critical value:     1.747,    1.62
##
## References: Lo (1991), Long-term Memory in Stock Market Prices,
Econometrica 59, 1279--1313
##
rs.test <- function(x, q, alpha)
{	
	xbar <- mean(x)
	N <- length(x)
	r <- max(cumsum(x-xbar)) - min(cumsum(x-xbar))
	kovarianzen <- NULL
	for (i in 1:q)
	{	
		kovarianzen <- c(kovarianzen,
sum((x[1:(N-i)]-xbar)*(x[(1+i):N]-xbar)))
	}
	if (q > 0)
		s <- sum((x-xbar)^2)/N + sum((1-(1:q)/(q+1))*kovarianzen)*2/N
	else
		s <- sum((x-xbar)^2)/N
	rs <- r/(sqrt(s)*sqrt(N))
	method <- "R/S Test for Long Memory"
	names(rs) <- "R/S Statistic"
	names(q) <- "Bandwidth q"
	structure(list(statistic = rs, parameter = q, method = method,
data.name=deparse(substitute(x))), class="htest")
}

## vs.test calculates the statistic of the modified V/S test
##
## x: time series
## q: number of lags included for calculation of covariances
## 
## significance level: 0.01,   0.05,     0.1
## critical value:     0.2685, 0.1869,   0.1518
##
## References: Giraitis, Kokoszka und Leipus (2000), Rescaled variance
and related tests for long memory in volatility and levels
##
vs.test <- function(x, q, alpha)
{	
	xbar <- mean(x)
	N <- length(x)
	v <- sum((cumsum(x-xbar))^2) - (sum(cumsum(x-xbar)))^2/N
	kovarianzen <- NULL
	for (i in 1:q)
	{	
		kovarianzen <- c(kovarianzen,
sum((x[1:(N-i)]-xbar)*(x[(1+i):N]-xbar)))
	}
	if (q > 0)
		s <- sum((x-xbar)^2)/N + sum((1-(1:q)/(q+1))*kovarianzen)*2/N
	else
		s <- sum((x-xbar)^2)/N
	vs <- v/(s*N^2)
	method <- "V/S Test for Long Memory"
	names(vs) <- "V/S Statistic"
	names(q) <- "Bandwidth q"
	structure(list(statistic = vs, parameter = q, method = method,
data.name=deparse(substitute(x))), class="htest")
}


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-- 
Christoph Helwig * Meitnerweg 7 * 44227 Dortmund * +49-(0)231-751708

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list