Error while making prediction using GBM model and mrsdeploy package

I am facing issue while calling predict.gbm using mrsdeploy function after publishing model as service.Below are two codes, one is demo code wherein predict function for glm model works fine both locally and after publishing service but GBM code works only locally and not after publishing the model

#Error Message as below

"Error in predict.gbm(new_model, newdata, type = "response", n.trees = 2722) : could not find function "predict.gbm"answer could not be returned."


library(mrsdeploy)

library(gbm)

-----------------Demo Code works fine both locally and after publishing service--------------

carsModel <- glm(formula = am ~ hp + wt, data = mtcars, family = binomial)

#function that can use the model

manualTransmission <- function(hp, wt) {

newdata <- data.frame(hp = hp, wt = wt)

predict(carsModel, newdata, type = "response")

}

test function locally by printing results

print(manualTransmission(120, 2.8))

output=> 0.6418125

remoteLogin("localhost:12800",

        username = "admin", 

        password = "****",

        session = FALSE)

api <- publishService(

"mtService",

code = manualTransmission,

model = carsModel,

inputs = list(hp = "numeric", wt = "numeric"),

outputs = list(answer = "numeric"),

v = "v1.0.0"

)

#test model prediction using api call

result <- api$manualTransmission(120, 2.8)

print(result$output("answer"))

output=> 0.6418125

#Demo Code Ends

###################GBM Code, prediction works when model is not published.

#load model object

new_model <- readRDS("\new_model.RDS")

Produce a prediction function that can use the model

salarypred <- function(PEAAK_EMI_TS,CITY_TIER,TOTAL_SANC_AMT_CC,TIME_SINCE_FIRST_CC,MIN_LIVE_AMT_BY_TENNURE_BL_AND_P,CUSTOMER_COMPANY_CATEGORY_PL,TS_AVG_SANC_CLOSED_PL,AVG_LIVE_AMT_BY_TENNURE_THICK,MIN_SANC_LIVE_REV,AGE_AT_POS,AVG_PAID_LIVE_HL,TIME_SINCE_LAST_LIVE_CC,MIN_MOB_BL_AND_PL,TS_MIN_SANC_CLOSED_AL,MIN_LIVE_AMT_BY_TENNURE_AL,AVG_TENNURE_BL_AND_PL,RESTYPE_RENTED,GENDER

) {

newdata <- data.frame(PEAAK_EMI_TS=PEAAK_EMI_TS , CITY_TIER=CITY_TIER , TOTAL_SANC_AMT_CC=TOTAL_SANC_AMT_CC , TIME_SINCE_FIRST_CC=TIME_SINCE_FIRST_CC , MIN_LIVE_AMT_BY_TENNURE_BL_AND_P=MIN_LIVE_AMT_BY_TENNURE_BL_AND_P , CUSTOMER_COMPANY_CATEGORY_PL=CUSTOMER_COMPANY_CATEGORY_PL , TS_AVG_SANC_CLOSED_PL=TS_AVG_SANC_CLOSED_PL , AVG_LIVE_AMT_BY_TENNURE_THICK=AVG_LIVE_AMT_BY_TENNURE_THICK , MIN_SANC_LIVE_REV=MIN_SANC_LIVE_REV , AGE_AT_POS=AGE_AT_POS , AVG_PAID_LIVE_HL=AVG_PAID_LIVE_HL , TIME_SINCE_LAST_LIVE_CC=TIME_SINCE_LAST_LIVE_CC , MIN_MOB_BL_AND_PL=MIN_MOB_BL_AND_PL , TS_MIN_SANC_CLOSED_AL=TS_MIN_SANC_CLOSED_AL , MIN_LIVE_AMT_BY_TENNURE_AL=MIN_LIVE_AMT_BY_TENNURE_AL , AVG_TENNURE_BL_AND_PL=AVG_TENNURE_BL_AND_PL , RESTYPE_RENTED=RESTYPE_RENTED , GENDER=GENDER)

predict.gbm(new_model, newdata, type = "response",n.trees=2722)

}

test function locally by printing results

print(salarypred(10.61928526,'Tier1',12.16838635,166,-999,'zzzzz',-999,71,82603,42,-999,166,-999,945000,-999,-999,0,'MALE'))

output=>10.8409

remoteLogin("localhost:12800",

        username = "admin", 

        password = "*****",

        session = FALSE)

api <- publishService(

"salService",

code = salarypred,

model = new_model,

inputs = list(PEAAK_EMI_TS= "numeric",CITY_TIER= "character",TOTAL_SANC_AMT_CC= "numeric",TIME_SINCE_FIRST_CC= "numeric",MIN_LIVE_AMT_BY_TENNURE_BL_AND_P= "numeric",CUSTOMER_COMPANY_CATEGORY_PL="character",TS_AVG_SANC_CLOSED_PL= "numeric",AVG_LIVE_AMT_BY_TENNURE_THICK= "numeric",MIN_SANC_LIVE_REV= "numeric",AGE_AT_POS= "numeric",AVG_PAID_LIVE_HL= "numeric",TIME_SINCE_LAST_LIVE_CC= "numeric",MIN_MOB_BL_AND_PL= "numeric",TS_MIN_SANC_CLOSED_AL= "numeric",MIN_LIVE_AMT_BY_TENNURE_AL= "numeric",AVG_TENNURE_BL_AND_PL= "numeric",RESTYPE_RENTED= "numeric",GENDER="character"),

outputs = list(answer = "numeric"),

v = "v1.0.0"

)

#test model prediction using api call

result <- api$salarypred(10.61928526,'Tier1',12.16838635,166,-999,'zzzzz',-999,71,82603,42,-999,166,-999,945000,-999,-999,0,'MALE')

print(result$output("answer"))

output=>NULL

result[["errorMessage"]]

#Error Message as below

"Error in predict.gbm(new_model, newdata, type = "response", n.trees = 2722) : could not find function "predict.gbm"answer could not be returned."

Hi, and welcome!

Please see the FAQ: What's a reproducible example (`reprex`) and how do I create one? Using a reprex, complete with representative data will attract quicker and more answers.

Here, though, the last line has all the necessary information

could not find function "predict.gbm"

The code runs locally because the gbm package was loaded via

library(gbm

which points to the likely conclusion that it is not provisioned on the Microsoft R Server. See this operalization post for a easy way of seeing if local and remote configurations are identically configured.

Thanks for your reply @technocrat.
I forgot to mention that above code is directly run on server and by "locally" I meant running on the server but before publishing the model.

I had installed gbm package on server, and placed downloaded package in both libraries

C:\Users\11090\Documents\R\win-library\3.4
C:\Program Files\Microsoft\ML Server\R_SERVER\library .

Did not have to do anything for GLM model and the prediction works fine before and after publishing the model.

GBM prediction works only before the model is published and not via API call

1 Like

You may have better luck on a Microsoft R server oriented board. In the few years I've been active here, this is the only post I recall reading that is in that api space. (But always feel welcome for any questions related to non-api related issues in coding, particularly in the tidyverse).

The message, unless Microsoft flavored R differs considerably, is best understood as

whether or not the library is on the file system, it's not in my namespace so I can't find its predict.gbm function

1 Like

Thanks again for the reply and that was very well put in the end.
Will keep trying and update if I find any solution

1 Like

@technocrat. Could you suggest if below output helps.

I ran below code to see if the package is actually loaded and attached or not.
It says that gbm is attached

print(sessionInfo())

R version 3.4.3 (2017-11-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server x64 (build 14393)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252

attached base packages:
[1] parallel splines stats graphics grDevices utils datasets methods base

other attached packages:
[1] caret_6.0-78 ggplot2_2.2.1 gbm_2.1.3 survival_2.41-3 RevoUtilsMath_10.0.1
[6] RevoUtils_10.0.7 RevoMods_11.0.0 MicrosoftML_9.3.0 mrsdeploy_1.1.3 RevoScaleR_9.3.0
[11] lattice_0.20-35 rpart_4.1-11

loaded via a namespace (and not attached):
[1] Rcpp_0.12.14 lubridate_1.7.1 tidyr_0.7.2 class_7.3-14 assertthat_0.2.0
[6] ipred_0.9-6 psych_1.7.8 foreach_1.4.4 R6_2.2.2 plyr_1.8.4
[11] stats4_3.4.3 pillar_1.0.1 rlang_0.1.6 lazyeval_0.2.1 curl_3.1
[16] kernlab_0.9-25 Matrix_1.2-12 CVST_0.2-1 ddalpha_1.3.1 gower_0.1.2
[21] stringr_1.2.0 foreign_0.8-69 munsell_0.4.3 broom_0.4.3 compiler_3.4.3
[26] pkgconfig_2.0.1 mnormt_1.5-5 dimRed_0.1.0 nnet_7.3-12 tidyselect_0.2.3
[31] tibble_1.4.1 mrupdate_1.0.1 prodlim_1.6.1 DRR_0.0.2 codetools_0.2-15
[36] RcppRoll_0.2.2 CompatibilityAPI_1.1.0 dplyr_0.7.4 withr_2.1.1 MASS_7.3-48
[41] recipes_0.1.1 ModelMetrics_1.1.0 grid_3.4.3 nlme_3.1-131 jsonlite_1.5
[46] gtable_0.2.0 magrittr_1.5 scales_0.5.0 stringi_1.1.6 reshape2_1.4.3
[51] bindrcpp_0.2 timeDate_3042.101 robustbase_0.92-8 lava_1.5.1 iterators_1.0.9
[56] tools_3.4.3 glue_1.2.0 DEoptimR_1.0-8 purrr_0.2.4 sfsmisc_1.1-1
[61] colorspace_1.3-2 bindr_0.1

1 Like

The base packages are always loaded and gbm_2.1.3 does show up as being attached. It lags a bit from the current gbm_2.1.5, but that probably doesn't matter. Puzzling.

Out of curiosity: what happens with changing from predict.gmb to gbm::predict.gbm?

1 Like

@technocrat. you are a godsend.gbm::predict.gbm fixes the issue but I am confused now.

I distinctly remember using gbm::predict.gbm in the past and there was error while executing the code.

But in that attempt the gbm package was only inside user library and not in server's R path .
Couple of days back I copied that to R base path as well.

1 Like

I'm confused, too. An attached library's function should be fully available without needing the gbm:: prefix. Again, I know zip about the MS flavor, but I can't think of any reason that it would have decided to do things in a way that requires this.

In the *nix using vanilla R, there is a distinction between the system library that is available to all users and generally requires permissions to write to and update plus one or more libraries for users who lack the system library permission. In *nix-speak I would say that

the user under which this server process is running is accessing only the system library and not the user library in which the package for the desired library is installed into

If that were the case, then installing into the system library with the required permissions should fix it.

But I'm getting into the realms of speculation.

1 Like

Thanks for sharing above.
I will to try different permutations and combinations between gbm package placed between user and R base library path with gbm::predict.gbm

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.