I have a dataset with the number of hourly visits an animal made during a period of 12 months. I want to use the Fast Fourier Transform to examine cyclical patterns and periodicity. In the past, I have used Statistica for this this; however, I would like to use R to get a plot of the spectral density vs. period. Is there an easy way to do this in R? I would like to identify 12 and 24 hr peak in activity if possible.
相关问题
- R - Quantstart: Testing Strategy on Multiple Equit
- Using predict with svyglm
- Reshape matrix by rows
- Extract P-Values from Dunnett Test into a Table by
- split data frame into two by column value [duplica
相关文章
- How to convert summary output to a data frame?
- How to plot smoother curves in R
- Paste all possible diagonals of an n*n matrix or d
- ess-rdired: I get this error “no ESS process is as
- How to use doMC under Windows or alternative paral
- dyLimit for limited time in Dygraphs
- Saving state of Shiny app to be restored later
- How to insert pictures into each individual bar in
You may consider the following functions.
periodogram
fromTSA
package immediately plots a periodogram.periodogram
fromGeneCycle
returns a list of frequencies and estimated power spectral densities. It is a wrapper function forstats::spectrum
with some special options set.spectrum
fromstats
allows you to choose the method used to estimate the spectral density: either periodogram or using autoregressive process.cpgram
fromstats
plots a cumulative periodogram along with a confidence interval.See, e.g.,
?cpgram
or?spectrum
for all the details and keep in mind that it is, e.g.,TSA::periodogram
andGeneCycle::periodogram
when names of the functions coincide.There are also plenty of examples and tutorials online on how to use those functions. See here for the usage of
fft
and here for an even more extensive tutorial.Also, as you probably already know, a given time series must be detrended. Hence, use, e.g.,
diff(x)
instead ofx
. And finally, the length of your time series must be divisible by 12 as to be able to identify 12 and 24 hours frequencies, it can be achieved by, e.g.,x[-(1:(length(x) %% 12))]
, wherex
is a detrended time series.Use
spectrum
to do a spectral density analysis; alsofft
for the base fast Fourier transform.