Skip to content

Commit 3e72322

Browse files
committed
version 0.7
1 parent ebd3b08 commit 3e72322

File tree

5 files changed

+52
-17
lines changed

5 files changed

+52
-17
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: sasctl
22
Title: The sasctl package enables easy communication between the SAS Viya platform APIs and the R runtime
3-
Version: 0.6.4.9000
3+
Version: 0.7.0
44
Author: Eduardo Hellas
55
Authors@R: c(
66
person(given = "Eduardo",

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# sasctl (development version)
1+
# sasctl 0.7.0
22

33
* Fixed correct release retrieval when using `session` to connect to Viya 2020.x
44
* Added `codegen` function in experimental state. Works for simple `lm`, `glm` models and [tidymodels](https://www.tidymodels.org/) `workflow` with regression or classification model mode`

R/codegen.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ codegen.workflow <- function(tm_workflow, path = "scoreCode.R", rds = "model.rds
201201
202202
output_list <- list(EM_CLASSIFICATION = predictions[[".pred"]],
203203
EM_EVENTPROBABILITY = predictions[[".pred_<<referenceLevel>>"]],
204-
EM_PROBABILITY = subset(predictions, select = -c(.pred))[boolClass],
204+
EM_PROBABILITY = apply(subset(predictions, select = -c(.pred)), 1, max),
205205
I_<<target>> = predictions[[".pred"]],
206206
<<target>> = predictions[[".pred"]],
207207
<<p_labels>>
@@ -237,9 +237,10 @@ codegen.workflow <- function(tm_workflow, path = "scoreCode.R", rds = "model.rds
237237
if (!exists("sasctlRmodel"))
238238
{
239239
assign("sasctlRmodel", readRDS(file = paste(rdsPath, "<<rds>>", sep = "")), envir = .GlobalEnv)
240-
<<target_labels_string>>
240+
241241
}
242-
242+
<<target_labels_string>>
243+
243244
data <- data.frame(<<paste(predictors," = ", predictors, collapse = ",\n ")>>)
244245
245246
predictions <- predict(sasctlRmodel, new_data = data, type = "<<response_type>>")

R/json_files.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,15 @@ write_in_out_json <- function(data, input = TRUE, path = './', noFile = FALSE){
3737
if (is.factor(x)) as.character(x) else x }))
3838

3939
### variable lengths, 8 is default for numeric
40+
41+
4042
size <- sapply(data,
4143
FUN = function(x){
42-
if (is.character(x)) max(sapply(x, nchar)) else {8}}
44+
if (is.character(x)) max(sapply(x, nchar), na.rm = TRUE) else {8}}
4345
)
4446

47+
size[sapply(size, function(x) is.na(x) | x == 0 )] <- 8
48+
4549
df <- data.frame(name = names(vars),
4650
length = size,
4751
type = vars,

vignettes/xgb-tidymodel.Rmd

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ knitr::opts_chunk$set(
1414
)
1515
```
1616

17-
R-sasctl has tools to help you create all the necessary files to upload an R model successfully to SAS Viya Model Manager and be able to take advantage of the many .
17+
R-sasctl has tools to help you create all the necessary files to upload an R model successfully to SAS Viya Model Manager and be able to take advantage of its many features.
1818

1919
## Simple data preparation
2020

@@ -126,6 +126,8 @@ Scoring codes are required to run R and Python models in SAS Model Manager, `sas
126126

127127
You will notice that the scoring code has many `EM_*` and `P_<<target>><<level>>` variables. They're not required, but it makes very consistent on how models made in SAS Viya UI are created. Making it easier to mix these models.
128128

129+
130+
## Generating the Score code
129131
```{r vars, echo=FALSE, include=FALSE}
130132
used_vars <- paste0("`",colnames(xgb_fitted[["pre"]][["mold"]][["predictors"]]), "`", collapse = ", ")
131133
expected_vars <- paste0("`",colnames(hmeq)[-1], "`", collapse = ", ")
@@ -139,24 +141,52 @@ We can use the `inputs` to generate alternate input variables. In this case it i
139141
code <- codegen(xgb_fitted,
140142
path = paste0(path, "scoreCode.R"),
141143
inputs = colnames(hmeq)[-1],
144+
referenceLevel = 1,
142145
rds = modelrda)
143146
code
144147
```
145148

146-
Now we create some additional files which are required to configure SAS Model Manager when uploading the files. For the variables specifically, should match the inputs from the model and outputs (outputs may be needed to match what comes out the scoring code)
149+
### Testing the scoring code
150+
151+
```{r test_code}
152+
153+
## getting the .rda Path to be called in the function
154+
## this is simulating what Viya passes to R when calling it
155+
rdsPath <- path
156+
157+
## Calling the generated code string as code to
158+
## create the function locally
159+
codeExpression <- str2expression(code)
160+
eval(codeExpression)
161+
162+
## this is a helper to create the variables
163+
## cat(paste0(colnames(hmeq)[-1], " = hmeq[, '", colnames(hmeq)[-1],"']", collapse = ",\n " ))
164+
165+
scoreRes <- scoreFunction(LOAN = hmeq[, 'LOAN'],
166+
MORTDUE = hmeq[, 'MORTDUE'],
167+
VALUE = hmeq[, 'VALUE'],
168+
REASON = hmeq[, 'REASON'],
169+
JOB = hmeq[, 'JOB'],
170+
YOJ = hmeq[, 'YOJ'],
171+
DEROG = hmeq[, 'DEROG'],
172+
DELINQ = hmeq[, 'DELINQ'],
173+
CLAGE = hmeq[, 'CLAGE'],
174+
NINQ = hmeq[, 'NINQ'],
175+
CLNO = hmeq[, 'CLNO'],
176+
DEBTINC = hmeq[, 'DEBTINC'])
177+
178+
scoreRes <- as.data.frame(scoreRes)
179+
head(scoreRes)
180+
```
147181

148-
```{r creating_files5}
149-
## note required to be correct values, just correct format
150-
scoreddf$I_BAD <- as.character(scoreddf$BAD)
151-
scoreddf$BAD <- as.character(scoreddf$BAD)
152-
scoreddf$EM_EVENTPROBABILITY <- 0.1
153-
scoreddf$EM_PROBABILITY <- 0.1
154-
scoreddf$EM_CLASSIFICATION <- as.character(scoreddf$BAD)
155182

156183

157-
write_in_out_json(hmeq[,-1], input = TRUE, path = path)
158-
write_in_out_json(scoreddf[-3], input = FALSE, path = path)
184+
Now we create some additional files which are required to configure SAS Model Manager when uploading the files. For the variables specifically, should match the inputs from the model and outputs. We can use the result from the scoring code test from the previous chunck. Otherwise you can create a `data.frame` with the expected outputs and pass it.
159185

186+
```{r creating_files5}
187+
188+
write_in_out_json(hmeq[,-1], input = TRUE, path = path)
189+
write_in_out_json(scoreRes, input = FALSE, path = path)
160190
161191
write_fileMetadata_json(scoreCodeName = "scoreCode.R",
162192
scoreResource = modelrda,

0 commit comments

Comments
 (0)