-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.Rmd
426 lines (299 loc) · 12 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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(eval = FALSE)
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# R sasctl
<!-- badges: start -->
<!-- badges: end -->
## Table of Contents
1. [Overview](#overview)
2. [Installation](#installation)
3. [Session](#session)
4. [Examples](#examples)
* [PMML to SAS Example](#pmml-to-sas-example)
* [A native R model example](#a-native-r-model-example)
* [vPOST and vGET convenient functions](#vpost-and-vget-convenient-functions)
5. [Model Management helpers](#model-management-helpers)
## Overview
The goal of sasctl is to provide tools for easy access of SAS Viya APIs from an R perspective. It has useful tools to make model management easier.
## Installation
### Install from R
```{r, eval=FALSE}
## dev version
remotes::install_git("https://github.com/sassoftware/r-sasctl")
## released version
## You first have to install the dependencies
install.packages(c("jsonlite", "httr", "uuid", "furrr", "ROCR", "reshape2", "base64enc", "dplyr", "glue"))
## then the package
install.packages("https://github.com/sassoftware/r-sasctl/releases/download/X.X.X/r-sasctl_X.X.X.tar.gz", type = "source", repos = NULL)
library("sasctl")
```
### Install from terminal
The SWAT package for R is available from SAS as a tar.gz file. You can download releases from [https://github.com/sassoftware/r-sasctl/releases](https://github.com/sassoftware/r-sasctl/releases).
After you download the package, you can install the package with a command that is similar to the following:
```{bash}
R CMD INSTALL r-sasctl-X.X.X.tar.gz
```
## Session
You have a few options on how to make a connections to the SAS Viya server. The first example uses password authentication.
```{r reg_connection_real, echo=FALSE, include=FALSE, cache = FALSE}
source("local_auth.R")
```
```{r reg_connection, eval=FALSE}
sess <- session(hostname = "http://myserver.sas.com",
username = "sasuser",
password = "s3cr3t")
sess
```
You may also use the .authinfo file.
```{r token_connection, eval=FALSE}
## authinfo file (recommended)
sess <- session(hostname = "http://myserver.sas.com",
authinfo = "./_authinfo")
# authinfo file structure:
# default login sasuser password s3cr3t
## or for mutiple hosts in a single file but hostname must match otherwise will fail
# host http://server1.sas.com login sasuser password s3cr3t
# host https://server2.sas.com login sasuser password s3cr3t
```
If you were provided access tokens or client_id and client_secret, you may follow one of the following methods.
```{r token_connection2, eval=FALSE}
## cient_id and client_secret
sess <- session(hostname = "http://myserver.sas.com",
client_id = "client_id",
client_secret = "client_s3cr3t")
## token type
sess2 <- session(hostname = "https://myserver.sas.com",
oauth_token = token$access_token
)
sess2
## if you want to use authorization code from Viya 4
## will open a browser, login and then copy and paste the code in the R prompt terminal
## set client secret as "", only a client_id with authorization_code permission
sess2 <- session(hostname = "https://myserver.sas.com",
username = "username" # not required, you will be prompt on browser
client_id = "client_id", # only if default was removed
auth_code = TRUE
)
```
## Examples
The following examples offer different options of the model management life cycle. First, the model is created. Then the model is registered and published. Finally, the model is scored. The code samples include expected responses inline as well.
### PMML to SAS Example
```{r full_example_part1, cache = TRUE}
hmeq <- read.csv("https://support.sas.com/documentation/onlinedoc/viya/exampledatasets/hmeq.csv")
## removing missing data
hmeq[hmeq == ""] <- NA # empty strings to NA
hmeq <- na.omit(hmeq)
hmeq$BAD <- as.factor(hmeq$BAD)
hmeq$REASON <- as.factor(hmeq$REASON)
hmeq$JOB <- as.factor(hmeq$JOB)
## creating logistic regression
model1 <- glm(BAD ~ ., hmeq, family = binomial("logit"))
summary(model1)
## saving model as pmml
XML::saveXML(pmml::pmml(model1, model.name = "General_Regression_Model",
app.name = "Rattle/PMML",
description = "Logistic Regression Model"),
"my_model.pmml")
## registering the model
mod <- register_model(
session = sess,
file = "my_model.pmml",
name = "R_model_pmml",
type = "pmml",
project = "rsasctl_auto",
force = TRUE
)
module <- publish_model(sess, mod, "R_model_pmml") ## defaults to maslocal
## 10 rows
## see documentation for parallel request
scored <- masScore(sess, module, hmeq[1:10,-1])
scored
## deleteing a project delete all associated models
delete_project(sess, "rsasctl_auto")
## delete the published model
delete_masmodule(sess, "R_model_pmml")
```
### A native R model example
```{r example2, cache = TRUE}
hmeq <- read.csv("https://support.sas.com/documentation/onlinedoc/viya/exampledatasets/hmeq.csv")
hmeq[hmeq == ""] <- NA
hmeq <- na.omit(hmeq) ### probably you don't want to do that, by for sake of simplicity
hmeq$BAD <- as.factor(hmeq$BAD)
hmeq$REASON <- as.factor(hmeq$REASON)
hmeq$JOB <- as.factor(hmeq$JOB)
### creating train/test/val
partition <- sample(c(1,2,3), replace = TRUE, prob = c(0.7, 0.2, 0.1), size = nrow(hmeq))
### logistic regression
model1 <- glm(formula = BAD ~ .,
family = binomial(link = 'logit'),
data = hmeq[partition == 1,]
)
### model summary
summary(model1)
dir.create("myModel")
path <- "myModel/"
## model saved
saveRDS(model1, paste0(path, 'rlogistic.rda'), version = 2)
## creating the score code
code <- codegen(model1, path = paste0(path, "scoreCode.R"), rds = "rlogistic.rda")
## The following function to creates a sample if you don't want to use the generated code
# create_scoreSample(path, openFile = FALSE)
## scoring the whole table
## running the generated scoring code for testing
codeExpression <- str2expression(code)
eval(codeExpression)
rdsPath <- path
result <- scoreFunction(LOAN = hmeq[, 'LOAN'],
MORTDUE = hmeq[, 'MORTDUE'],
VALUE = hmeq[, 'VALUE'],
REASON = hmeq[, 'REASON'],
JOB = hmeq[, 'JOB'],
YOJ = hmeq[, 'YOJ'],
DEROG = hmeq[, 'DEROG'],
DELINQ = hmeq[, 'DELINQ'],
CLAGE = hmeq[, 'CLAGE'],
NINQ = hmeq[, 'NINQ'],
CLNO = hmeq[, 'CLNO'],
DEBTINC = hmeq[, 'DEBTINC'])
scoreddf <- as.data.frame(result)
scoreddf$Actual <- as.numeric(hmeq$BAD) - 1
scoreddf$partition <- partition
### diagnostics requires the true Target column name defined in "targetName"
### and the predicted probability column name defined in "targetPredicted"
diags <- diagnosticsJson(validadedf = scoreddf[scoreddf$partition == 3,],
traindf = scoreddf[scoreddf$partition == 1,],
testdf = scoreddf[scoreddf$partition == 2,],
targetEventValue = 1,
targetName = "Actual",
targetPredicted = "EM_EVENTPROBABILITY",
path = path) ## safely ignore warning, knitr bug
## writing other files
write_in_out_json(hmeq[,-1], input = TRUE, path = path)
write_in_out_json(scoreddf[-c(4, 8, 9)], input = FALSE, path = path)
write_fileMetadata_json(scoreCodeName = "scoreCode.R",
scoreResource = "rlogistic.rda",
path = path)
write_ModelProperties_json(modelName = "Rlogistic",
modelFunction = "Classification",
trainTable = "hmeq",
algorithm = "Logistic Regression",
numTargetCategories = 2,
targetEvent = "1",
targetVariable = "BAD",
eventProbVar = "P_BAD1",
modeler = "sasctl man",
path = path)
files_to_zip <- list.files(path, "*.json|*.R|*.rda", full.names = T)
zip(paste0(path, "Rmodel.zip"),
files = files_to_zip)
mod <- register_model(
session = sess,
file = "myModel/Rmodel.zip",
name = "RzipModel",
type = "zip",
project = "R_sasctl",
force = TRUE
)
## deleteing a project delete all associated models
delete_project(sess, "R_sasctl")
```
### vPOST and vGET convenient functions
#### MAS call example
You can make generic calls to endpoints with minimal effort.
```{r convenient_get, cache = TRUE}
models <- vGET(sess,
"microanalyticScore/modules/")
models$items[c(2:3, 8)]
```
Next, we need to create the transform table using the correct JSON
payload for a MAS call, which doesn’t have a standard format.
```{json, cache = TRUE}
### Payload for Viya MAS
{"inputs": [
{"name": "<input1_name>", "value": 123},
{"name": "<input2_name>", "value": "string_value"},
{"name": "<input3_name>", "value": null} ## if value: NA
]
}
### Payload for SCR on Viya 2021.1.5 or higher
{
"metadata": {
"<metadata_1>": 1,
"<metadata_2>": "any metadata string",
"<metadata_3>": 3
},
"data": {
"<input1_name>": 5.1,
"<input2_name>": 0.2,
"<input3_name>": "string_value"
}
}
```
There is a helper function that transform all the rows in a vector
of strings, where each string is a JSON payload, since you cannot send
data for batch scoring.
```{r json_transform, cache = TRUE}
hmeq <- read.csv("https://support.sas.com/documentation/onlinedoc/viya/exampledatasets/hmeq.csv")
hmeq_json <- format_data_json(head(hmeq)) ## use argument scr = TRUE for newer format
jsonlite::prettify(hmeq_json[1])
```
Then you can make a call to a Model.
```{r, cache = TRUE}
output <- sasctl::vPOST(sess,
path = "microanalyticScore/modules/dt_hmeq/steps/score",
payload = hmeq_json[3], ## choose a row
# content_type used to be hard coded, but we would have less flexibility
httr::content_type("application/json")
)
output
```
### Model Management helpers
```{r, cache = TRUE}
## to write inputVar.json
## removing BAD column
write_in_out_json(hmeq[,2:ncol(hmeq)], input = FALSE)
```
```{r, cache = TRUE}
## to write outputVar.json
## you should create your own output dataframe since it will be what you
## put in your score code, which usually means EM_PROBABILITY, EM_CLASSIFICATION,
## or whaterver names you create P_BAD1, P_BAD0, etc.
out_example <- data.frame(P_BAD0 = 0.78,
P_BAD1 = 0.22,
BAD = '1')
write_in_out_json(out_example)
```
```{r, cache = TRUE}
## to write fileMetadata.json
## defaults should be fine, unless you use different file names
write_fileMetadata_json()
```
```{r, cache = TRUE}
## to write ModelProperties.json
## defaults should be fine, unless you use different file names
write_ModelProperties_json(modelName = "My R Model",
modelDescription = "Awesome Description",
modelFunction = "Classification",
trainTable = " ",
algorithm = "Logistic Regression",
numTargetCategories = 2,
targetEvent = "BAD",
targetVariable = "P_BAD1",
eventProbVar = "P_BAD1",
modeler = "John SAS")
```
```{r delete_files, echo=FALSE, include=FALSE}
unlink(c('inputVar.json', 'outputVar.json', "ModelProperties.json", "fileMetadata.json",
"my_model.pmml", "my_model_converted42.pmml",
"myModel"), recursive = TRUE)
```