Estimate and Plot the Partial Autocorrelation Function (PACF) in R

Your goal

You need to estimate and plot the partial autocorrelation function (PACF) for time series data.

Step-by-step tutorial

Use the pacf function from the stats package to estimate and plot the partial autocorrelation function (PACF) in R.

First, we can load some hygrometer data into a data frame, and plot it.

> hygrometer.df <- read.csv('hygrometer.csv')
> head(hygrometer.df)
       Interval Humidity.Average Temperature.Average
1 11/12/20 1:00         55.50117            21.93301
2 11/12/20 1:12         57.07868            19.70803
3 11/12/20 1:24         56.20748            19.85207
4 11/12/20 1:36         56.12080            19.87645
5 11/12/20 1:48         57.92270            19.32857
6 11/12/20 2:00         57.70622            19.19605
> temp <- hygrometer.df[, 'Temperature.Average']
> plot(temp, type='l')
Temperature time series

This time series has an obvious anomalous spike around index 100. This happened when I put the hygrometer in sleep mode by mistake. So let's subset the time series before we continue. The anomaly ends at index 109, so we'll remove elements 1-109 from the vector:

> temp.clean <- temp[-(1:109)]
> plot(temp.clean, type='l')
Cleaned-up temperature time series

Now use the pacf to estimate the PACF and generate a plot:

> pacf(temp.clean)
PACF of the temperature time series

To see more lags, use the lag.max parameter:

> pacf(temp.clean, lag.max=6*24)
PACF of the temperature time series, more lags