Time Series Analysis

Let’s analyze a simple time series dataset using the forecast package.

Step 1: Install and Load Forecast

If you don’t have forecast installed, you can install it:

rCopy codeinstall.packages("forecast")

Then, load the library:

rCopy codelibrary(forecast)

Step 2: Create a Time Series Dataset

We’ll create a sample time series dataset:

rCopy code# Generate a time series dataset
set.seed(101)
ts_data <- ts(rnorm(120, mean = 10, sd = 2), frequency = 12, start = c(2020, 1))
plot(ts_data, main = "Sample Time Series Data", ylab = "Value", xlab = "Time")

Step 3: Decompose the Time Series

rCopy code# Decompose the time series
decomposed <- decompose(ts_data)
plot(decomposed)

Step 4: Forecasting

rCopy code# Fit an ARIMA model and forecast
fit <- auto.arima(ts_data)
forecasted_values <- forecast(fit, h = 12)

# Plot the forecast
plot(forecasted_values, main = "Forecast for Next 12 Months")

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *