-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReplicate_Paper.Rmd
115 lines (73 loc) · 4.41 KB
/
Replicate_Paper.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
---
title: "Modeling the COVID-19 Infection Rates by Regime Switching Unobserved Components Models"
output: html_notebook
number_sections: TRUE
---
This notebook replicates the results of **Haimerl, P., and Hartl, T. (2022). Modeling the COVID-19 infection rates by regime-switching unobserved components models**
If you use any of the code, please cite this paper.
In case of questions or if you encounter a bug, please feel free to reach out or raise an issue.
------------------------------------------------------------------------
#### 1. Set up the working directory
Note that the folder "Scripts" as well as the folder holding the grid search results have to be stored in the working directory.
```{r}
rm(list = ls())
# setwd()
```
#### 2. Initialize the environment
Load packages as well as the data on COVID-19 infections by executing the initialization function. The string "modelSpec" indicates which UC-model to run. Our model specifications are stored in R/Models.
```{r message=FALSE, warning=TRUE}
source("R/0_Init.R")
modelSpec <- "D.Seas.C."
startDate <- "2020-04-1"
endDate <- Sys.Date()
country <- "US"
Init_fctn(modelSpec, startDate, endDate, country)
```
#### 3. Search for the perfect parameters
This function executes the 3-step grid search routine. Note that the grid search results as used in in the paper are already provided in the repository. Running the grid search is computationally intensive and may require up to 2 hours to complete. If you run the grid search, the results are stored in Output/"Model_Spec"\_Output/GridSearch.
```{r}
GridSearch_fctn(model = modelSpec, data = dataVec, trendIni = trendIni, nRandomGrid = 3e04, stepsGrid = 6, storeOutput = TRUE)
```
#### 4. Apply the model
The following step employs the grid search results to run the Kim filter and smoother recursions. All relevant output is collected in the modelOutputList.
```{r}
modelOutputList <- ModelApplication_fctn(model = modelSpec, data = dataVec, trendIni = trendIni, dataTib = dataTib)
```
#### 5. Run the nowcasting simulation
Next, we mimic a nowcasting exercise. The parameter estimates are based on increasing subsets of the entire observational horizon. The nowcasted values reflect the one-step-ahead predictions of the Kim filter. The output is stored in Output/"Model_Spec"\_Output/Nowcasting.
```{r}
Nowcasting_fctn(
model = modelSpec, data = dataVec, trendIni = trendIni, dataTib = dataTib,
iniPeriod = 150, estimInterval = 2, gridSearchInterval = 500
)
```
#### 6. Visualize the findings
This code chunk uses the previously collected output to reproduce the figures in the paper. The figures are stored in Output/"Model_Spec"\_Output/Figures.
```{r}
Figures_fctn(
model = modelSpec, modelOutput = modelOutputList, dataTib = dataTib, dataExten = dataTibExten
)
```
#### 7. Report the estimates
Tables 1 and 2 may be recreated by running the following block. An .xlsx book with the all entries of the tables as well as the various regime indicators as reported below table 1 will be created in Output/"Model_Spec"\_Output/Tables.
```{r}
Tables_fctn(model = modelSpec, modelOutput = modelOutputList)
```
#### 9. Investigate the estimation method via a Monte Carlo study
To evaluate the actual estimation performance of the model at hand, the following Chunk performs a Monte Carlo simulation study. Even though we use warm starts to make the routine more efficient, this step can by quite computationally expensive. A summary of the results together with a data set of the nIters estimates is stored in Output/"Model_Spec"\_Output/MonteCarlo.
```{r}
KimMCStudy_fctn(model = modelSpec, nPeriods = 1000, nIters = 1000, nRandomGrid = 30e4, stepsGrid = 6)
```
#### 9. Run further diagnostics
This code chunk produces diagnostic plots of the one-step-ahead prediction errors, their AC-function, the difference between smoothed trend and log infections over time as well as the seasonal component. All figures are stored in Output/"Model_Spec"\_Output/Diagnostics
```{r}
Diagnostics_fctn(model = modelSpec, dataTib = dataTib, modelOutput = modelOutputList)
```
#### 10. Validate the frequentist inference with a Gibbs sampler
The following function estimates the model via a Gibbs sampler. The output is stored in Output/"Model_Spec"\_Output/GibbsSampling
```{r}
MCMCOutput <- GibbsSampler_fctn(
model = modelSpec, data = dataVec, trend = trendIni, nIters = 30e3, nBurnin = 5e3,
dataTib = dataTib, thinIntervall = 3, thinIntervallReverse = TRUE
)
```