Re: [R] using spec.pgram

From: Anthony Mathelier <>
Date: Mon, 16 Jun 2008 15:33:18 +0200

Perhaps I'm applying spec.pgram wrong as you said. I will explain what I want, so you can tell me why I'm wrong and perhaps what I have to do to do it well.
I have some points in a 1-D space and I want to know if they are spaced at a certain periodic distance. So, I computed all the distances between points in my space. Then, I would like to know if a certain distance (period), or multiples of a certain distance, is preferred to space my data. I made a histogram of the distances and apply the spec.pgram function to know the frequence (so the period) which is the most important to space the original data.
But, when I have to sets of data (without necessarily the same number of observation in each set), I want to compare the importance of the period given by spec.pgram between the sets. Could I normalize the amplitude of the peaks given by spec.pgram?
So, am I wrong to apply this methodology to exhibit a periodic distance between my data? If, true, what could you recommend me to do this? Thanks in advance for your answers.
Best regards,


On Tue, Jun 10, 2008 at 6:13 PM, stephen sefick <> wrote:

> I from a first thought I would say that you are apply this wrong! The
> fourier transform convolves a function (cos(x)+isin(x) (this may not be the
> exact formula but I don't have my books near)) to the data and then
> integrates over -1/2 to 1/2 takes the modulus and plots this- the
> periodogram. The reason you preform a fourier transform is to look at
> recurring frequencies in the data, which are in the time domain. The
> fourier transform converts the time series into the frequency domain and
> viola you have a peak into the hidden/recurring parts of your signal. From
> your explaination your are applying this technique wrong- look at schumway,
> MASS4, et al. books to get a handle on how this technique is used. If you
> are to apply a time series analysis please use it on a time series. Maybe
> your logic is not flawed but I don't see how a histogram with its associated
> binning is a better candidate for time series analysis than the original
> time series if at all.
> good luck
> Stephen
> On Tue, Jun 10, 2008 at 8:49 AM, Matthieu Stigler <
>> wrote:
>> Hello
>> I don't know exactly what you want to do but:
>> -why do you use in your example h$counts and not h? Furthermore helpl file
>> says it should be a time series, why then rather not your time series?
>> -usually na.action will make the "default" action, which you can see by
>> getOptions("na.action")
>> -here in this function it is provided in the function values na.action =
>> so it will just remove the NA in the time series
>> -if you want to study a function, I advise you to copy it entirely, rename
>> it and then just insert print(curiousobject...) in the function, this will
>> allow you to let the function run and grasp the interessting objects, like:
>> study<-function (x, spans = NULL, kernel = NULL, taper = 0.1, pad = 0,
>> fast = TRUE, demean = FALSE, detrend = TRUE, plot = TRUE,
>> na.action =, ...)
>> {
>> series <- deparse(substitute(x))
>> x <- na.action(as.ts(x))
>> print(x)
>> xfreq <- frequency(x)
>> ...}
>> study(sunspots)
>> -when you provide an example, instead of giving an external reference for
>> the data, try to search a convenient internal data (accessed by data() ), so
>> one will be able to reproduce your problems. Here you could use sunspots
>> -to obtain the commented code... I don't know it...
>> -good luck
>> Matthieu
>> Hi everyone,
>>> first of all, I would like to say that I am a newbie in R, so I apologize
>>> in
>>> advance if my questions seem to be too easy for you.
>>> Well, I'm looking for periodicity in histograms. I have histograms of
>>> certain phenomenons and I'm asking whether a periodicity exists in these
>>> data. So, I make a periodogram with the function spec.pgram. For
>>> instance,
>>> if I have a histogram h, I call spec.pgram by spec.pgram (h, log="no",
>>> taper=0.5). So, I have some peaks that appear and I would like to
>>> interpret
>>> them but I do not know how they are computed and so what a peak with a
>>> value
>>> of 10000 represents in comparison with a peak of value 600 with another
>>> histogram.
>>> I looked at the source code of the function spec.pgram to better
>>> understand
>>> what is behind. But, when I apply the source code line by line, I've got
>>> a
>>> problem. For instance, I make:
>>>> >data = scan ("file.txt")
>>>> >h = hist (data, breaks=max(data)/5000)
>>> #then I apply the first two lines of the spec.pgram function
>>>> >series <- deparse(substitute(h$counts))
>>>> >x <- na.action(as.ts(h$counts))
>>>> >x
>>> NULL
>>> I do not understand why when I apply the first two lines of the function
>>> I
>>> have x which is equal to NULL (which make a mistake in the following
>>> lines
>>> of the code) but if I apply the function directly with h$counts it gives
>>> me
>>> a result.
>>> So, if someone can explain to me what is the problem and/or how
>>> spec.pgram
>>> exactly computes the periodogram and how to interpret it with my data, I
>>> would be so grateful.
>>> And subsidiary questions:
>>> - Is it possible to have the commented source code of the function?
>>> - I do not understand what is the function na.action in the second line
>>> of
>>> spec.pgram, so if you can explain it to me.
>>> Thanks in advance for your answers.
>>> Best regards,
>>> Anthony Mathelier
>>> [[alternative HTML version deleted]]
>> ______________________________________________
>> mailing list
>> PLEASE do read the posting guide
>> and provide commented, minimal, self-contained, reproducible code.
> --
> Let's not spend our time and resources thinking about things that are so
> little or so large that all they really do for us is puff us up and make us
> feel like gods. We are mammals, and have not exhausted the annoying little
> problems of being mammals.
> -K. Mullis

        [[alternative HTML version deleted]] mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Mon 16 Jun 2008 - 14:32:48 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 16 Jun 2008 - 17:30:42 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive