For the command 'spectrum' I read: The spectrum here is defined with scaling 1/frequency(x), following S-PLUS. This makes the spectral density a density over the range (-frequency(x)/2, +frequency(x)/2], whereas a more common scaling is 2? and range (-0.5, 0.5] (e.g., Bloomfield) or 1 and range (-?, ?]. Forgive my ignorance but I am having a hard time interpreting this. Does this mean that in the spectrum output every element of the $spec array is scaled by 1/frequency(x)? I am having a hard time determing what is meant by 'frequency'.Say I define a time series for a year with samples for every day. I input a 'frequency' of 365 (which in my mind is the period). On the output of 'spectrum' would this mean that every element of the $spec array is scaled by 1/365? There is a corresponding frequency array on the output from 'spectrum'. If the frequency is 365 and an element in the frequency array output from 'spectrum' is .1 am I to assume that the period is 36.5 and a corresponding sin wave would be sin(2 * pi * 36.5/365)? Thank you in advance for helping me clear up some confusion. Kevin
This is why some help pages have references: please use them (Venables & Ripley explain the exact formulae used in R). On Tue, 9 Sep 2008, rkevinburton at charter.net wrote:> For the command 'spectrum' I read: > > The spectrum here is defined with scaling 1/frequency(x), following > S-PLUS. This makes the spectral density a density over the range > (-frequency(x)/2, +frequency(x)/2], whereas a more common scaling is 2? > and range (-0.5, 0.5] (e.g., Bloomfield) or 1 and range (-?, ?]. > > > Forgive my ignorance but I am having a hard time interpreting this. Does > this mean that in the spectrum output every element of the $spec array > is scaled by 1/frequency(x)? I am having a hard time determing what is > meant by 'frequency'.So please do look up the help for frequency().> Say I define a time series for a year with samples > for every day. I input a 'frequency' of 365 (which in my mind is the > period).The point is that your time unit is 1 year, and your measurements are every 1/365 year. That is unrelated to the 'period' (no one mentioned periodicity yet).> On the output of 'spectrum' would this mean that every element > of the $spec array is scaled by 1/365? There is a corresponding > frequency array on the output from 'spectrum'. If the frequency is 365 > and an element in the frequency array output from 'spectrum' is .1 am I > to assume that the period is 36.5 and a corresponding sin wave would be > sin(2 * pi * 36.5/365)?Hmm, you need a 't' in there (and a phase). The issue is the units for t. A frequency in the 'freq' element of the output of 0.1 corresponds to 10 cycles per unit of time, and in your example the unit of time is 365 observations. So the sine (sic) wave is sin(2*pi*0.1*t + phi), where the increments in 't' are 1/365: that gives 10 complete cycles in observations at, say, c(1990, 1) ... c(1990, 365), the days of 1990 (not a leap year).> Thank you in advance for helping me clear up some confusion. > > Kevin-- 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
Hello, [...]> ------------------------------ > > Message: 41 > Date: Tue, 9 Sep 2008 9:44:34 -0700 > From: <rkevinburton at charter.net> > Subject: [R] Help with 'spectrum' > To: r-help at r-project.org > Message-ID: <20080909124434.WMMQ2.1008129.root at mp11> > Content-Type: text/plain; charset=utf-8 > > For the command 'spectrum' I read: > > The spectrum here is defined with scaling 1/frequency(x), following > S-PLUS. This makes the spectral density a density over the range > (-frequency(x)/2, +frequency(x)/2], whereas a more common scaling is > 2? and range (-0.5, 0.5] (e.g., Bloomfield) or 1 and range (-?, ?]. > > > Forgive my ignorance but I am having a hard time interpreting this.... Hehe, better ignore, if it's unclear ;-) The term frequency might be used differently, depending on the context. :) I just want to add a note regarding fft(vect). fft() for example does not scale with 1/n (n is length(vect)). This is, how fft() is used like a continous forier transform. The discrete fourier transform is defined with 1/N (N here is te nmber of samples). This would mean, that fft() even if it is working on discrete data, is calculatd like the classical non-discrete fourier transform. To get a DFT out of fft() one must divide by length of the used vector. On the other hand, fft()'s result is then really rather a FFT than a DFT and therefore the name of that function is good choice, IMHO. But I'm just new to R and there may be more issues that one can think about. spectrum() for me also is new and I didn't looked at it in detail. As far as I understand, the result is divided by a factor also. The R-Help enry for frequency gives:00 "'frequency' returns the number of samples per unit time". Maybe that means 1/N ? But I'm not used to R's timeseries attributes, so I can't give you more help here. BTW: I've bought the book from Venable and Ripley and it's quite good. There you can find many hints. If it is sufficient information to you, I don't know. But it's a good fingerpost I think. Ciao, Oliver
Hi r-help-bounces at r-project.org napsal dne 09.09.2008 18:44:34:> For the command 'spectrum' I read: > > The spectrum here is defined with scaling 1/frequency(x), followingS-PLUS.> This makes the spectral density a density over the range(-frequency(x)/2,> +frequency(x)/2], whereas a more common scaling is 2? and range (-0.5,0.5]> (e.g., Bloomfield) or 1 and range (-?, ?]. > > > Forgive my ignorance but I am having a hard time interpreting this. Doesthis> mean that in the spectrum output every element of the $spec array isscaled by> 1/frequency(x)? I am having a hard time determing what is meant by > 'frequency'.Say I define a time series for a year with samples for everyday.> I input a 'frequency' of 365 (which in my mind is the period). On theoutput> of 'spectrum' would this mean that every element of the $spec array isscaled> by 1/365? There is a corresponding frequency array on the output from > 'spectrum'. If the frequency is 365 and an element in the frequencyarray> output from 'spectrum' is .1 am I to assume that the period is 36.5 anda> corresponding sin wave would be sin(2 * pi * 36.5/365)?Well, although I am not an expert in this matter I try to explain my humble understanding of spectrum results. sss<-spectrum(sunspots) sss$freq[which.max(sss$spec)] [1] 0.09166667 1/sss$freq[which.max(sss$spec)] [1] 10.90909 you can see that maximum of spec value is for this time series at freq 0.09 and 1/0.09 is almost eleven. Knowing that sunspots have periodicity 11 years I assume that for each series I can get an information about its periodicity by 1/freq. E.g. if your series is days 1/freq means period in days. Regards Petr> > Thank you in advance for helping me clear up some confusion. > > Kevin > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Kevin- this is a simple rescaling of the axes so that the "area under the curve" remains constant (and is half of the variance since you only look at the positive frequencies). In this case, freq(x) = 1/dx, where dx is the time between points. It is basically a graphic device so that you get pretty graphics and it's akin to drawing probability histograms so that area corresponds to probability. I think you'd get a good idea of what is going on by doing this: x <- ts(cos(2*pi*1:60*1/12)) # think of monthly high temps y <- ts(x, freq=12) par(mfrow=c(2,1)) plot(x) plot(y) # see the difference? ... which do you prefer? now do this: par(mfrow=c(2,1)) spec.pgram(x, taper=0, log="no") # freq axis is 0 to .5 cycles per unit time (which is 1) spec.pgram(y, taper=0, log="no") # freq axis is 0 to 6 cycles per unit time (which is 12) # ... which do you prefer? and you'll see by stretching the frequency axis you have to adjust the spectrum axis accordingly so that you maintain "variance under the curve". rkevinburton wrote:> > For the command 'spectrum' I read: > > The spectrum here is defined with scaling 1/frequency(x), following > S-PLUS. This makes the spectral density a density over the range > (-frequency(x)/2, +frequency(x)/2], whereas a more common scaling is 2? > and range (-0.5, 0.5] (e.g., Bloomfield) or 1 and range (-?, ?]. > > > Forgive my ignorance but I am having a hard time interpreting this. Does > this mean that in the spectrum output every element of the $spec array is > scaled by 1/frequency(x)? I am having a hard time determing what is meant > by 'frequency'.Say I define a time series for a year with samples for > every day. I input a 'frequency' of 365 (which in my mind is the period). > On the output of 'spectrum' would this mean that every element of the > $spec array is scaled by 1/365? There is a corresponding frequency array > on the output from 'spectrum'. If the frequency is 365 and an element in > the frequency array output from 'spectrum' is .1 am I to assume that the > period is 36.5 and a corresponding sin wave would be sin(2 * pi * > 36.5/365)? > > Thank you in advance for helping me clear up some confusion. > > Kevin > > ______________________________________________ > R-help at r-project.org 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. > >----- The power of accurate observation is commonly called cynicism by those who have not got it. George Bernard Shaw -- View this message in context: http://www.nabble.com/Help-with-%27spectrum%27-tp19396471p19466898.html Sent from the R help mailing list archive at Nabble.com.