You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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:
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.
167
170
168
171
```R
169
172
#* COVID
170
173
#* @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)
177
178
}
178
179
```
179
180
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).
181
182
182
183
### Build, push, and deploy the function
183
184
@@ -194,10 +195,11 @@ curl -X GET -G \
194
195
$OPENFAAS_URL/function/covid-forecast \
195
196
-d region=canada-combined \
196
197
-d cases=confirmed \
197
-
-d window=4
198
+
-d window=4 \
199
+
- last=2021-02-18
198
200
```
199
201
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):
201
203
202
204
```bash
203
205
{
@@ -210,13 +212,6 @@ Or simply by visiting the URL `$OPENFAAS_URL/function/covid-forecast?region=cana
210
212
}
211
213
```
212
214
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
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
-
220
215
### Wrapping up
221
216
222
217
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