# Chapter 4 Basic time series functions in R

This chapter introduces you to some of the basic functions in R for plotting and analyzing univariate time series data. Many of the things you learn here will be relevant when we start examining multivariate time series as well. We will begin with the creation and plotting of time series objects in R, and then moves on to decomposition, differencing, and correlation (e.g., ACF, PACF) before ending with fitting and simulation of ARMA models.

A script with all the R code in the chapter can be downloaded here. The Rmd for this chapter can be downloaded here.

### Data and packages

This chapter uses the stats package, which is often loaded by default when you start R, the MARSS package and the forecast package. The problems use a dataset in the datasets package. After installing the packages, if needed, load:

library(stats)
library(forecast)
library(datasets)

The chapter uses data sets which are in the atsalibrary package. If needed, install using the devtools package.

library(devtools)
# Windows users will likely need to set this
# Sys.setenv('R_REMOTES_NO_ERRORS_FROM_WARNINGS' = 'true')
devtools::install_github("nwfsc-timeseries/atsalibrary")

The main one is a time series of the atmospheric concentration of CO$$_2$$ collected at the Mauna Loa Observatory in Hawai’i (MLCO2). The second is Northern Hemisphere land and ocean temperature anomalies from NOAA. (NHTemp). The problems use a data set on hourly phytoplankton counts (hourlyphyto). Use ?MLCO2, ?NHTemp and ?hourlyphyto for information on these datasets.

data(NHTemp, package = "atsalibrary")
phyto_dat <- hourlyphyto