Simple Time Series Analysis
First, I will show a basic example of time series analysis in R using the built-in AirPassengers dataset.
The dataset contains monthly totals of international airline passengers from 1949 to 1960.
# Load necessary libraries library(forecast) # Load the AirPassengers dataset data("AirPassengers") # Convert the dataset to a time series object passengers_ts <- ts(AirPassengers, frequency = 12, start = c(1949, 1)) # Plot the time series data plot(passengers_ts, main = "International Airline Passengers", ylab = "Number of Passengers", xlab = "Year") # Decompose the time series into trend, seasonal, and residual components decomposed_ts <- decompose(passengers_ts) # Plot the decomposed components plot(decomposed_ts)
Let me explain each and every line of the above code.
- Start by loading the forecast library, which contains functions for time series analysis.
- Load the AirPassengers dataset, which is included in R. This dataset contains the monthly totals of international airline passengers.
- Convert the dataset to a time series object using the ts() function. We specify that the data has a frequency of 12 (monthly data) and starts in January 1949.
- Plot the time series data using the plot() function. This gives us a visual representation of the number of international airline passengers over time.
- Decompose the time series into its trend, seasonal, and residual components using the decompose() function. This helps us understand the underlying patterns in the data.
- Plot the decomposed components using the plot() function. This allows us to visualize the trend, seasonal, and residual components separately.
This is a basic example to get you started with time series analysis in R. Depending on your specific analysis goals, you may want to explore further techniques such as forecasting or modeling.
Time Series Analysis using ARIMA
Now I’ll take you into time series analysis with an example that includes forecasting using the ARIMA (AutoRegressive Integrated Moving Average) model.
Let’s use the same AirPassengers dataset for this example.
# Load necessary libraries library(forecast) # Load the AirPassengers dataset data("AirPassengers") # Convert the dataset to a time series object passengers_ts <- ts(AirPassengers, frequency = 12, start = c(1949, 1)) # Plot the time series data plot(passengers_ts, main = "International Airline Passengers", ylab = "Number of Passengers", xlab = "Year") # Decompose the time series into trend, seasonal, and residual components decomposed_ts <- decompose(passengers_ts) # Plot the decomposed components plot(decomposed_ts) # Fit an ARIMA model to the data arima_model <- auto.arima(passengers_ts) # Print the model summary print(summary(arima_model)) # Forecast future values using the ARIMA model forecast_values <- forecast(arima_model, h = 24) # Forecasting the next 24 months # Plot the forecasted values plot(forecast_values, main = "Forecasted International Airline Passengers", ylab = "Number of Passengers", xlab = "Year")
Again, I will explain the steps in detail.
- Load the forecast library, which contains functions for time series analysis.
- Load the AirPassengers dataset, which contains the monthly totals of international airline passengers.
- Convert the dataset to a time series object using the ts() function.
- Plot the time series data to visualize the number of international airline passengers over time.
- Decompose the time series into its trend, seasonal, and residual components using the decompose() function. This helps us understand the underlying patterns in the data.
- Plot the decomposed components to visualize the trend, seasonal, and residual components separately.
- Use the auto.arima() function to automatically fit an ARIMA model to the time series data.
- Print a summary of the ARIMA model to see the details of the fitted model.
- Use the forecast() function to forecast future values of the time series using the fitted ARIMA model. Here, we forecast the next 24 months.
- Plot the forecasted values to visualize the predicted trend of international airline passengers for the next 24 months.
In simple terms, this example shows how to fit an ARIMA model to time series data, make forecasts, and visualize the results.