jlivsey / UB-sping24-time-series

3 stars 1 forks source link

Pranitha Reddy Samala #35

Closed spranithareddy closed 4 months ago

spranithareddy commented 4 months ago
library(fredr)
  library(forecast)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
# Set your FRED API key
fredr_set_key("0f0e54b3a924da49f1f99bf3c764ecdd")

# Retrieve the data for the specified series
dat <- fredr(series_id = "MSRSFL445")
dat <- na.omit(dat)

# Selecting time and value columns
dat <- dplyr::select(dat, time = date, value)

# Plot the time series
ts_plot(dat)
#> Error in ts_plot(dat): could not find function "ts_plot"

# Convert data to time series object
x <- ts(dat$value)

# Plot the time series
plot(x)


# Plot the differenced time series
plot(diff(x))


# Display ACF and PACF of differenced time series
tsdisplay(diff(x))


# Display ACF and PACF of log differenced time series
tsdisplay(diff(log(x)))
#> Warning in log(x): NaNs produced


# Fit a seasonal ARIMA model
fit <- Arima(dat$value, order = c(0, 1, 4), seasonal = list(order = c(0, 1, 1), period = 12))

# Print model summary
summary(fit)
#> Series: dat$value 
#> ARIMA(0,1,4)(0,1,1)[12] 
#> 
#> Coefficients:
#>           ma1     ma2      ma3     ma4     sma1
#>       -0.2315  0.0511  -0.2840  0.1028  -1.0000
#> s.e.   0.1560  0.1448   0.1918  0.1867   0.2096
#> 
#> sigma^2 = 38.95:  log likelihood = -153.09
#> AIC=318.19   AICc=320.4   BIC=329.03
#> 
#> Training set error measures:
#>                     ME     RMSE      MAE       MPE   MAPE      MASE        ACF1
#> Training set -0.387603 5.183105 2.923656 -3.403652 41.722 0.8177056 -0.07464527

# Plot original data
plot(dat$time, dat$value, type = "l", xlab = "Time", ylab = "Value", main = "Original Data")

# Overlay fitted values
lines(dat$time, fitted(fit), col = "red")

# Add legend
legend("topright", legend = c("Original Data", "Fitted Values"), col = c("black", "red"), lty = 1)

Created on 2024-02-14 with reprex v2.0.2