-
Notifications
You must be signed in to change notification settings - Fork 1
/
readme.Rmd
717 lines (528 loc) · 20 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
---
title: WEBMICE -
subtitle: webmice 0.0.1 (December 2023)
author:
output:
html_document:
number_sections: false
self-contained: true
theme: united
editor_options:
chunk_output_type: console
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, cache = FALSE)
```
## Overview
WEBMICE is a web service for imputing and analysing incomplete datasets. WEBMICE provides a language-independent API to the functionality of the MICE R package.
It can
1. read data from local system of the client;
2. create multiple complete versions from incomplete data;
3. analyse each of the imputed datasets;
4. pool the analyses results into one result.
WEBMICE is a RESTful API that runs on a (remote) host. In principle, any `HTTP` client will work with WEBMICE. The following sections illustrate how a client can make requests to WEBMICE using various client languages.
### Primary WEBMICE end points
| Verb | API Endpoint | Description | Maps to `mice` function |
|:------|:-------------------------- |:------------------------------------------ |:-------------------------|
| GET | `/version` | Get the `mice`'s version | `utils::packageVersion("mice")` |
| GET | `/exampledata` | Get example of an incomplete data (`nhanes` and `nhanes2`) | `mice::nhanes` or `mice::nhanes2` |
| POST | `/data` | Upload an incomplete data in CSV format | |
| GET | `/long` | Impute missing data | `mice::mice()`
|
| GET | `/fit` | Get some predefined fit value | `mice::getfit(with())` |
| GET | `/pool` | Pool results of a fit given its summary table | `mice::pool()`
|
The table lists the defined API end points and the internal mapping between the API end point and the corresponding R function.
### Objective
This document provides a quick introduction into the main WEBMICE features, and how these can be assessed from `R` and from the bash command line.
## Installation
See the [installation guide](./vignettes/installation.Rmd).
## Features
### {.tabset}
#### **R**
##### **`/version`**: Obtain version information
Let us first check whether WEBMICE is running. The following code makes a simple request to WEBMICE to see whether it is alive and to return the version number of the underlying `mice` R package. We illustrate both requests in `R` and in `bash`.
We first need to install and load packages.
```{r httr, eval=FALSE}
# install.packages(c("httr2", "jsonlite"))
```
```{r load}
library(httr2)
library(jsonlite)
```
Let's find out the WEBMICE version number. In this document, we define the server that hosts WEBMICE as follows, and optionally create a variable `.host` for use in the bash terminal.
```{r host}
host <- "http://localhost:8080"
```
We first illustrate a method that makes a request to the server. The following command prepares a GET request `req` to the `/version` end point:
```{r req_version}
req <- request(host) |>
req_headers(accept = "text/plain") |>
req_url_path("/version")
req
```
The next step sends the request to the API server, and stores the result in the server response `resp`:
```{r call_version}
resp <- req_perform(req)
resp
```
The next step parses the `resp` object and converts the body into an R list:
```{r ret_version}
ret <- resp |>
resp_body_string() |>
fromJSON()
ret
```
See the documentation of the `httr2` package for other operations and extractions. One may chain the above operations as:
```{r}
request(host) |>
req_headers(accept = "text/plain") |>
req_url_path("/version") |>
req_perform() |>
resp_body_string() |>
fromJSON()
```
##### **`/exampledata`**: Impute built-in dataset
```{r}
req_ex <- request(host) |>
req_headers(accept = "text/json") |>
req_url_path("/exampledata") |>
req_url_query(name = "nhanes")
req_ex |> req_dry_run()
resp_ex <- req_perform(req_ex)
ret_ex <- resp_ex |>
resp_body_string() |>
fromJSON()
head(ret_ex$result)
```
##### **`/long`**: Impute built-in dataset
Raw call
```{r}
resp <- request("http://localhost:8080/long?payload=%7B%22data%22%3A%22nhanes%22%2C%22maxit%22%3A2%2C%22m%22%3A2%2C%22seed%22%3A1%7D") %>%
req_method("GET") %>%
req_headers(accept = "text/json") %>%
req_perform()
ret <- resp |>
resp_body_string() |>
fromJSON()
head(ret$result)
```
A more user-friendly call
```{r, eval=TRUE}
req_long <- request(host) |>
req_headers("accept" = "text/json") |>
req_url_path("/long") |>
req_url_query(payload = '{"data":"nhanes","maxit":2,"m":1,"seed":1}')
req_long |> req_dry_run()
resp_long <- req_perform(req_long)
ret_long <- resp_long |>
resp_body_string() |>
fromJSON()
head(ret_long$result)
```
##### **`/fit`**: Some predifined fits
All result from `/long` as input for `/fit`. A more user-friendly call (data comes from `complete(..., action = 'long', include = TRUE)`)
```{r, eval=TRUE}
body_long <- (toJSON(ret_long$result))
req_fit <- request(host) |>
req_headers("accept" = "text/json") |>
req_url_path("/fit") |>
req_url_query(payload = paste0('{"data":', body_long,', "model":["lm"], "formula":["chl ~ age + bmi"]}'))
req_dry_run(req_fit)
resp_fit <- req_fit |> req_perform()
ret_fit <- resp_fit |>
resp_body_string() |>
fromJSON()
ret_fit$result
```
##### **`/pool`**: Pool results of a fit given its summary table (mice 3.14.4 or higher)
```{r}
resp2 <- request("http://localhost:8080/pool?payload=%7B%22data%22%3A%20%5B%7B%22term%22%3A%22%28Intercept%29%22%2C%22estimate%22%3A23.9357%2C%22std.error%22%3A3.8732%2C%20%22statistic%22%3A6.1798%2C%22p.value%22%3A3.2106e-06%2C%22nobs%22%3A25%2C%22df.residual%22%3A22%7D%2C%20%7B%22term%22%3A%22hyp%22%2C%22estimate%22%3A1.1677%2C%22std.error%22%3A1.6218%2C%22statistic%22%3A0.72%2C%20%22p.value%22%3A0.4791%2C%22nobs%22%3A25%2C%22df.residual%22%3A22%7D%2C%7B%22term%22%3A%22chl%22%2C%22estimate%22%3A0.0063%2C%20%22std.error%22%3A0.0185%2C%22statistic%22%3A0.3385%2C%22p.value%22%3A0.7382%2C%22nobs%22%3A25%2C%22df.residual%22%3A22%7D%2C%20%7B%22term%22%3A%22%28Intercept%29%22%2C%22estimate%22%3A24.5434%2C%22std.error%22%3A4.7611%2C%22statistic%22%3A5.155%2C%20%22p.value%22%3A0%2C%22nobs%22%3A25%2C%22df.residual%22%3A22%7D%2C%7B%22term%22%3A%22hyp%22%2C%22estimate%22%3A1.5843%2C%20%22std.error%22%3A2.236%2C%22statistic%22%3A0.7086%2C%22p.value%22%3A0.486%2C%22nobs%22%3A25%2C%22df.residual%22%3A22%7D%2C%20%7B%22term%22%3A%22chl%22%2C%22estimate%22%3A-0.0032%2C%22std.error%22%3A0.0205%2C%22statistic%22%3A-0.1569%2C%20%22p.value%22%3A0.8768%2C%22nobs%22%3A25%2C%22df.residual%22%3A25%7D%5D%7D") %>%
req_method("GET") %>%
req_headers(accept = "text/json") %>%
req_perform()
ret2 <- resp2 |>
resp_body_string() |>
fromJSON()
head(ret2$result)
```
A more user-friendly call
```{r, eval=TRUE}
body_fit <- as.character(toJSON(ret_fit$result))
req_pool <- request(host) |>
req_headers("accept" = "text/json") |>
req_url_path("/pool") |>
req_url_query(payload = paste0('{"data":', body_fit,'}'))
req_dry_run(req_pool)
resp_pool <- req_pool |> req_perform()
ret_pool <- resp_pool |>
resp_body_string() |>
fromJSON()
ret_pool$result
```
##### **`/data`**: Upload data
Uploading data to WEBMICE can be done by sending a text file. Suppose that `../testdata/tempdata.csv` is a comma-delimited file with data. We may upload it as
```{r upload_r, eval = TRUE}
input <- curl::form_file("C:/Users/Zenius/Documents/GitHub/webmice/testdata/tempdata.csv", type = "text/csv")
#input <- curl::form_file("testdata/tempdata.csv", type = "text/csv")
resp <- request(host) |>
req_url_path("/data") |>
req_headers("accept" = "text/plain",
"content-type" = "multipart/form-data") |>
req_body_multipart(csvfile = input) |>
req_perform()
resp_header(resp, "data_token")
```
The file is uploaded to the server. The `data_token` header may be used for subsequent requests to refer to the uploaded data.
##### **`/long`**: Impute data using the token (not yet working)
```{r impute_r, eval=TRUE}
token <- resp_header(resp, "data_token")
token_with_quotes <- paste0('"', token, '"')
print(token_with_quotes)
req_longdata <- request(host) |>
req_headers("accept" = "text/json") |>
req_url_path("/long") |>
req_url_query(payload = paste0('{"data":', token_with_quotes, ',"maxit":2,"m":1,"seed":1}'))
req_longdata |> req_dry_run()
resp_longdata <- req_longdata |> req_perform()
ret_longdata <- resp_longdata |>
resp_body_string() |>
fromJSON()
head(ret_longdata$result)
```
##### **`/fit`**: Some predifined fits
```{r, eval=TRUE}
body_longdata <- as.character(toJSON(ret_longdata$result))
req_fitdata <- request(host) |>
req_headers("accept" = "text/json") |>
req_url_path("/fit") |>
req_url_query(payload = paste0('{"data":', body_longdata,', "model":["lm"], "formula":["chl ~ age + bmi"]}'))
req_dry_run(req_fitdata)
resp_fitdata <- req_fitdata |> req_perform()
ret_fitdata <- resp_fitdata |>
resp_body_string() |>
fromJSON()
ret_fitdata$result
```
##### **`/pool`**: Pool results of a fit given its summary table (mice 3.14.4 or higher)
```{r, eval=TRUE}
body_fitdata<- as.character(toJSON(ret_fitdata$result))
req_pooldata <- request(host) |>
req_headers("accept" = "text/json") |>
req_url_path("/pool") |>
req_url_query(payload = paste0('{"data":',body_fit,'}') )
req_dry_run(req_pooldata)
resp_pooldata <- req_pooldata |> req_perform()
ret_pooldata <- resp_pooldata |>
resp_body_string() |>
fromJSON()
ret_pooldata$result
```
#### **Python**
```{python}
import requests
from urllib.parse import urljoin
import urllib.parse
import json
import re
# Specify the url for host
host = 'http://localhost:8080'
```
##### **`/version`**: Obtain version information
```{python}
## /version
# Add path
path_version = '/version'
# Combine the url
version = urljoin(host,path_version)
# Add headers
headers = {'Accept': 'application/json'}
# Get the url
response_version = requests.get(version, headers=headers)
# See the response
response_version.content
```
##### **`/exampledata`**: Get the example data
There are two different example data sets, namely 'nhanes' and 'nhanes2'
```{python}
# /exampledata (nhanes)
# Add path
endpoint = "/exampledata"
# Construct the query parameters
params = {'name': 'nhanes'}
# Additional headers if needed
headers = {'Accept': 'application/json'}
# Make the GET request
response = requests.get(host + endpoint, params=params, headers=headers)
# Print the response
print(response.text)
```
##### **`/long`**: Impute built-in dataset
```{python}
## /long
# Add path
path_long = '/long'
# Combine the url
long = urljoin(host,path_long)
# Add headers
headers = {'Accept': 'application/json'}
# Add payload
params = {'data':'nhanes','maxit':2,'m':1,'seed':1}
# Encode the payload as a JSON string and then URL-encode the entire payload
payload = urllib.parse.quote('{"data":"%s","maxit":%d,"m":%d,"seed":%d}' % (params['data'], params['maxit'], params['m'], params['seed']))
# Construct the complete URL
long_url = f"{long}?payload={payload}"
# Make a request using the complete URL
response = requests.get(long_url)
# Get the response body
response.json()
json_response = response.json()
# Access the "result" part
result_part = json_response.get("result", [])
result_part
```
##### **`/fit`**: Some predifined fits
``` {python}
## /fit
# Add path
path_fit = '/fit'
# Combine the url
fit = urljoin(host,path_fit)
# Add headers
headers = {'Accept': 'application/json'}
# Add payload
params_fit = {
"data": result_part,
"model": ["lm"],
"formula": ["chl ~ age + bmi"]
}
# Encode the payload as a JSON string and then URL-encode the entire payload
payload_fit = json.dumps(params_fit)
url_encoded_payload_fit = urllib.parse.quote(payload_fit)
# Construct the complete URL
fit_url = f"{fit}?payload={url_encoded_payload_fit}"
# Make a request using the complete URL
response_fit = requests.get(fit_url)
# Get the response body
json_response_fit = response_fit.json()
# Access the "result" part
result_fit = json_response_fit.get("result", [])
result_fit
```
##### **`/pool`**: Pool results of a fit given its summary table (mice 3.14.4 or higher)
```{python}
## /pool
# Add path
path_pool = '/pool'
# Combine the url
pool = urljoin(host,path_pool)
# Add headers
headers = {'Accept': 'application/json'}
# Add payload
params_pool = {"data": result_fit}
# Encode the payload as a JSON string and then URL-encode the entire payload
payload_pool = json.dumps(params_pool)
url_encoded_payload_pool = urllib.parse.quote(payload_pool)
# Construct the complete URL
pool_url = f"{pool}?payload={url_encoded_payload_pool}"
# Make a request using the complete URL
response_pool = requests.get(pool_url)
# Get the response body
json_response_pool = response_pool.json()
# Access the "result" part
result_pool = json_response_pool.get("result", [])
result_pool
```
##### **`/data`**: Upload data
Uploading data to WEBMICE can be done by sending a text file. Suppose that `../testdata/tempdata.csv` is a comma-delimited file with data. We may upload it as
```{python}
# /data
# Add path
path_data = '/data'
# Combine the url
data_upload = urljoin(host,path_data)
# File path of the csv file
file_path = r"C:\Users\Zenius\Documents\GitHub\webmice\testdata\tempdata.csv"
# Construct the files dictionary
files = {'csvfile': (file_path, open(file_path, 'rb'))}
# Additional headers
headers = {'accept': 'text/plain'}
# Make the POST request
response = requests.post(data_upload, files=files, headers=headers)
# Print all response headers
for key, value in response.headers.items():
print(f"{key}: {value}")
# Retrieve the data_token from headers
data_token = response.headers.get('data_token')
print(data_token)
```
##### **`/long`**: Impute built-in dataset
This part is exactly the same procedure as the previous one using example data (nhanes). The only difference is the data part for the payload. We use `params = {'data':data_token,'maxit':2,'m':1,'seed':1}` instead of `params = {'data':'nhanes,'maxit':2,'m':1,'seed':1}`
```{python}
## /long
# Add path
path_long = '/long'
# Combine the url
long = urljoin(host,path_long)
# Add headers
headers = {'Accept': 'application/json'}
# Add payload
params = {'data':data_token,'maxit':2,'m':1,'seed':1}
# Encode the payload as a JSON string and then URL-encode the entire payload
payload = urllib.parse.quote('{"data":"%s","maxit":%d,"m":%d,"seed":%d}' % (params['data'], params['maxit'], params['m'], params['seed']))
# Construct the complete URL
long_url = f"{long}?payload={payload}"
# Make a request using the complete URL
response = requests.get(long_url)
# Get the response body
response.json()
json_response = response.json()
# Access the "result" part
result_part = json_response.get("result", [])
result_part
```
For `/fit` and `/pool` parts, follow the same steps using the example data.
##### **`/fit`**: Some predifined fits
``` {python}
## /fit
# Add path
path_fit = '/fit'
# Combine the url
fit = urljoin(host,path_fit)
# Add headers
headers = {'Accept': 'application/json'}
# Add payload
params_fit = {
"data": result_part,
"model": ["lm"],
"formula": ["chl ~ age + bmi"]
}
# Encode the payload as a JSON string and then URL-encode the entire payload
payload_fit = json.dumps(params_fit)
url_encoded_payload_fit = urllib.parse.quote(payload_fit)
# Construct the complete URL
fit_url = f"{fit}?payload={url_encoded_payload_fit}"
# Make a request using the complete URL
response_fit = requests.get(fit_url)
# Get the response body
json_response_fit = response_fit.json()
# Access the "result" part
result_fit = json_response_fit.get("result", [])
result_fit
```
##### **`/pool`**: Pool results of a fit given its summary table (mice 3.14.4 or higher)
```{python}
## /pool
# Add path
path_pool = '/pool'
# Combine the url
pool = urljoin(host,path_pool)
# Add headers
headers = {'Accept': 'application/json'}
# Add payload
params_pool = {"data": result_fit}
# Encode the payload as a JSON string and then URL-encode the entire payload
payload_pool = json.dumps(params_pool)
url_encoded_payload_pool = urllib.parse.quote(payload_pool)
# Construct the complete URL
pool_url = f"{pool}?payload={url_encoded_payload_pool}"
# Make a request using the complete URL
response_pool = requests.get(pool_url)
# Get the response body
json_response_pool = response_pool.json()
# Access the "result" part
result_pool = json_response_pool.get("result", [])
result_pool
```
#### **bash**
We use the `curl` Linux command. If needed, on Ubuntu install `curl` as
```{bash, eval=FALSE}
sudo apt update
sudo apt -y install curl
```
##### **`/version`**: Obtain version information
Let’s find out the WEBMICE version number. We first illustrate a method that makes two requests to the server.
The following `bash` commands call the `/version` API end point with a GET request, and stores the response in file `resp`.
The response to the request consists of a set of URLs created on the server, each of which contains details on the response.
```{bash, eval = FALSE}
echo http://localhost:8080 > .host
curl \
-X GET "$(cat .host)/version" \
-H "accept: text/plain" \
-o resp \
cat resp
```
##### **`/exampledata`**: Obtain example data
The R package `mice` contains a few built-in datasets (See <https://amices.org/mice/reference/index.html#datasets>). These datasets can be imputed and analysed directly on the server. This is to get example data of `nhanes`
```{bash, eval = FALSE}
curl \
-X GET "$(cat .host)/exampledata?name=nhanes" \
-H "accept: text/json" \
-o nhanes.json \
cat nhanes.json
```
##### **`/data`**: Upload data
Uploading data to WEBMICE can be done by sending a text file.
Suppose that `../testdata/tempdata.csv` is a comma-delimited file with data. We may upload it as:
(This part should be change to your full local path `F "csvfile=@fullpath/tempdata.csv;type=text/csv"`).
From this part, we can obtain data_token from the uploaded csv file
```{bash, eval = FALSE}
curl -i \
-X POST "$(cat .host)/data" \
-H "accept: text/plain" \
-H "Content-Type: multipart/form-data" \
-F "csvfile=@C:/Users/Zenius/Documents/GitHub/webmice/testdata/tempdata.csv;type=text/csv" \
-o response.txt \
cat response.txt
# Get the data_token from headers
data_token=$(grep -i '^data_token:' response.txt | awk '{print $2}')
echo "$data_token"
```
The file is uploaded to the server. The `data_token` header may be used for subsequent requests to refer to the uploaded data.
##### **`/long`**: Obtain version information
Get the long data for `nhanes` data
```{bash, eval = FALSE}
curl -G "$(cat .host)/long" \
-H "accept: application/json" \
--data-urlencode 'payload={"data":"nhanes","maxit":2,"m":1,"seed":1}' \
-o outputlong.json \
cat outputlong.json
```
Get the long data for uploaded data using the data_token
```{bash, eval = FALSE}
curl -G -v "$(cat .host)/long" \
-H "accept: application/json" \
--data-urlencode 'payload={"data":"'$data_token'","maxit":2,"m":1,"seed":1}' \
-o longdata.json \
cat longdata.json
```
##### **`/fit`**: Obtain version information
```{bash, eval = FALSE}
# Extract the result from JSON and remove spaces
result_long=$(jq -r '.result' outputlong.json | tr -d '[:space:]')
# Encode the result_long as JSON string and remove spaces
result_long=$(echo "$result_long" | jq -c -r)
# Construct the payload with the encoded result_long
payload="{\"data\":$result_long,\"model\":[\"lm\"],\"formula\":[\"chl~age+bmi\"]}"
# Use the payload in the curl command
curl -G -v "$(cat .host)/fit" \
-H "accept: application/json" \
--data-urlencode "payload=$payload" \
-o outputfit.json
```
##### **`/pool`**: Obtain version information
```{bash, eval = FALSE}
# Extract the result from JSON and remove spaces
result_fit=$(jq -r '.result' outputfit.json | tr -d '[:space:]')
# Encode the result_long as JSON string and remove spaces
result_fit=$(echo "$result_fit" | jq -c -r)
# Construct the payload with the encoded result_long
payload="{\"data\":$result_fit}"
# Use the payload in the curl command
curl -G -v "$(cat .host)/pool" \
-H "accept: application/json" \
--data-urlencode "payload=$payload" \
-o outputpool.json
```
### {-}