[R] typo in ts detrending implementation in spec.pgram?
Mikhail Titov
tito0003 at umn.edu
Wed Jul 8 19:59:27 CEST 2009
Hello!
I wonder if there is a typo in detrending code of spec.pgram in spectrum.R from stats package.
One can see in the code https://svn.r-project.org/R/trunk/src/library/stats/R/spectrum.R .
I am afraid there is a typo and the code should look like
if (detrend) {
t <- 1L:N - (N + 1)/2
sumt2 <- N * (N^2 - 1)/12
for (i in 1L:ncol(x))
x[, i] <- x[, i] - mean(x[, i]) - sum((x[, i]-mean(x[,i]) * t) * t/sumt2
}
Note x[, i]-mean(x[,i]) instead of x[,i] only as in repository. Here is a quick reference http://en.wikipedia.org/wiki/Simple_linear_regression#Estimating_the_regression_line . Note $\hat b$ there. It has not x in summation, but x-mean(x).
Perhaps, the even better solution would be resid(lm(x[,i] ~ seq(along = x[,i]))) . See http://tolstoy.newcastle.edu.au/R/help/05/01/10115.html
Mikhail
More information about the R-help
mailing list