-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
152 lines (116 loc) · 7.25 KB
/
README.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/",
out.width = "100%"
)
```
# lmForc <a href='https://github.com/nelson-n/lmForc/blob/main/vignettes/logo/lmForc_hexSticker.png'><img src='/Users/nelsonrayl/Desktop/init/lmForc/lmForc/vignettes/logo/lmForc_hexSticker.png' align="right" height="200" /></a>
<!-- badges: start -->
[![CRAN Version](https://www.r-pkg.org/badges/version/lmForc)](https://www.r-pkg.org/pkg/lmForc)
[![CRAN Posit Mirror Downloads](https://cranlogs.r-pkg.org/badges/grand-total/lmForc)](https://www.r-pkg.org/pkg/lmforc)
[![R-CMD-check](https://github.com/nelson-n/lmForc/workflows/R-CMD-check/badge.svg)](https://github.com/nelson-n/lmForc/actions)
[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-success.svg)](https://lifecycle.r-lib.org/articles/stages.html)
<!-- badges: end -->
<!-- [![Monthly Downloads](https://cranlogs.r-pkg.org/badges/lmForc)](https://www.r-pkg.org/pkg/lmforc) -->
The R package *lmForc* introduces functions for testing forecasting models and a new class for working with forecast data: `Forecast`. Test models out-of-sample by conditioning on realized values, vintage forecasts, or lagged values. Benchmark against AR models, historical average forecasts, or random walk forecasts. Create performance weighted or states weighted forecast combinations. These functions are built around the `Forecast` class and support both linear forecasting models and more complex models such as logistic regression, tree based models, and neural networks.
## Vignette
For an overview of the *lmForc* package, please read the vignette: [lmForc Vignette](https://html-preview.github.io/?url=https://github.com/nelson-n/lmForc/blob/main/doc/lmForc.html)
<!-- Optional: Link to vignette that is uploaded to CRAN: https://cran.r-project.org/web/packages/lmForc/vignettes/lmForc.html -->
<a href='https://cran.r-project.org/web/packages/lmForc/vignettes/lmForc.html'><img src='/Users/nelsonrayl/Desktop/init/lmForc/lmForc/vignettes/vignette_demo.png' align="center" height="220" /></a>
## Paper
Accompanying Paper: [lmForc: Linear Model Forecasting in R](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=4130453)
**Abstract**: Linear forecasting models are a popular option in many domains due to their simplicity and interpretability. This paper considers the tools a forecaster has for evaluating linear forecasting models and presents lmForc, a package which implements these evaluation functions. Functions in the lmForc package are built around a new S4 class, Forecast, which introduces a dedicated data structure for storing forecasts to the R language and permits the creation of complex forecasting functions. The lmForc package is designed to leverage the simplicity and interpretability of linear models so that a forecaster may easily test model specifications and understand precisely how a model arrives at a forecast.
## Installation
To install the **stable** version from [CRAN](https://cran.r-project.org/package=lmForc):
```{r cran-installation, eval = FALSE}
install.packages("lmForc")
```
To install the **development** version from [GitHub](https://github.com/nelson-n/lmForc):
```{r github-installation, eval = FALSE}
# install.packages("remotes")
remotes::install_github("nelson-n/lmForc")
```
## Linear Forecasting Model Example
Produce an out-of-sample forecast conditioned on realized values. Calculates linear model coefficients in each
period based on information that would have been available to the forecaster. Coefficients are combined with
future realized values to compute a conditional forecast. Evaluates the performance of a linear model had it
been conditioned on perfect information.
```{r example1, message=FALSE}
library(lmForc)
# Stylized dataset.
date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31",
"2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31",
"2012-03-31", "2012-06-30"))
y <- c(1.09, 1.71, 1.09, 2.46, 1.78, 1.35, 2.89, 2.11, 2.97, 0.99)
x1 <- c(4.22, 3.86, 4.27, 5.60, 5.11, 4.31, 4.92, 5.80, 6.30, 4.17)
x2 <- c(10.03, 10.49, 10.85, 10.47, 9.09, 10.91, 8.68, 9.91, 7.87, 6.63)
data <- data.frame(date, y, x1, x2)
# Linear model out-of-sample forecast.
forecast1 <- oos_realized_forc(
lm_call = lm(y ~ x1 + x2, data),
h_ahead = 2L,
estimation_end = as.Date("2011-03-31"),
time_vec = data$date
)
forecast1
```
Produce an out-of-sample forecast based on the historical median. In each period
the historical median of the series is calculated based on information that would
have been available to the forecaster. Replicates the historical median forecast
that would have been produced in real-time and serves as a benchmark for other models.
```{r example2, message=FALSE}
# Historical Median Forecast
forecast2 <- historical_average_forc(
avg_function = "median",
realized_vec = data$y,
h_ahead = 2L,
estimation_end = as.Date("2011-03-31"),
time_vec = data$date
)
forecast2
```
Compare the performance of both models by calculating RMSE forecast error.
```{r example3, message=FALSE}
rmse(forecast1)
rmse(forecast2)
```
Create a performance weighted forecast combination of `forecast1` and `forecast2`. In each period forecast accuracy is calculated over recent periods and each model is given a weight based on recent accuracy. The forecast for the next period is calculated as a weighted combination of both forecasts.
```{r example4, message=FALSE}
performance_weighted_forc(
forecast1, forecast2,
eval_window = 1L,
errors = "mse",
return_weights = FALSE
)
```
## General Forecasting Model Example
Produces an out-of-sample forecast conditioned on realized values similar to the example above, but does so using a logistic regression. Functions with `_general` in the name are designed to work with any model that can be estimated in R, including tree based models, neural networks, or models with hand-built parameters. The user only needs to specify a `model_function` that estimates the model and the `prediction_function` which produces predictions from the model.
```{r example5, message=FALSE}
# Stylized Dataset.
date <- as.Date(c("2010-03-31", "2010-06-30", "2010-09-30", "2010-12-31",
"2011-03-31", "2011-06-30", "2011-09-30", "2011-12-31",
"2012-03-31", "2012-06-30", "2012-09-30", "2012-12-31",
"2013-03-31", "2013-06-30", "2013-09-30", "2013-12-31"))
y <- c(1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0)
x1 <- c(8.22, 3.86, 4.27, 3.37, 5.88, 3.34, 2.92, 1.80, 3.30, 7.17, 3.22, 3.86, 4.27, 3.37, 5.88, 3.34)
x2 <- c(4.03, 2.46, 2.04, 2.44, 6.09, 2.91, 1.68, 2.91, 3.87, 1.63, 4.03, 2.46, 2.04, 2.44, 6.09, 2.91)
dataLogit <- data.frame(date, y, x1, x2)
# Logit model out-of-sample forecast.
forecast3 <- oos_realized_forc_general(
model_function = function(data) {glm(y ~ x1 + x2, data = data, family = binomial)},
prediction_function = function(model_function, data) {as.vector(predict(model_function, data, type = "response"))},
data = dataLogit,
realized = dataLogit$y,
h_ahead = 2L,
estimation_end = as.Date("2012-06-30"),
time_vec = dataLogit$date,
estimation_window = NULL
)
forecast3
```