colliera at ukzn.ac.za
2006-Nov-27 14:42 UTC
[R] NaN with ccf() for vector with all same element
hello, i have been using ccf() to look at the correlation between lightning and electrogamnetic data. for the most part it has worked exactly as expected. however, i have come across something that puzzles me a bit:> x <- c(1, 0, 1, 0, 1, 0) > y <- c(0, 0, 0, 0, 0, 0) > ccf(x, x, plot = FALSE)Autocorrelations of series 'X', by lag -4 -3 -2 -1 0 1 2 3 4 0.333 -0.500 0.667 -0.833 1.000 -0.833 0.667 -0.500 0.333> ccf(x, y, plot = FALSE)Autocorrelations of series 'X', by lag -4 -3 -2 -1 0 1 2 3 4 NaN NaN NaN NaN NaN NaN NaN NaN NaN> y <- c(1, 1, 1, 1, 1, 1) > ccf(x, y, plot = FALSE)Autocorrelations of series 'X', by lag -4 -3 -2 -1 0 1 2 3 4 NaN NaN NaN NaN NaN NaN NaN NaN NaN i don't see why the result from ccf() would be NaN if the elements of y are all the same... perhaps i am just being silly or missing something. but if i work this out by hand, then i get a proper result. so, why not with ccf()? thanks for the help! best regards, andrew. -- Andrew B. Collier Space Physics Group Hermanus Magnetic Observatory Antarctic Research Fellow tel: +27 31 2601157 Space Physics Research Institute fax: +27 31 2616550 University of KwaZulu-Natal, Durban, 4041, South Africa gsm: +27 83 3813655
Gabor Grothendieck
2006-Nov-27 14:51 UTC
[R] NaN with ccf() for vector with all same element
The denominator holds the variance and since the variance is zero you get that. Calculate the covariance instead of the correlation: ccf(x, y, plot = FALSE, type = "cov") On 11/27/06, colliera at ukzn.ac.za <colliera at ukzn.ac.za> wrote:> hello, > > i have been using ccf() to look at the correlation between lightning and electrogamnetic data. for the most part it has worked exactly as expected. however, i have come across something that puzzles me a bit: > > > x <- c(1, 0, 1, 0, 1, 0) > > y <- c(0, 0, 0, 0, 0, 0) > > ccf(x, x, plot = FALSE) > > Autocorrelations of series 'X', by lag > > -4 -3 -2 -1 0 1 2 3 4 > 0.333 -0.500 0.667 -0.833 1.000 -0.833 0.667 -0.500 0.333 > > ccf(x, y, plot = FALSE) > > Autocorrelations of series 'X', by lag > > -4 -3 -2 -1 0 1 2 3 4 > NaN NaN NaN NaN NaN NaN NaN NaN NaN > > y <- c(1, 1, 1, 1, 1, 1) > > ccf(x, y, plot = FALSE) > > Autocorrelations of series 'X', by lag > > -4 -3 -2 -1 0 1 2 3 4 > NaN NaN NaN NaN NaN NaN NaN NaN NaN > > i don't see why the result from ccf() would be NaN if the elements of y are all the same... perhaps i am just being silly or missing something. but if i work this out by hand, then i get a proper result. so, why not with ccf()? > > thanks for the help! > > best regards, > andrew. > > -- > Andrew B. Collier > > Space Physics Group > Hermanus Magnetic Observatory > > Antarctic Research Fellow tel: +27 31 2601157 > Space Physics Research Institute fax: +27 31 2616550 > University of KwaZulu-Natal, Durban, 4041, South Africa gsm: +27 83 3813655 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Prof Brian Ripley
2006-Nov-27 16:12 UTC
[R] NaN with ccf() for vector with all same element
On Mon, 27 Nov 2006, colliera at ukzn.ac.za wrote:> hello, > > i have been using ccf() to look at the correlation between lightning and > electrogamnetic data. for the most part it has worked exactly as > expected. however, i have come across something that puzzles me a bit: > >> x <- c(1, 0, 1, 0, 1, 0) >> y <- c(0, 0, 0, 0, 0, 0) >> ccf(x, x, plot = FALSE) > > Autocorrelations of series 'X', by lag > > -4 -3 -2 -1 0 1 2 3 4 > 0.333 -0.500 0.667 -0.833 1.000 -0.833 0.667 -0.500 0.333 >> ccf(x, y, plot = FALSE) > > Autocorrelations of series 'X', by lag > > -4 -3 -2 -1 0 1 2 3 4 > NaN NaN NaN NaN NaN NaN NaN NaN NaN >> y <- c(1, 1, 1, 1, 1, 1) >> ccf(x, y, plot = FALSE) > > Autocorrelations of series 'X', by lag > > -4 -3 -2 -1 0 1 2 3 4 > NaN NaN NaN NaN NaN NaN NaN NaN NaN > > i don't see why the result from ccf() would be NaN if the elements of y > are all the same... perhaps i am just being silly or missing something. > but if i work this out by hand, then i get a proper result. so, why not > with ccf()?How do you get a non-zero value for the variance of y? Dividing zero by zero is NaN, and that is what is happening here. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595