Zeitreihenanalyse - Übung 4

From StatWiki
Jump to: navigation, search

Average using Timeseries +

> pdf(rpdf)
> gdpa=readdataSK("GDPA.csv", "csv")
> gdpa=data.frame(year=as.numeric(substr(gdpa$DATE,1,4)), gdp=gdpa$VALUE)
> gdpa=subset(gdpa, year>=1946)
> GDP.ts=ts(gdpa$gdp, start=1946)
> GDP.ls <- log(GDP.ts)
>
> S1 <- (lag(GDP.ls,k=-1)+GDP.ls+lag(GDP.ls,k=1))/3
> plot(GDP.ls,type="p",xlab="Year",ylab="log(GDP)")
> lines(S1,lwd=2)

Using loops

> pdf(rpdf)
> gdpa=readdataSK("GDPA.csv", "csv")
> gdpa=data.frame(year=as.numeric(substr(gdpa$DATE,1,4)), gdp=gdpa$VALUE)
> gdpa=subset(gdpa, year>=1946)
> GDP.ts=ts(gdpa$gdp, start=1946)
> GDP.ls <- log(GDP.ts)
>
> S7 <- GDP.ls
> for (i in 1:7) S7 <- S7+lag(GDP.ls,k=-i)+lag(GDP.ls,k=i)
>
> S7 <- S7/15
> plot(GDP.ls,type="p",xlab="Year",ylab="log(GDP)")
> lines(S7,lwd=2)

Using functions

> pdf(rpdf)
> gdpa=readdataSK("GDPA.csv", "csv")
> gdpa=data.frame(year=as.numeric(substr(gdpa$DATE,1,4)), gdp=gdpa$VALUE)
> gdpa=subset(gdpa, year>=1946)
> GDP.ts=ts(gdpa$gdp, start=1946)
> GDP.ls <- log(GDP.ts)
>
> smooth <- function(y,k,col,eps)
+ {
+ c <- 2*k+1; y.sm <- y
+ for (i in 1:k) y.sm <- y.sm+lag(y,k=-i)+lag(y,k=i)
+ y.sm <- y.sm/c
+ lines(y.sm+eps,lwd=2,col=col)
+ }
>
> plot(GDP.ls,type="p",xlab="Year",ylab="")
> smooth(GDP.ls,k=1,col=1,0)
> smooth(GDP.ls,k=7,col=2,-.5)
> smooth(GDP.ls,k=10,col=3,.5)
>
> legend(1955,9,c("k=1","k=7","k=10"), col=1:4, lty=1)