Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
With R Examples
Second Edition
• HOME
• CHAPTER 1
• CHAPTER 2
• CHAPTER 3
• CHAPTER 4
• CHAPTER 5
Disclaimer: Sometimes I use t for time ... e.g., t = 1984:2001. This is "bad R practice"
because t is used for matrix transpose, i.e., t(A) is the transpose of matrix A. If you don't want
to be bad, you can replace my t in the code below with something else. I like to be bad
sometimes, but you've been warned ... so don't blame me if you turn to a life of crime.
CHAPTER 1
Example 1.1
Example 1.6
fmri = read.table(“c:/data/fmri.dat")
par(mfrow=c(2,1)) # sets up the graphics - ?par for info
on mfrow()
ts.plot(fmri[,2:5], lty=c(1,4), ylab="BOLD")
ts.plot(fmri[,6:9], lty=c(1,4), ylab="BOLD")
Example 1.7
x = matrix(scan(“c:/data/eq5exp6.dat"), ncol=2)
par(mfrow=c(2,1))
plot.ts(x[,1], main="Earthquake", ylab="EQ5")
plot.ts(x[,2], main="Explosion", ylab="EXP6")
Example 1.9
Example 1.10
Example 1.11
set.seed(154)
w = rnorm(200,0,1)
x = cumsum(w)
wd = w +.2
xd = cumsum(wd)
plot.ts(xd, ylim=c(-5,55))
lines(x)
lines(.2*(1:200), lty="dashed")
Example 1.12
t = 1:500
c = 2*cos(2*pi*t/50 + .6*pi)
w = rnorm(500,0,1)
par(mfrow=c(3,1))
plot.ts(c)
plot.ts(c + w)
plot.ts(c + 5*w)
Example 1.24
speech = scan(“c:/data/speech.dat")
acf(speech,250)
Example 1.25
CHAPTER 2
Example 2.1
Example 2.2
mort = ts(scan(“c:/data/cmort.dat"),start=1970,
frequency=52)
temp = ts(scan(“c:/data/temp.dat"), start=1970,
frequency=52)
part = ts(scan(“c:/data/part.dat"), start=1970,
frequency=52)
par(mfrow=c(3,1), mar=c(3,4,3,2)) # plot data - ?
par for info on mar()
plot(mort, main="Cardiovascular Mortality", xlab="",
ylab="")
plot(temp, main="Temperature", xlab="", ylab="")
plot(part, main="Particulates", xlab="", ylab="")
x11() # open another graphic
device
pairs(cbind(mort, temp, part)) # scatterplot matrix
temp = temp-mean(temp)
temp2 = temp^2
trend = time(mort) # time
fit = lm(mort~ trend + temp + temp2 + part,
na.action=NULL)
summary(fit) # regression results
anova(lm(mort~cbind(trend, temp, temp2, part))) # pretty
ANOVA table
num = length(mort) # sample size
AIC(fit)/num - log(2*pi) # AIC (corresponds
to def 2.1)
AIC(fit, k=log(num))/num - log(2*pi) # BIC (corresponds
to def 2.3)
(AICc = log(sum(resid(fit)^2)/num)+(num+5)/(num-5-2)) #
AICc (as in def 2.2)
Examples 2.3 and 2.4
Example 2.6
You can download a function called lag.plot2 that will do the second part of this example
over here.
Example 2.8
t = 1:500
x = 2*cos(2*pi*t/50 + .6*pi) + rnorm(500,0,5)
I = abs(fft(x)/sqrt(500))^2 # the periodogram
P = (4/500)*I # the scaled periodogram
f = 0:250/500
plot(f, P[1:251], type="l", xlab="frequency", ylab=" ")
abline(v=seq(0,.5,.02), lty="dotted")
Example 2.10
mort = ts(scan(“c:/data/cmort.dat"),start=1970,
frequency=52)
ma5 = filter(mort, sides=2, rep(1,5)/5)
ma53 = filter(mort, sides=2, rep(1,53)/53)
plot(mort, type="p", xlab="week", ylab="mortality")
lines(ma5)
lines(ma53)
Example 2.11
Example 2.12
Example 2.13
par(mfrow=c(2,1))
plot(mort,type="p", ylab="mortality", main="nearest
neighbor")
lines(supsmu(time(mort), mort, span=.5))
lines(supsmu(time(mort), mort, span=.01))
plot(mort, type="p", ylab="mortality", main="lowess")
lines(lowess(mort, f=.02))
lines(lowess(mort, f=2/3))
Example 2.14
Example 2.15
par(mfrow=c(2,1))
plot(temp, mort, main="lowess")
lines(lowess(temp,mort))
plot(temp, mort, main="smoothing splines")
lines(smooth.spline(temp,mort))
top
CHAPTER 3
Example 3.1
par(mfrow=c(2,1))
plot(arima.sim(list(order=c(1,0,0), ar=.9), n=100),
ylab="x",main=(expression(AR(1)~~~phi==+.9))) # ~
is a space and == is equal
plot(arima.sim(list(order=c(1,0,0), ar=-.9), n=100),
ylab="x",main=(expression(AR(1)~~~phi==-.9)))
Example 3.3
par(mfrow=c(2,1))
plot(arima.sim(list(order=c(0,0,1), ma=.5), n=100),
ylab="x",main=(expression(MA(1)~~~theta==+.5)))
plot(arima.sim(list(order=c(0,0,1), ma=-.5), n=100),
ylab="x",main=(expression(MA(1)~~~theta==-.5)))
Example 3.9
set.seed(5)
ar2 = arima.sim(list(order=c(2,0,0), ar=c(1.5,-.75)), n =
144)
t = (1:144)/12
plot(t, ar2, type="l", xlab="Time (one unit = 12 points)")
abline(v=0:12, lty="dotted")
Example 3.10
Example 3.14
Example 3.16
Example 3.26
Example 3.29
Example 3.33
Example 3.35
gnp96 = read.table(“c:/data/gnp96.dat")
gnp = ts(gnp96[,2], start=1947, frequency=4)
plot(gnp)
acf(gnp, 50)
gnpgr = diff(log(gnp)) # growth rate
plot.ts(gnpgr)
par(mfrow=c(2,1))
acf(gnpgr, 24)
pacf(gnpgr, 24)
# Fit ARIMA and view results:
(gnpgr.ar = arima(gnpgr, order = c(1, 0, 0))) # potential
problem here (see R Issue 1)
(gnpgr.ma = arima(gnpgr, order = c(0, 0, 2)))
ARMAtoMA(ar=.35, ma=0, 10) # prints psi-weights
Example 3.36
tsdiag(gnpgr.ma, gof.lag=20)
Example 3.37
varve = scan(“c:/data/varve.dat")
(varve.ma = arima(log(varve), order = c(0, 1, 1)))
tsdiag(varve.ma)
(varve.arma = arima(log(varve), order = c(1, 1, 1)))
tsdiag(varve.arma, gof.lag=20)
Example 3.39
Example 3.41
phi = c(rep(0,11),.8)
acf = ARMAacf(ar=phi, ma=-.5, 50)
pacf = ARMAacf(ar=phi, ma=-.5, 50, pacf=T)
par(mfrow=c(1,2))
plot(acf, type="h", xlab="lag")
abline(h=0)
plot(pacf, type="h", xlab="lag")
abline(h=0)
Example 3.43
prod=ts(scan(“c:/data/prod.dat"), start=1948, frequency=12)
par(mfrow=c(2,1)) # (P)ACF of data
acf(prod, 48)
pacf(prod, 48)
par(mfrow=c(2,1)) # (P)ACF of d1 data
acf(diff(prod), 48)
pacf(diff(prod), 48)
par(mfrow=c(2,1)) # (P)ACF of d1-d12 data
acf(diff(diff(prod),12), 48)
pacf(diff(diff(prod),12), 48)
### fit model (iii)
prod.fit3 = arima(prod, order=c(1,1,1),
seasonal=list(order=c(2,1,1), period=12))
prod.fit3 # to view the results
tsdiag(prod.fit3, gof.lag=48) # diagnostics
### forecasts for the final model
prod.pr = predict(prod.fit3, n.ahead=12)
U = prod.pr$pred + 2*prod.pr$se
L = prod.pr$pred - 2*prod.pr$se
ts.plot(prod,prodpr$pred, col=1:2, type="o",
ylim=c(105,175), xlim=c(1975,1980))
lines(U, col="blue", lty="dashed")
lines(L, col="blue", lty="dashed")
CHAPTER 4
Example 4.1
t = 1:100
x1 = 2*cos(2*pi*t*6/100) + 3*sin(2*pi*t*6/100)
x2 = 4*cos(2*pi*t*10/100) + 5*sin(2*pi*t*10/100)
x3 = 6*cos(2*pi*t*40/100) + 7*sin(2*pi*t*40/100)
x = x1 + x2 + x3
par(mfrow=c(2,2))
plot.ts(x1, ylim=c(-10,10), main =
expression(omega==6/100~~~A^2==13))
plot.ts(x2, ylim=c(-10,10), main =
expression(omega==10/100~~~A^2==41))
plot.ts(x3, ylim=c(-10,10), main =
expression(omega==40/100~~~A^2==85))
plot.ts(x, ylim=c(-16,16),main="sum")
Example 4.2
P = abs(2*fft(x)/100)^2
f = 0:50/100
plot(f, P[1:51], type="o", xlab="frequency",
ylab="periodogram")
Example 4.7
x = c(1,2,3,2,1)
c1 = cos(2*pi*1:5*1/5)
s1 = sin(2*pi*1:5*1/5)
c2 = cos(2*pi*1:5*2/5)
s2 = sin(2*pi*1:5*2/5)
omega1 = cbind(c1, s1)
omega2 = cbind(c2, s2)
anova(lm(x~omega1+omega2)) # ANOVA Table
abs(fft(x))^2/5 # the periodogram (as a
check)
Example 4.9
soi = scan(“c:/data/soi.dat")
rec = scan(“c:/data/recruit.dat")
par(mfrow=c(2,1))
soi.per = spec.pgram(soi, taper=0, log="no")
abline(v=1/12, lty="dotted")
abline(v=1/48, lty="dotted")
rec.per = spec.pgram(rec, taper=0, log="no")
abline(v=1/12, lty="dotted")
abline(v=1/48, lty="dotted")
soi.per$spec[40] # soi pgram at freq 1/12 = 40/480
soi.per$spec[10] # soi pgram at freq 1/48 = 10/480
# -- conf intervals -- # returned value:
U = qchisq(.025,2) # 0.05063562
L = qchisq(.975,2) # 7.377759
2*soi.per$spec[10]/L # 0.1747835
2*soi.per$spec[10]/U # 25.46648
2*soi.per$spec[40]/L # 3.162688
2*soi.per$spec[40]/U # 460.813
# -- replace soi with rec above to get recruit values
###########################################################
############################
###- Repeat example using "frequency=12" for monthly data
and note the differences -###
###- basically, "everything" is in multiples of Δ = 1/12
-###
###########################################################
############################
soi=ts(scan(“c:/data/soi.dat"), start=1950, frequency=12)
rec=ts(scan(“c:/data/recruit.dat"), start=1950,
frequency=12)
par(mfrow=c(2,1))
soi.per = spec.pgram(soi, taper=0, log="no")
abline(v=1, lty="dotted")
abline(v=1/4, lty="dotted")
rec.per = spec.pgram(rec, taper=0, log="no")
abline(v=1, lty="dotted")
abline(v=1/4, lty="dotted")
soi.per$spec[40] # soi pgram at freq 1/12 = 40/480
soi.per$spec[10] # soi pgram at freq 1/48 = 10/480
# -- conf intervals -- # returned value:
U = qchisq(.025,2) # 0.05063562
L = qchisq(.975,2) # 7.377759
2*soi.per$spec[10]/L # 0.01456530
2*soi.per$spec[10]/U # 2.122207
2*soi.per$spec[40]/L # 0.2635573
2*soi.per$spec[40]/U # 38.40108
Example 4.10
par(mfrow=c(2,1))
k = kernel("daniell",4)
soi.ave = spec.pgram(soi, k, taper=0, log="no")
abline(v=1/12, lty="dotted")
abline(v=2/12, lty="dotted")
abline(v=3/12, lty="dotted")
abline(v=1/48, lty="dotted")
# -- Repeat 5 lines above using rec in place of soi
soi.ave$bandwidth # reported bandwidth
soi.ave$bandwidth*sqrt(12) # Bw
Example 4.11
Example 4.12
par(mfrow=c(3,1))
spectrum(soi, spans=c(7,7), taper=0, main="No Taper")
abline(v=1/12,lty="dashed")
abline(v=1/48,lty="dashed")
spectrum(soi, spans=c(7,7), main="10% Taper")
abline(v=1/12,lty="dashed")
abline(v=1/48,lty="dashed")
spectrum(soi, spans=c(7,7), taper=.5, main="50% Taper")
abline(v=1/12,lty="dashed")
abline(v=1/48,lty="dashed")
Example 4.13
x = matrix(scan(“c:/data/eq5exp6.dat"), ncol=2)
eqP = x[1:1024, 1]; eqS = x[1025:2048, 1]
exP = x[1:1024, 2]; exS = x[1025:2048, 2]
par(mfrow=c(2,2))
eqPs=spectrum(eqP, spans=c(21,21), log="no",
xlim=c(0,.25), ylim=c(0,.04))
eqSs=spectrum(eqS, spans=c(21,21), log="no",
xlim=c(0,.25), ylim=c(0,.4))
exPs=spectrum(exP, spans=c(21,21), log="no",
xlim=c(0,.25), ylim=c(0,.04))
exSs=spectrum(exS, spans=c(21,21), log="no",
xlim=c(0,.25), ylim=c(0,.4))
exSs$df
Example 4.16
x = ts(cbind(soi,rec))
s = spec.pgram(x, kernel("daniell",9), taper=0)
s$df # df = 35.8625
f = qf(.999, 2, s$df-2) # f = 8.529792
c = f/(18+f) # c = 0.3188779
plot(s, plot.type = "coh", ci.lty = 2)
abline(h = c)
Example 4.17
par(mfrow=c(3,1))
plot.ts(soi) # the data
plot.ts(diff(soi)) # first difference
k = kernel("modified.daniell", 6) #-- 12 month filter
soif = kernapply(soi,k)
plot.ts(soif)
x11() # open new graphics device
spectrum(soif, spans=9, log="no") #-- spectral analysis
abline(v=1/52, lty="dotted")
windows()
w = seq(0,.5, length=1000) #-- frequency response
FR = abs(1-exp(2i*pi*w))^2
plot(w, FR, type="l")
Example 4.19
Example 4.20
Example 4.21
Example 4.22
CHAPTER 5
Example 5.1
Example 5.3
Example 5.4
library(nlme)
mort = scan(“c:/data/cmort.dat")
temp = scan(“c:/data/temp.dat")
part = scan(“c:/data/part.dat")
temp = temp - mean(temp)
temp2 = temp^2
trend = 1:length(mort)
# -- lm() fit in Example 2.2 indicates AR(2) for the
residuals
fit.gls = gls(mort~trend + temp + temp2 + part,
correlation=corARMA(p=2), method="ML")
# -- the results (abbreviated)
summary(fit.gls)
Correlation Structure: ARMA(2,0)
Parameter estimate(s):
Phi1 Phi2
0.3848530 0.4326282
Coefficients: Value Std.Error t-value p-value
(Intercept) 87.63747 2.7327956 32.06880 0.0000
trend -0.02915 0.0081705 -3.56781 0.0004
temp -0.01881 0.0430264 -0.43715 0.6622
temp2 0.01542 0.0020289 7.60244 0.0000
part 0.15437 0.0248071 6.22297 0.0000
Residual standard error: 7.699336
Degrees of freedom: 508 total; 503 residual
# -- check noise for whiteness (* see note below)
w = filter(residuals(fit.gls), filter=c(1,-.3848530,
-.4326282), sides=1)
w = w[3:508] # first two are NA
Box.test(w, 20, type="Ljung") # Ljung-Box-Pierce
Statistic
X-squared = 26.793, df = 20, p-value = 0.1412
pchisq(26.793, 18, lower=F) # the p-value (they are
resids from an ar2 fit)
0.08296072
* The errors from the gls fit, say e(t), are estimated to be of the form e(t) = .3848530 e(t-1) +
.4326282 e(t-2) + w(t). The filter statement gets at the w(t), which should be white.
top