In the following sections we introduce basic operations for time series analysis. We disuses the following topics:
As mentioned in the previous section there exist many different R-packages to work with time series. Please be aware that basic operations on time series are implemented in most of them. Hence, it is very important to be aware of the object class and respectively, the data representation. This representation dictates which functions will be available for loading, processing, analyzing, printing, and plotting the time series data.
For the purpose of demonstration we load the monthly
(ts_FUB_monthly
), daily (ts_FUB_daily
) and
hourly (ts_FUB_hourly
) time series data for the weather
station Berlin-Dahlem (FU) into R. We can do that by using the
load()
function. Check out the previous section on data
sets used to remind yourself how we processed the data.
library(xts)
load(url("https://userpage.fu-berlin.de/soga/data/r-data/DWD_FUB.RData"))
First, we check to object classes for the three data sets:
class(ts_FUB_monthly)
## [1] "xts" "zoo"
str(ts_FUB_monthly)
## An xts object on 1719-01-01 / 2021-12-01 containing:
## Data: double [3636, 1]
## Index: Date [3636] (TZ: "UTC")
class(ts_FUB_daily)
## [1] "xts" "zoo"
str(ts_FUB_daily)
## An xts object on 1950-01-01 / 2021-12-31 containing:
## Data: double [26298, 2]
## Columns: Temp, Rain
## Index: Date [26298] (TZ: "UTC")
class(ts_FUB_hourly)
## [1] "xts" "zoo"
str(ts_FUB_hourly)
## An xts object on 2002-01-28 11:00:00 / 2021-12-31 23:00:00 containing:
## Data: double [174023, 1]
## Columns: Rain
## Index: POSIXct,POSIXt [174023] (TZ: "UTC")
The data sets are of class xts
and zoo
.
Note that xts
is just an extension of the zoo
class. Visit the vignettes for these both packages to refresh your
knowledge on these type of data representation. Type
vignette('zoo')
and vignette('xts')
into your
console to open the vignettes.
In order to extract the data vector from the xts
object,
in our case the ts_FUB_daily
object, we apply the
coredata()
function on it.
x <- coredata(ts_FUB_daily)
head(x, 15)
## Temp Rain
## [1,] -3.2 2.2
## [2,] 1.0 12.6
## [3,] 2.8 0.5
## [4,] -0.1 0.5
## [5,] -2.8 10.3
## [6,] 2.6 7.2
## [7,] 5.7 0.4
## [8,] 7.0 0.0
## [9,] 5.8 3.7
## [10,] -2.4 4.5
## [11,] -7.2 0.6
## [12,] 0.6 7.1
## [13,] 2.5 0.0
## [14,] 5.4 0.0
## [15,] 5.8 3.8
To get the time index we apply the index()
function on
the xts
object.
t <- index(ts_FUB_daily)
head(t, 15)
## [1] "1950-01-01" "1950-01-02" "1950-01-03" "1950-01-04" "1950-01-05"
## [6] "1950-01-06" "1950-01-07" "1950-01-08" "1950-01-09" "1950-01-10"
## [11] "1950-01-11" "1950-01-12" "1950-01-13" "1950-01-14" "1950-01-15"
Now let us plot the monthly data with the plot.ts()
function.
plot.ts(ts_FUB_monthly)
We could as well have used the autoplot()
function from
the ggfortify
package (alternatively from the
ggplot2
package). Let us now visualize the
ts_FUB_monthly
object using the autoplot()
function.
library(ggfortify)
autoplot(ts_FUB_monthly)
Exercise: Plot the daily and hourly data sets using the
autoplot()
function
## Your code here...
autoplot(ts_FUB_daily)
## Your code here...
autoplot(ts_FUB_hourly)
The xts
object ts_FUB_daily
stores two time
series: one for temperatures (Temp
) and one for rainfall
(Rain
). The autoplot()
function can deal with
more than one time series stored in an xts
object and, by
default, plots all variables in the data set in one graph.
ts
classFor the sake of this tutorial we transform the monthly data set
(ts_FUB_monthly
) into a ts
object. The
ts
object class is less flexible compared to the
zoo
or xts
object classes, however, this
object class is widely used and thus, we believe it is important to know
how to interact with that particular object class. Therefore, we apply
the ts()
function, which converts a vector into a time
series.
The ts
function needs at least these arguments:
x
: a vector with datastart
: starting date of the seriesend
: ending date of the seriesfrequency
: number of observations in the series per
unit of timeIn order to extract the data vector from the xts
object,
we again apply the coredata()
function.
x_monthly <- coredata(ts_FUB_monthly)
head(x_monthly)
## [,1]
## [1,] 2.8
## [2,] 1.1
## [3,] 5.2
## [4,] 9.0
## [5,] 15.1
## [6,] 19.0
Further, to extract the start
argument we apply the
index()
function on of the ts_FUB_monthly
object and combine it with the year()
and
month()
function. The start
argument is either
a single number or a vector of two integers, which specify a natural
time unit.
library(lubridate)
start <- c(
year(index(ts_FUB_monthly)[01]),
month(index(ts_FUB_monthly)[01])
)
start
## [1] 1719 1
As we are dealing with monthly data we set the argument
frequency = 12
.
Putting it all together gives
ts_FUB_monthly <- ts(x_monthly,
start = start,
frequency = 12
)
class(ts_FUB_monthly)
## [1] "ts"
str(ts_FUB_monthly)
## Time-Series [1:3636, 1] from 1719 to 2022: 2.8 1.1 5.2 9 15.1 19 21.4 18.8 13.9 9 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr "Series 1"
The ts_FUB_monthly
object contains a time series of
monthly mean temperatures from January 1719 to December 2021.
Let us plot the ts
object with the generic
plot()
function.
plot(ts_FUB_monthly)
As before, we could as well have used the autoplot()
function.
Citation
The E-Learning project SOGA-R was developed at the Department of Earth Sciences by Kai Hartmann, Joachim Krois and Annette Rudolph. You can reach us via mail by soga[at]zedat.fu-berlin.de.
Please cite as follow: Hartmann, K., Krois, J., Rudolph, A. (2023): Statistics and Geodata Analysis using R (SOGA-R). Department of Earth Sciences, Freie Universitaet Berlin.