attach(blowfly)
names(blowfly)

flies<-ts(flies)

tsplot(flies)

f1<-flies[-1]

length(f1)

length(flies)

par(mfrow=c(2,2))

plot(flies[-361],f1)

f2<-flies[-c(1,2)]
f3<-flies[-c(1,2,3)]
f4<-flies[-c(1,2,3,4)] 

plot(flies[-c(361,360)],f2)
plot(flies[-c(361,360,359)],f3)
plot(flies[-c(361,360,359,358)],f4)

f7<-flies[-c(1:7)]
f8<-flies[-c(1:8)]
f9<-flies[-c(1:9)]
f10<-flies[-c(1:10)]
plot(flies[-c(355:361)],f7)
plot(flies[-c(354:361)],f8)
plot(flies[-c(353:361)],f9)
plot(flies[-c(352:361)],f10)

acf(flies)

acf(flies,type="p")

second<-flies[201:361]

summary(lm(second~I(1:length(second))))

detrended<-second - predict(lm(second~I(1:length(second))))
tsplot(detrended)

acf(detrended)
acf(detrended, type=p)

attach(soaysheep)
names(soaysheep)

tsplot(Population)

acf(Population)
acf(Population,type="p")

delta<-diff(log(Population))
length(delta)

length(Population)

numbers<-Population[-45]

plot(log(numbers),delta)

abline(0,lty=2)

xv<-seq(6.4,7.6,0.01)

lines(xv,predict(loess(delta~log(numbers)),data.frame(numbers=exp(xv))))

attach(Lynx)
names(Lynx)

tsplot(Numbers,ylab="Lynx numbers")
year<-1:length(Numbers)

summary(lm(Numbers~year))

acf(Numbers)

acf(Numbers,type="p")
 
ma3<-function (x) {
	y<-numeric(length(x)-1)
	for (i in 2:length(x)-1) {
		y[i]<-(x[i-1]+x[i]+x[i+1])/3
	}
y    }

attach(temp)
tm<-ma3(temp)
plot(temp)
lines(tm)

attach(SilwoodWeather)
names(SilwoodWeather)

plot(upper,type="l")

index<-1:4748
4748/13

time<-index/365.2308

model<-lm(upper~sin(time*2*pi)+cos(time*2*pi))

plot(time, upper, pch=".")
lines(time, predict(model))

summary(model)

plot(model$resid,pch=".")

par(mfrow=c(1,2))
acf(model$resid)
acf(model$resid,type=p)

temp<-ts(as.vector(tapply(upper,list(month,yr),mean)))
acf(temp)

ytemp<-ts(as.vector(tapply(upper,yr,mean)))
acf(ytemp)
start=c(1987,1)frequency=365high<-rts(upper,start=c(1987,1),frequency=365)
ts.plot(high)

up<-ts(upper,start=c(1987,1),frequency=365.25)

high<-stl(up,"period")

plot(high)

highmonths<-as.vector(tapply(upper,list(month,yr),mean))

highs<-rts(highmonths,start=c(1987,1),frequency=12)

highdec<-stl(highs,"periodic")

ts.plot(highdec$rem,highdec$sea)

length(highmonths)

time<-1:156
plot(time,highmonths)
model<-lm(highmonths~time)
abline(model)
summary(model)

spectrum(Numbers)

attach(twoseries)
names(twoseries)

tsplot(x,y)

par(mfrow=c(1,2))
acf(x,type="p") 
acf(y,type="p")

par(mfrow=c(1,1))
acf(cbind(x,y))
acf(cbind(x,y),type="p")

acf(cbind(x,y),type="cov")

plot(diff(x),diff(y))

attach(interaction)
names(interaction)

cd<-diff(log(cinnabar))
rd<-diff(log(ragwort))

data<-cbind(cd,rd)

ar(data)

plot(cinnabar[-19],cd)

tsplot(cinnabar,ragwort)

mean(ragwort)

mean(cinnabar)

tsplot(log(cinnabar),log(ragwort))

par(mfrow=c(2,1))
tsplot(cinnabar)
tsplot(ragwort)

par(mfrow=c(1,1))
par(mar=c(5,4,4,5)+0.1)

tsplot(cinnabar,ylab="cinnabar")

par(new=T,xaxs="d")

tsplot(ragwort,axes=F,ylab="",lty=2)

axis(side=4)
mtext(side=4,line=3.8, "ragwort")

attach(repmeasures)
names(repmeasures)

summary(aov(height~seed,subset=(time==4)))

tapply(height,list(time,seed),mean)

summary(aov(height~seed*time))

summary(lm(height~seed+time))

rep<-factor(rep)
tt<-factor(time)
summary(aov(height~seed+Error(rep/seed/tt)))

structure<-groupedData(height~time|rep/seed)

plot(structure)

plot(structure,outer=~seed)

lmList(structure)

summary(lme(fixed = height~seed,random = ~time|rep/seed))

coef(lme(fixed = height~seed,random = ~time|rep/seed))

Y<-rnorm(250,0,2)
par(mfrow=c(1,2))
tsplot(Y)
acf(Y)

Z<-rnorm(250,0,2)
Y<-numeric(250)
Y[1]<-Z[1]
for (i in 2:250) Y[i]<-  - 0.5*Y[i-1]+Z[i]
tsplot(Y)
acf(Y)

Z<-rnorm(250,0,2)
Y[1]<-Z[1]
for (i in 2:250) Y[i]<- 0.5*Y[i-1]+Z[i]
tsplot(Y)
acf(Y)

Z<-rnorm(250,0,2)
Y[1]<-Z[1]
for (i in 2:250) Y[i]<- Y[i-1]+Z[i]
tsplot(Y)
acf(Y)

attach(Lynx)
names(Lynx)

tsplot(Numbers,ylab="Lynx numbers")

model10<-arima.mle(Numbers,model=list(order=c(1,0,0)))
model21<-arima.mle(Numbers,model=list(order=c(2,1,0)))



