Skip to content

Commit 8aaab58

Browse files
psolymosalexellis
authored andcommitted
Document last argument
Signed-off-by: Peter Solymos <psolymos@gmail.com>
1 parent 4ac65eb commit 8aaab58

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

_posts/2021-02-26-r-templates.md

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ covid_forecast <- function(region, cases, window, last) {
107107
## time series: daily new cases
108108
y <- pmax(0, diff(x$rawdata[[cases]]))
109109
## dates
110-
z <- as.Date(x$rawdata$date)
110+
z <- as.Date(x$rawdata$date[-1])
111111
## trim time series according to last date
112112
if (!missing(last)) {
113113
last <- min(max(z), as.Date(last))
@@ -135,16 +135,17 @@ function(region, cases, window, last) {
135135
}
136136
```
137137

138-
The R script loads the forecast package, defines the `covid_forecast` function with three arguments:
138+
The R script loads the forecast package, defines the `covid_forecast` function with 4 arguments:
139139

140140
- `region`: a region slug value for the API endpoint in global data set (see [available values](https://hub.analythium.io/covid-19/api/v1/regions/)),
141141
- `cases`: one of `"confirmed"` or `"deaths"`,
142-
- `windows`: a positive integer giving the forecast horizon in days.
142+
- `windows`: a positive integer giving the forecast horizon in days,
143+
- `last`: last day (`"YYYY-MM-DD"` date format) of the time series to consider.
143144

144145
The function gives the following output in R:
145146

146147
```R
147-
covid_forecast("canada-combined", cases="confirmed", window=4)
148+
covid_forecast("canada-combined", cases="confirmed", window=4, last="2021-02-18")
148149
# $Date
149150
# [1] "2021-02-19" "2021-02-20" "2021-02-21" "2021-02-22"
150151
# $`Point Forecast`
@@ -159,25 +160,25 @@ covid_forecast("canada-combined", cases="confirmed", window=4)
159160
# [1] 4646.033 4670.894 4705.710 4750.596
160161
```
161162

162-
The result of the call is a list with six elements, all elements are vectors of length 4 which is our time window. The `Date` element gives the days of the forecast, the `Point Forecast` is the expected value of the prediction, whereas the lower (`Lo`) and upper (`Hi`) prediction intervals represent the uncertainty around the point forecast. The 80% interval (within the `Lo 80` and `Hi 80` bound) and the 95% interval means that the 80% or 95% of the future observations will fall inside that range, respectively. The following plot combines the historical daily case counts and the 14-day forecast for Canada. The point forecast is the blue line, the 80% and 95% forecast intervals are the shaded areas:
163+
The result of the call is a list with six elements, all elements are vectors of length 4 which is our time window. The `Date` element gives the days of the forecast, the `Point Forecast` is the expected value of the prediction, whereas the lower (`Lo`) and upper (`Hi`) prediction intervals represent the uncertainty around the point forecast. The 80% interval (within the `Lo 80` and `Hi 80` bound) and the 95% interval means that the 80% or 95% of the future observations will fall inside that range, respectively.
163164

164-
![COVID-19 Canada](/images/2021-02-r/covid-canada-2021-02-19.png)
165+
The following plot combines the historical daily case counts and the 30-day forecast for Canada. The point forecast is the white line, the 80% and 95% forecast intervals are the blue shaded areas. I made two forecasts, the first on December 1st, 2020, the second on February 18th, 2021:
166+
167+
![COVID-19 Canada](/images/2021-02-r/covid-canada-2021-02-18.png)
165168

166169
The last part of the script defines the Plumber endpoint `/` for a GET request. One of the nicest features of Plumber is that it allows you to create a web API by [decorating the R source code](https://www.rplumber.io/articles/quickstart.html) with special `#*` comments. These annotations will tell Plumber how to handle the requests, what kind of parsers and formatters to use, etc. The current setup will treat the function arguments as URL parameters. The default content type for the response is JSON, thus we do not need to specify it.
167170

168171
```R
169172
#* COVID
170173
#* @get /
171-
function(region, cases, window) {
172-
if (missing(cases))
173-
cases <- "confirmed"
174-
if (missing(window))
175-
window <- 14
176-
covid_forecast(region, cases, as.numeric(window))
174+
function(region, cases, window, last) {
175+
if (!missing(window))
176+
window <- as.numeric(window)
177+
covid_forecast(region, cases, window, last)
177178
}
178179
```
179180

180-
Adding default values as part of the handle function arguments makes some of the URL parameters optional. In this case, we need to treat missing parameters as `missing()`. We also need to remember that URL form encoded parameters will be of character type, thus checking type and making appropriate type conversions is necessary (i.e. `as.numeric()` for the `window` argument passed to `covid_forecast`).
181+
The `covid_forecast` arguments can be missing except for region. This makes the corresponding URL parameters optional. We have to remember that URL form encoded parameters will be of character type, thus checking type and making appropriate type conversions is necessary (i.e. `as.numeric()` for the `window` argument).
181182

182183
### Build, push, and deploy the function
183184

@@ -194,10 +195,11 @@ curl -X GET -G \
194195
$OPENFAAS_URL/function/covid-forecast \
195196
-d region=canada-combined \
196197
-d cases=confirmed \
197-
-d window=4
198+
-d window=4 \
199+
- last=2021-02-18
198200
```
199201

200-
Or simply by visiting the URL `$OPENFAAS_URL/function/covid-forecast?region=canada-combined&window=4`. The output should be something like this (depending on the day you make the request):
202+
Or simply by visiting the URL `$OPENFAAS_URL/function/covid-forecast?region=canada-combined&window=4&last=2021-02-18`. The output should be something like this (depending on the day you make the request):
201203

202204
```bash
203205
{
@@ -210,13 +212,6 @@ Or simply by visiting the URL `$OPENFAAS_URL/function/covid-forecast?region=cana
210212
}
211213
```
212214

213-
Only the `region` parameter is mandatory, the the other two defaults to
214-
`cases="confirmed"` and `window=14`.
215-
`OPENFAAS_URL/function/covid-forecast?region=us` will be the same as
216-
`OPENFAAS_URL/function/covid-forecast?region=us&window=14`.
217-
218-
The time series itself that was the basis for the forecast, along with the forecast and the associated uncertainty (prediction intervals) for the US would look like the this:
219-
220215
### Wrapping up
221216

222217
In this post I showed how to use the R templates for OpenFaaS. We built a serverless function that consumes data from an external APIs, fits exponential smoothing model, and makes a forecast. The data API with the forecasting function can be added to web applications to provide timely updates on the fly.

0 commit comments

Comments
 (0)