R: Saving Lists with Renaming and Loading into a List

This post shows a streamlined list handling by saving lists with renaming and then loading them into a list for ongoing analysis.



Saving and loading forecast results of each model using a List



Let's assume that we already performed forecasting using two models (DNS and AFNS models), for example, with the following specifications.

  • 2 out-of-sample periods (2023:01, 2023:02)
  • 3 forecast horizons (3m, 6m, 12m)
  • 4 maturities (1Y, 3Y, 5Y, 10Y)

The specification is overly restrictive and therefore unreasonable. For instance, typical out-of-sample periods often extend over long durations, frequently exceeding 10 or 20 years. Thus, this example is provided solely for illustrative purposes.


Saving Lists


In this example, randomly generated forecast results are saved as list_fcst_yield, and we assign each model name using the assign() function as follows.

  • fcst_yield_DNS
  • fcst_yield_AFNS

rm(list = ls()) 
set.seed(1# fix random seed
setwd("your working directory")
 
#------------------------------------
# Save each model's forecasts
#------------------------------------
 
# 1) DNS model 
# Perform forecasting exercises
list_fcst_yield <- list()
list_fcst_yield[[1]] <- matrix(runif(3*4),3,4)
list_fcst_yield[[2]] <- matrix(runif(3*4),3,4)
 
# Save it as fcst_DNS.RData
str_model <- "DNS"
new_list_name <- paste0("fcst_", str_model)
assign(new_list_name, list_fcst_yield)
save(list = new_list_name, 
     file = paste0(new_list_name,".RData"))
new_list_name
 
 
# 2) AFNS model
# Perform forecasting exercises
list_fcst_yield <- list()
list_fcst_yield[[1]] <- matrix(runif(3*4),3,4)
list_fcst_yield[[2]] <- matrix(runif(3*4),3,4)
 
# Save it as fcst_AFNS.RData
str_model <- "AFNS"
new_list_name <- paste0("fcst_", str_model)
assign(new_list_name, list_fcst_yield)
save(list = new_list_name, 
     file = paste0(new_list_name,".RData"))
new_list_name
 
cs



Loading Lists


When loading saved lists with the .RData extension, there are two methods.

The first method is to load each file separately.

#------------------------------------
# Load each file separately
#------------------------------------
rm(list = ls()) # clear for test
 
load("fcst_DNS.RData")
load("fcst_AFNS.RData")
 
fcst_DNS
fcst_AFNS
 
cs


> fcst_DNS
[[1]]
          [,1]      [,2]       [,3]      [,4]
[1,] 0.7323137 0.8612095 0.07067905 0.5186343
[2,] 0.6927316 0.4380971 0.09946616 0.6620051
[3,] 0.4776196 0.2447973 0.31627171 0.4068302
 
[[2]]
          [,1]      [,2]       [,3]      [,4]
[1,] 0.9128759 0.3323947 0.47854525 0.8753213
[2,] 0.2936034 0.6508705 0.76631067 0.3390729
[3,] 0.4590657 0.2580168 0.08424691 0.8394404
 
> fcst_AFNS
[[1]]
          [,1]      [,2]      [,3]      [,4]
[1,] 0.3466835 0.8921983 0.7773207 0.7125147
[2,] 0.3337749 0.8643395 0.9606180 0.3999944
[3,] 0.4763512 0.3899895 0.4346595 0.3253522
 
[[2]]
          [,1]      [,2]       [,3]      [,4]
[1,] 0.7570871 0.1216919 0.23962942 0.8762692
[2,] 0.2026923 0.2454885 0.05893438 0.7789147
[3,] 0.7111212 0.1433044 0.64228826 0.7973088
 
cs


The second step is to load all files and insert them into one list. This facilitates ongoing analysis by using iteration to refer to each model's forecast result.

#------------------------------------
# Load all files at once
#------------------------------------
rm(list = ls()) # clear for test
 
# read all RData files
fcst_model <- list.files(getwd(), pattern = "\\.RData$"
                         full.names = FALSE)
 
# Remove the .RData extension
fcst_model_name <- sub("\\.RData$""", fcst_model)
fcst_model_name
 
# Create an empty list to store the loaded objects
list_fcst_model <- list()
 
for(i in 1:length(fcst_model_name)) {
    # Load the object into the global environment
    load(paste0(fcst_model_name[i], ".RData"))
    
    # Insert the loaded object into the list
    list_fcst_model[[i]] <- get(fcst_model_name[i])   
}
 
names(list_fcst_model) <- fcst_model_name
list_fcst_model
 
cs


> list_fcst_model
$fcst_AFNS
$fcst_AFNS[[1]]
          [,1]      [,2]      [,3]      [,4]
[1,] 0.3466835 0.8921983 0.7773207 0.7125147
[2,] 0.3337749 0.8643395 0.9606180 0.3999944
[3,] 0.4763512 0.3899895 0.4346595 0.3253522
 
$fcst_AFNS[[2]]
          [,1]      [,2]       [,3]      [,4]
[1,] 0.7570871 0.1216919 0.23962942 0.8762692
[2,] 0.2026923 0.2454885 0.05893438 0.7789147
[3,] 0.7111212 0.1433044 0.64228826 0.7973088
 
 
$fcst_DNS
$fcst_DNS[[1]]
          [,1]      [,2]       [,3]      [,4]
[1,] 0.7323137 0.8612095 0.07067905 0.5186343
[2,] 0.6927316 0.4380971 0.09946616 0.6620051
[3,] 0.4776196 0.2447973 0.31627171 0.4068302
 
$fcst_DNS[[2]]
          [,1]      [,2]       [,3]      [,4]
[1,] 0.9128759 0.3323947 0.47854525 0.8753213
[2,] 0.2936034 0.6508705 0.76631067 0.3390729
[3,] 0.4590657 0.2580168 0.08424691 0.8394404
 
cs


This post covers simple and trivial content, yet I belive it useful when comparing out-of-sample forecasting results across models under various specifications, including many models and different sample periods, among other factors.



No comments:

Post a Comment