Category Archives: R

The beauty of piecewise polynomials

It always amazes me  what beautiful objects may be created by very simple mathematical equations…piecewise

Continue reading

Tagged , ,

R version 2.13.1 has just been released

For a complete description of this release please refer to the latest issue of the R journal (p. 73)

Tagged ,

R Art


Continue reading

Tagged ,

How to Make Bubble Charts in R

A nice step-by-step guide seen in

ARIMA spectrum

It is ofter useful to examine time-series from a frequency perspective. Note that  the power spectrum is defined as

f_{y}(\lambda)=\underbrace{|M(e^{-i\lambda})|^{2}}_{| \Gamma ( \omega) |^{2}}f_{x}(\lambda)



is the spectral generating function. For ARIMA

f_{y}(\lambda)= \frac{\sigma}{2 \pi}\frac{|\theta(e^{-i\lambda})|^{2}}{|\phi(e^{-i\lambda})|^{2}}

The following simple R code generates spectra for ARMA class time series.

spectrums<- function (AR,MA, sigma)
n<- length(AR)
m<- length(MA)
AR<- matrix(AR)
AR[-1]<- -AR[-1]
MA<- matrix(MA)
flp<- c()
for (i in 1:100){
TAR<- matrix(exp(0:(n-1)*i*pi/100*1i))
TMA<- matrix(exp(0:(m-1)*i*pi/100*1i))
ARs<- (t(AR)%*%TAR)*Conj(t(AR)%*%TAR)
MAs<- (t(MA)%*%TMA)*Conj(t(MA)%*%TMA)
fl<- 1/(2*pi)*MAs/ARs


#instruction: spectrums(AR parameters, MA parameters, Variance)

# For AR1 AR=c(1,phi1) MA=c(1), for ARMA(2,2) AR=c(1,phi1,phi2), MA=c(1,theta1, theta2) par(mfrow=c(2,2))

#example ARMA(1,1) phi=0.4, theta=-0.6

plot(spectrums(AR<- c(1,.4),MA<- c(1,-0.6), sigma<-1),ylab="ARMA(0.4,-0.6)", type="l")

#example AR(1) phi=0.5

plot(spectrums(AR<- c(1,.5),MA<- c(1), sigma<-1),ylab="AR1(0.5)", type="l")

#example AR(1) phi=0.8

plot(spectrums(AR<- c(1,.8),MA<- c(1), sigma<-1),ylab="AR1(0.8)", type="l")

#example MA(2) theta1=-0.6, theta2=-0.4

plot(spectrums(AR<- c(1),MA<- c(1,-0.6,-0.4),sigma<-1),ylab="MA2(-0.6,-0.4)", type="l")

Note that it is also very  easy to apply a linear filter (or a series of them) to a given time series (or to generate its spectral density function). For example a moving average filter could be applied to a given AR(1) process by executing the following command

2*pi*spectrums(AR<- c(1),MA<- c(1/3,1/3,1/3),sigma<-1)*spectrums(AR<- c(1,-0.8,MA<- c(1),sigma<-1)

and a second difference filter

2*pi*spectrums(AR<- c(1),MA<- c(1,0,-1),sigma<-1)*spectrums(AR<- c(1,-0.8,MA<- c(1),sigma<-1)

Tagged , ,


An introduction to R!

Just press the play button 🙂

Tagged , , ,