In [9]:
# Load the packages you intend to work with every time you start a new session
library(tseries)
library(TSA)
library(forecast)
In [10]:
### Object of class time series (ts)
args(ts)
In [11]:
### Load data
# climatic index for southern Europe
NAO <- read.table("http://www.escet.urjc.es/biodiversos/R/NAO.txt", header=T, sep="\t")
str(NAO)
In [12]:
parasit <- read.table("http://www.escet.urjc.es/biodiversos/R/parasitismo.txt", header=T, sep="\t")
In [13]:
head(parasit)
In [14]:
colnames(parasit) <- c("N_eggs", "Born", "Parasit", "Year")
In [15]:
head(parasit)
In [16]:
str(parasit) # 3 variables that change over time
In [17]:
boxplot(Parasit~Year, parasit)
In [18]:
# applys func 'mean' on the first the cols, group them by year
# this will reduce the data set from 4000+ rows to 30+ rows
aggregate(parasit[,1:3], list(parasit$Year), mean)
In [19]:
parasit.mean <- aggregate(parasit[,1:3], list(parasit$Year), mean)
In [20]:
str(parasit)
In [21]:
str(parasit.mean)
In [22]:
parasit.ts <- ts(data=parasit.mean[,2:4], start=min(parasit.mean$Group.1))
In [23]:
str(parasit.ts)
In [24]:
# plot all 3 timeseries stored in parasit.ts
plot(parasit.ts, type = "b", pch=19)
In [27]:
x <- rnorm(10)
x.ts <-ts(x, start=2000, frequency=1/2) # time series: 2000, 2002, 2004, ...
plot(x.ts, type="b")
In [26]:
head(NAO)
In [27]:
# $ is used to access a column by name
NAO$NAO[1:5] # get the first 5 values from column NAO
In [28]:
# get min / max value from a column
min(NAO$year)
max(NAO$year)
In [29]:
### Coerce data to a time series
NAO.ts <- ts(data=NAO$NAO, start=min(NAO$year), end=max(NAO$year))
In [30]:
class(NAO.ts)
In [31]:
str(NAO.ts)
In [32]:
NAO.ts
In [33]:
# show usage examples for ts (time series)
# example(ts)
In [34]:
### Time series for seasonal data (one data point for each quarter)
ts(1:10, frequency = 4, start = c(1959, 2))
In [35]:
### Time series for seasonal data (one data point per month)
ts(1:10, frequency = 12, start = c(1959, 2))
In [36]:
# example: rnorm(5) generates 5 random deviates from a standard normal distribution.
rnorm(5)
In [37]:
ts(rnorm(30), frequency = 12, start = c(12, 2))
In [38]:
# matrix w/ 4 rows and 3 cols w/ 12 random vals
# from a std normal distribution
matrix(rnorm(12), 4, 3)
In [39]:
### Multivariate time series data set
z <- ts(matrix(rnorm(30), 10, 3), start=c(1961, 1), frequency=12)
In [40]:
class(z)
In [41]:
z
In [42]:
zp <- z[,2] # access the time series stored in col 2
In [43]:
zp
In [44]:
class(zp)
In [45]:
# "plot 'NAO$NAO' as a function of 'NAO$year'"
plot(NAO~year, data=NAO)
In [46]:
# add unconnected lines (type b) between data points
# and use a different pch (plotting character)
plot(NAO~year, data=NAO, type="b", pch=19)
In [47]:
plot(NAO~year, data=NAO, type="b", pch=19, ylab="NAO index", xlab="")
In [48]:
### same plot with abline (horizontal line)
plot(NAO~year, data=NAO, type="b", pch=19, ylab="NAO index", xlab="")
abline(h=0)
In [49]:
### Time series plot
x11()
plot(NAO.ts, type="o", ylab="NAO index", xlab="")
abline(h=0)
### Multivariate time series plot
parasit <- read.table("http://www.escet.urjc.es/biodiversos/R/parasitismo.txt", header=T, sep="\t")
parasit.mean <- aggregate(parasit[,1:3],list(parasit$Yr),mean)
str(parasit.mean)
parasit.ts <- ts(parasit.mean[,2:4], start=min(parasit.mean$Group.1))
plot(parasit.ts, type="b", pch=19, xlab="", main="")
In [ ]: