---
# System prepended metadata

title: 其他R API遇到問題整理
tags: [產險]

---

# 其他R API遇到問題整理
###### tags: `產險`

[toc]

---
## gitlab repo link
- [gitlab repo | R service](https://gitlab.com/carinsdev/data-team/dat-py-dat-cm/r_server_temp)
- [gitlab repor | R service in container](https://gitlab.com/carinsdev/data-team/dat-py-dat-cm/dat-cm-rs-modx-rserver)

---
## LIAB
### :bulb: 問題1 - input格式維度 (已解決)
```
維度問題
Error in Dataset[which(Dataset$IS_LIAB == 1), ]: incorrect number of dimensions\n
```
![](https://i.imgur.com/NuIQ7Id.png)

:wrench: 想法：
```
已解決，轉為dataframe。
```
![](https://i.imgur.com/Lo5ERV3.png)

-
### :bulb: 問題2 - 找不到的function & object
```
找不到object 'datastructure'
Error in is.data.frame(x): object 'datastructure' not found\n"
```
![](https://i.imgur.com/oxdKNHU.png)

```
Rscript會使用function: "dataprocess" & "model_liab"，但此function定義內容已經被註解。
```
![](https://i.imgur.com/XfhP5nf.png)

![](https://i.imgur.com/mB0WT5o.png)

![](https://i.imgur.com/F56ojvx.png)

### :wrench: 想法：
```
1. 需要提供 datastructure.rds 檔案
2. function是從Rdata導入進來
```

### :wrench: 想法(2021/09/27)：
```
將會用到的function實作出來
```
#### LIAB_model.Rdata裡的function
![](https://i.imgur.com/pdQz9IZ.png)

#### imp_fun.Rdata裡的function
![](https://i.imgur.com/VhDt82u.png)


#### 結論：可順利取得結果
![](https://i.imgur.com/ayaKq4g.png)



---

## REPORT_CAR_RISK
### :bulb: 問題1 - Output格式
```
result是matrix, 有兩個輸出值。
```
![](https://i.imgur.com/NyjYfmb.png)
```log```
![](https://i.imgur.com/Nyrh5De.png)


:wrench: 想法
```
1. 想參考之前output範例，是否要輸出兩個result?
```

:wrench: 9/14更新: output參考
![](https://i.imgur.com/YOM1SkV.jpg)

:wrench: 9/15更新: output更新（但型態有點不同)
![](https://i.imgur.com/Pi0Fd3n.png)


#### :wrench: 9/29更新
**作法1**
```r=
response <- data.frame(
                success = "",
                errorMessage = "",
                outputParameters = "",
                outputFiles = "",
                consoleOutput = "",
                changedFiles = ""
            )
response$success <- "true"
response$outputParameters <- list(result = result) 
response$outputParameters$result$"0" <-list(as.list(result[1]))
response$outputParameters$result$"1" <- list(as.list(result[2]))
response$outputParameters <- c(response$outputParameters)
response <- c(response)
```
**回傳結果**
![](https://i.imgur.com/ZAHPD33.png)




**作法2**
差別在"response$outputParameters"的格式
```r=
response <- data.frame(
                success = "",
                errorMessage = "",
                outputParameters = "",
                outputFiles = "",
                consoleOutput = "",
                changedFiles = ""
            )
response$success <- "true"
response$outputParameters <- list(result = as.data.frame(result))
response$outputParameters$result$"0" <- list(as.list(result[1]))
response$outputParameters$result$"1" <- list(as.list(result[2]))
response$outputParameters <- c(response$outputParameters)
response <- c(response)
```
**回傳結果**
![](https://i.imgur.com/TMNy6RZ.png)


**作法3**
根據欄位順序替換value為list的值
```r=
response <- data.frame(
                success = "",
                errorMessage = "",
                outputParameters = "",
                outputFiles = "",
                consoleOutput = "",
                changedFiles = ""
            )
response$success <- "true"
response$outputParameters <- list(result = as.data.frame(result))
# 把value替換成list
for (i in 1:length(result)) {
    if (is.list(result[i])) {
        response$outputParameters$result[i] <- as.list(result[i])
    } else {
        response$outputParameters$result[i] <- list(as.list(result[i]))
    }
}
response$outputParameters$result <- c(response$outputParameters$result)
response$outputParameters <- c(response$outputParameters)
response <- c(response)
```

**回傳結果**
![](https://i.imgur.com/aZSGjXr.png)




---
## CAR_BODY
### :bulb: 問題1 - 需安裝Java環境
```
Error in .h2o.checkJava(): Cannot find Java. Please install the latest JRE from http://www.oracle.com/technetwork/java/javase/downloads/index.html
```
![](https://i.imgur.com/0JWUHIg.png)


:wrench: 想法
```
1. 正在安裝rJava & jdk
```

:wrench: 9/14更新 - server R版本 1.8
![](https://i.imgur.com/mvAWu8M.jpg)



---
## CAR_BODY2
### :bulb: 問題1 - 找不到的function(已解決)
```
Rscript使用到的function沒有在Rscript中
"Dataprocess", "Model_Car", "Imp_Car_Body"
```
![](https://i.imgur.com/FpZYfBb.png)


### :wrench: 想法
```
1. function是從Car_Body_Model.Rdata導入進來
```


### :bulb: 問題2 - 找不到object "Column_Type"
```
Error in is.data.frame(x): object 'Column_Type' not found
```

### :wrench: 處理1
```bash=
進入container查看function後
> File_path <- paste0("/src/")
> load(paste0(File_path, "/Car_Body_Model.Rdata"))
> Dataprocess
```
```r=
根據確認log, 發現停下來的部分是在
  Dataset_trun      <- Dataset[,colnames(Dataset) %in% colnames(Column_Type)]
  Dataset_trun      <- as.data.frame(rbind(Column_Type,Dataset_trun))
```
![](https://i.imgur.com/sZ8S0p4.png)


查看Column_Type object
```r=
> Column_Type
   UNIT_NO DISCOUNT_PREMIUM BUSINESS_ORIGIN COPERATE_COMPANY_NO CHANNEL_TYPE
19       1            14812             XXX                <NA>           10
   IS_CONTINUE_POLICY_NO IS_BURGLAR IS_LIAB IS_PASSENGER IS_OTHER
19                     Y          1       1            1        0
.....
   DIV_PREMIUM DIV_INSURED_CNT DIV_POLICY_CNT DIV_LOSS_RATE
19          NA              NA             NA            NA
```
![](https://i.imgur.com/vZtA1XQ.png)


查看function "Dataprocess"
```r=
> Dataprocess
function(Dataset){
  
  # Big Case
  colnames(Dataset) <- toupper(names(Dataset))
  
  # Spcial Colume
  Dataset[Dataset==""]=NA
  Dataset$COMPUTER_CALCULAT_CODE <- ifelse(Dataset$COMPUTER_CALCULAT_CODE   %in% c("1" ,"y" ,"Y") ,1,NA)
  Dataset$SPECIAL_INSRNCE_TYPE   <- ifelse(Dataset$SPECIAL_INSRNCE_TYPE%in%c("N","0"),0,1)
  Dataset$QUESTION_CAR_NO_IND    <- ifelse(Dataset$QUESTION_CAR_NO_IND == 1,"Y",ifelse(Dataset$QUESTION_CAR_NO_IND==0,"N",NA))
  Dataset$IS_NEW_CAR             <- ifelse(Dataset$IS_NEW_CAR == 1,"Y",ifelse(Dataset$IS_NEW_CAR==0,"N",NA))
  Dataset$IS_NOT_FORCE           <- ifelse(Dataset$IS_NOT_FORCE == 1,"Y",ifelse(Dataset$IS_NOT_FORCE==0,"N",NA))
  Dataset$IS_CONTINUE_POLICY_NO  <- ifelse(Dataset$IS_CONTINUE_POLICY_NO == 1,"Y",ifelse(Dataset$IS_CONTINUE_POLICY_NO==0,"N",NA)) #有等於3
  Dataset$QUESTION_ENGIN_IND     <- ifelse(Dataset$QUESTION_ENGIN_IND == 1,"Y",ifelse(Dataset$QUESTION_ENGIN_IND==0,"N",NA))
  
  # Dataset$SEX                    <- ifelse(Dataset$SEX == 1,"M",ifelse(Dataset$SEX==0,"F",NA))
  # Dataset$MAXIMUM_LOAD_UNIT      <- ifelse(Dataset$MAXIMUM_LOAD_UNIT == 1,"T",ifelse(Dataset$MAXIMUM_LOAD_UNIT==0,"P",NA))
  # Dataset$MOTOR_FLG              <- ifelse(Dataset$MOTOR_FLG == 1,"Y",ifelse(Dataset$MOTOR_FLG==0,"N",NA))
  # Dataset$IS_NOT_FORCE           <- ifelse(Dataset$IS_NOT_FORCE == 1,"Y",ifelse(Dataset$IS_NOT_FORCE==0,"N",NA))
  
  # Column Select 
  print(paste(Sys.time(), "開始欄位篩選"))
  loginfo( "開始欄位篩選", logger='process')
  
  Dataset_trun      <- Dataset[,colnames(Dataset) %in% colnames(Column_Type)]
  Dataset_trun      <- as.data.frame(rbind(Column_Type,Dataset_trun))
  
  # As.numeric
  print(paste(Sys.time(), "開始欄位型態轉換"))
  loginfo( "開始欄位型態轉換", logger='process')
  Dataset_trun <- as.data.frame(sapply(Dataset_trun,as.numeric))
  
  Factor_List  <- c('IS_CONTINUE_POLICY_NO','MAXIMUM_LOAD_UNIT','MOTOR_FLG','SEX','QUESTION_CAR_NO_IND',
                    'QUESTION_ENGIN_IND','IS_NEW_CAR','IS_NOT_FORCE')
  
  # Yes No colume Start at 0
  for(i in Factor_List){
    Dataset_trun[,i] <- Dataset_trun[,i]-1
  }
  
  Dataset_trun <- Dataset_trun[-1,]
  
  
  print(paste(Sys.time(), "完成資料處理"))
  loginfo( "完成資料處理", logger='process')
  
  return(Dataset_trun)
}
```


查看function "Model_Car"
```r=
> Model_Car
function(Dataset_trun,Dataset,thre){
  
  pred <- array(NA,c(4,dim(Dataset_trun)[1]))
  
  for(i in 1:4){
    print(paste(Sys.time(), "進行第", i, "模型檔預測"))
    loginfo( paste("進行第", i, "模型檔預測"), logger='process')
    pred[i,] <- predict(get(paste0("XG.model.CAR",i)),  xgb.DMatrix(data= data.matrix(Dataset_trun) ,missing=NA))
  }
  
  print(paste(Sys.time(), "模型預測完成"))
  loginfo("模型預測完成", logger='process')
  
  PRED_LOSS <- apply(pred,2,mean)
  result = data.frame("CONTRACT_NO"=Dataset$CONTRACT_NO,
                      "RISK_PROB"= PRED_LOSS,
                      "PRED" = ifelse( PRED_LOSS > thre , 1 , 0 ))
  return(result)
  
}
```

查看function "Imp_Car_Body"
```r=
> Imp_Car_Body
function(result,Dataset,Imp_Featrue){
  # 選取重要因子欄位
  Dataset     <- Dataset[,colnames(Dataset)%in%Imp_Featrue$variable]

  
  Imp_Car <- function(Dataset,Imp_Featrue){
    
    Dataset     <- data.frame(variable = names(Dataset),t(Dataset),t(Dataset),t(Dataset),t(Dataset),t(Dataset) ,row.names = NULL)
    colnames(Dataset)  <- c("variable","x","x1","x2","x3","x4")
    
    # 資料與百分位表 Join
    Tmp_Imp_Featrue_Comp <- merge(x = Imp_Featrue, y = Dataset, by = "variable", all.x = TRUE)
    
    # 篩選數值欄位進行百分位比對
    Tmp_Imp_Featrue <- Tmp_Imp_Featrue_Comp[Tmp_Imp_Featrue_Comp$type %in% c("numeric","integer"),]

    
    # 進行百分位比對
    Qun_Contr <- as.character(apply(matrix(mapply(function(x,y){ifelse(y>=x,1,0)},x=c(Tmp_Imp_Featrue$Q10,Tmp_Imp_Featrue$Q25,Tmp_Imp_Featrue$Q50,Tmp_Imp_Featrue$Q75,Tmp_Imp_Featrue$Q90),
                                                  y=c(Tmp_Imp_Featrue$x,Tmp_Imp_Featrue$x1,Tmp_Imp_Featrue$x2,Tmp_Imp_Featrue$x3,Tmp_Imp_Featrue$x4)),c(dim(Tmp_Imp_Featrue)[1],5)),1,sum))
    
    
    # 中文比對
    Qun_Contr_All <- cbind(Tmp_Imp_Featrue,Qun_Contr)
    Qun_Contr_All <- mapply( function(x,y){
             switch (x,
                     "0"= "低於90%的歷史案件",
                     "1"= "低於75%的歷史案件",
                     "2"= "低於50%的歷史案件",
                     "3"= "高於50%的歷史案件",
                     "4"= "高於75%的歷史案件",
                     "5"= "高於90%的歷史案件",NA)} ,x=as.character(Qun_Contr_All$Qun_Contr),y=Qun_Contr_All$sign)
    
    Contra_data <- sapply(Tmp_Imp_Featrue_Comp$type,function(x){
      ifelse(x=='factor',"風險因子","0") })
    
    # 橫打直 
    Contra_data[which(Contra_data=="0")] <- unlist(Qun_Contr_All)
    Tmp_Imp_Featrue_Comp$re <- Contra_data
    Tmp_Imp_Featrue_Comp    <- Tmp_Imp_Featrue_Comp[order(-Tmp_Imp_Featrue_Comp$percentage_abs),]
    Tmp <- data.frame("TMP" = array(NA,c(1,1)), row.names ="")
    
    for(i in 1:nrow(Tmp_Imp_Featrue_Comp)){
      Tmp_1        <- Tmp_Imp_Featrue_Comp[i,c("name",'x',"re")]
      names(Tmp_1) <- c(paste0("IMP_NAME",i),paste0("IMP_VALUE",i),paste0("IMP_QU",i))
      Tmp          <- cbind(Tmp,Tmp_1)
      row.names(Tmp)<-NULL
      
    }
    
    return(Tmp[,-1])
    
  }
  EveryRow <- apply(Dataset,1,function(x,y){Imp_Car(data.frame(t(unlist(x))),y)},Imp_Featrue)
  result   <- as.data.frame(cbind(result,do.call(rbind,EveryRow)), row.names =NULL)
  
  return(result)
  print(paste(Sys.time(), "完成重要因子計算"))
  loginfo( "完成重要因子計算", logger='process')
}
```


### :wrench: 處理2: 實作function & Column_Type
錯誤訊息
```
Error in rbind(deparse.level, ...): numbers of columns of arguments do not match
```

```r=
rbind(a,b) # rbind()：把兩個data frame，依據row合併起來
cbind(a,b) # cbind()：把兩個data frame，依據column合併起來
```

將特殊處理的狀況印出：
```r=
Dataset[Dataset == ""] <- NA
    Dataset$COMPUTER_CALCULAT_CODE <- ifelse(Dataset$COMPUTER_CALCULAT_CODE %in% c("1", "y", "Y"), 1, NA)
    Dataset$SPECIAL_INSRNCE_TYPE <- ifelse(Dataset$SPECIAL_INSRNCE_TYPE %in% c("N", "0"), 0, 1)
    Dataset$QUESTION_CAR_NO_IND <- ifelse(Dataset$QUESTION_CAR_NO_IND == 1, "Y", ifelse(Dataset$QUESTION_CAR_NO_IND == 0, "N", NA))
    Dataset$IS_NEW_CAR <- ifelse(Dataset$IS_NEW_CAR == 1, "Y", ifelse(Dataset$IS_NEW_CAR == 0, "N", NA))
    Dataset$IS_NOT_FORCE <- ifelse(Dataset$IS_NOT_FORCE == 1, "Y", ifelse(Dataset$IS_NOT_FORCE == 0, "N", NA))
    Dataset$IS_CONTINUE_POLICY_NO <- ifelse(Dataset$IS_CONTINUE_POLICY_NO == 1, "Y", ifelse(Dataset$IS_CONTINUE_POLICY_NO == 0, "N", NA)) # 有等於3
    Dataset$QUESTION_ENGIN_IND <- ifelse(Dataset$QUESTION_ENGIN_IND == 1, "Y", ifelse(Dataset$QUESTION_ENGIN_IND == 0, "N", NA))
    print(paste0("Dataset$COMPUTER_CALCULAT_CODE: ", Dataset$COMPUTER_CALCULAT_CODE))
    print(paste0("Dataset$SPECIAL_INSRNCE_TYPE: ", Dataset$SPECIAL_INSRNCE_TYPE))
    print(paste0("Dataset$QUESTION_CAR_NO_IND: ", Dataset$QUESTION_CAR_NO_IND))
    print(paste0("Dataset$IS_NEW_CAR: ", Dataset$IS_NEW_CAR))
    print(paste0("Dataset$IS_NOT_FORCE: ", Dataset$IS_NOT_FORCE))
    print(paste0("Dataset$IS_CONTINUE_POLICY_NO: ", Dataset$IS_CONTINUE_POLICY_NO))
    print(paste0("Dataset$QUESTION_ENGIN_IND: ", Dataset$QUESTION_ENGIN_IND))
```
![](https://i.imgur.com/5Q3AkZw.png)


將rbind前的Column_Type & Dataset_trun印出：
```r=
  print(paste0("Column_Type: ", Column_Type))
    print(paste0("names$Column_Type: ", names$Column_Type))
    print(paste0("Dataset_trun: ", Dataset_trun))
    print(paste0("names$Dataset_trun: ", names$Dataset_trun))
    Dataset_trun <- as.data.frame(rbind(Column_Type, Dataset_trun))
```

Column_Type有，Dataset_trun沒有：
```r=
IS_CONTINUE_POLICY_NO
IS_NEW_CAR
QUESTION_CAR_NO_IND
QUESTION_ENGIN_IND
IS_NOT_FORCE
OMPLEX_IND_L3Y
```

Dataset_trun有，Column_Type沒有：
```r=
FORCE_INJURE_IND_L3Y
DIV_INSURE
DIV_PREMIUM
DIV_INSURED_CNT
DIV_POLICY_CNT
DIV_LOSS_RATE
```


### :wrench: 處理3: load Rdata後printout function
```r=
load(paste0(File_path, "/Car_Body_Model.Rdata"), car_body_model <- new.env())
print(car_body_model$Column_Type)
print(ls(car_body_model))

```
![](https://i.imgur.com/l5Rvux6.png)

結果：
![](https://i.imgur.com/nPDJCG8.png)



## plumber input handling
ref: [plumber input-handling](https://www.rplumber.io/articles/routing-and-input.html#input-handling)

ref: [equivalent-programmatic-usage-1](
https://www.rplumber.io/articles/annotations.html#equivalent-programmatic-usage-1)

### Equivalent programmatic usage
```r=
text_handler <- function(name, age) { sprintf("%s is %i years old", name, max(age)) }
qp_handler <- function(name, age) { age <- as.integer(age); text_handler(name, age) }
file_handler <- function(file) { as_attachment(file[[1]], names(file)[1]) }
pr() %>%
  pr_get(path = "/query/parameters",
         handler = qp_handler,
         serializer = serializer_text(),
         params = list("name" = list(type = "string", required = FALSE, isArray = FALSE),
                       "age" = list(type = "integer", required = FALSE, isArray = TRUE))) %>%
  pr_get(path = "/dyn/<name:str>/<age:[int]>/route",
         handler = text_handler,
         serializer = serializer_text(),
         parsers = "none",
         responses = list("200" = list(description = "A sentence"))) %>%
  pr_post(path = "/upload_file",
          handler = file_handler,
          serializer = serializer_rds(),
          parsers = c("multi", "rds"),
          params = list("file" = list(type = "file", desc = "A file", required = FALSE, isArray = FALSE)),
          comments = "Upload an rds file and return the object")
```

ref: [pass a json dataframe to plumber API](https://community.rstudio.com/t/should-one-pass-a-json-dataframe-to-plumber-api-instead-of-feeding-it-by-rows-if-yes-how-to-do-so/73305)

- example
```r=
library(plumber)
library(xgboost)
model <- readRDS("model.RDS")
sample_dt <- readRDS("sample_dt.RDS")

#* Predict using model
#* @param dt:list
#* @post /predict
function(dt = sample_dt) {
  predict(model, new_data = dt)
}
```

實作:
```r=
#* Return the predict result
#* @param dataset:list
#* @serializer unboxedJSON
#* @post /car_body2
car_body2 <- function(dataset) {
    out <- tryCatch(
        {
            print(paste(Sys.time(), "start to Processed Data..."))
            loginfo(toJSON(dataset), logger = "input")
            print(class(dataset))
```

錯誤訊息:
```r=
Error in plumberToSwaggerType(nameType[1, 3]) : Unrecognized type: list
```

結論：
pass這個選項。

### :wrench: 處理4: 實作Rdata內的function & Column_Type & 將return結果改為toJSON


原始R service結果：
gitlab: [output_car_bpdy2.json](https://gitlab.com/carinsdev/data-team/dat-py-dat-cm/r_server_temp/-/blob/main/output_car_bpdy2.json)
```json=
{
    "success": true,
    "errorMessage": "",
    "outputParameters": {
        "result": {
            "CONTRACT_NO": [
                "1410CA0064600486"
            ],
            "RISK_PROB": [
                -0.033508055843412876
            ],
            "PRED": [
                0
            ],
            "IMP_NAME1": [
                "車款名稱"
            ],
            "IMP_VALUE1": [
                "無"
            ],
            "IMP_QU1": [
                "風險因子"
            ],
            "IMP_NAME2": [
                "廠牌中文名稱"
            ],
            "IMP_VALUE2": [
                "Sym三陽(國產)"
            ],
            "IMP_QU2": [
                "風險因子"
            ],
            "IMP_NAME3": [
                "營業種類"
            ],
            "IMP_VALUE3": [
                "一般職業"
            ],
            "IMP_QU3": [
                "風險因子"
            ],
            "IMP_NAME4": [
                "車體險保費"
            ],
            "IMP_VALUE4": [
                "0"
            ],
            "IMP_QU4": [
                "低於90%的歷史案件"
            ],
            "IMP_NAME5": [
                "輔導單位代號"
            ],
            "IMP_VALUE5": [
                "CLA100A"
            ],
            "IMP_QU5": [
                "風險因子"
            ],
            "IMP_NAME6": [
                "輔導單位理賠次數"
            ],
            "IMP_VALUE6": [
                "2314"
            ],
            "IMP_QU6": [
                "低於90%的歷史案件"
            ],
            "IMP_NAME7": [
                "經紀人代號"
            ],
            "IMP_VALUE7": [
                "X3"
            ],
            "IMP_QU7": [
                "風險因子"
            ],
            "IMP_NAME8": [
                "被保人過去三年投保乘客險數量"
            ],
            "IMP_VALUE8": [
                "2"
            ],
            "IMP_QU8": [
                "高於50%的歷史案件"
            ],
            "IMP_NAME9": [
                "近五年總理賠次數"
            ],
            "IMP_VALUE9": [
                "0"
            ],
            "IMP_QU9": [
                "高於75%的歷史案件"
            ],
            "IMP_NAME10": [
                "其他險保費"
            ],
            "IMP_VALUE10": [
                "0"
            ],
            "IMP_QU10": [
                "高於50%的歷史案件"
            ],
            "IMP_NAME11": [
                "排氣量"
            ],
            "IMP_VALUE11": [
                "124"
            ],
            "IMP_QU11": [
                "低於90%的歷史案件"
            ],
            "IMP_NAME12": [
                "被保人過去三年投保竊盜險數量"
            ],
            "IMP_VALUE12": [
                "2"
            ],
            "IMP_QU12": [
                "高於50%的歷史案件"
            ],
            "IMP_NAME13": [
                "第三人責任險保費"
            ],
            "IMP_VALUE13": [
                "590"
            ],
            "IMP_QU13": [
                "低於75%的歷史案件"
            ],
            "IMP_NAME14": [
                "近五年其他險理賠金額(從車角度)"
            ],
            "IMP_VALUE14": [
                "0"
            ],
            "IMP_QU14": [
                "高於90%的歷史案件"
            ],
            "IMP_NAME15": [
                "被保人過去三年投保強制險數量"
            ],
            "IMP_VALUE15": [
                "3"
            ],
            "IMP_QU15": [
                "高於75%的歷史案件"
            ]
        }
    },
    "outputFiles": {},
    "consoleOutput": "[1] \"2021-09-16 17:19:55 輸入資料維度 1:236\"\n[1] \"2021-09-16 17:19:55 開始資料處理\"\n[1] \"2021-09-16 17:19:55 開始欄位篩選\"\n[1] \"2021-09-16 17:19:55 開始欄位型態轉換\"\n[1] \"2021-09-16 17:19:55 完成資料處理\"\n[1] \"2021-09-16 17:19:55 開始模型預測\"\n[1] \"2021-09-16 17:19:55 進行第 1 模型檔預測\"\n[1] \"2021-09-16 17:19:55 進行第 2 模型檔預測\"\n[1] \"2021-09-16 17:19:55 進行第 3 模型檔預測\"\n[1] \"2021-09-16 17:19:55 進行第 4 模型檔預測\"\n[1] \"2021-09-16 17:19:55 模型預測完成\"\n[1] \"2021-09-16 17:19:55 開始重要因子計算\"\n[1] \"2021-09-16 17:19:55 腳本結束 1 筆資料共耗時 0 秒\"\n",
    "changedFiles": []
}
```


2021/09/16執行結果：
![](https://i.imgur.com/QXwNL8g.png)



2021/09/17 調整output:
- 發現在轉換成dataframe之前，是希望的結果


- 轉換為dataframe之後，return值會有問題

- 需要手動調整結果


2021/09/23 調整path:
```r=
# File_path <- paste0("/opt/microsoft/mlserver/9.3.0/o16n/rserve/model/car_body2")
File_path <- getwd()
File_path_Log <- paste0(File_path, "/CAR_info_", Sys.Date(), ".log")
```

但還是無法出現'<simpleError in is.data.frame(x): object 'Column_Type' not found>'錯誤
![](https://i.imgur.com/HsLSmmN.png)


2021/09/24 重新實作finction:
```r=
load(paste0("./", "Car_Body_Model.Rdata"), car_body_model <- new.env())


# Column_Type
Column_Type <- '[{"UNIT_NO":"1","DISCOUNT_PREMIUM":14812,"BUSINESS_ORIGIN":"XXX","CHANNEL_TYPE":10,"IS_CONTINUE_POLICY_NO":"Y","IS_BURGLAR":1,"IS_LIAB":1,"IS_PASSENGER":1,"IS_OTHER":0,"CATEGORY_KIND_CNT":4,"INSRNCE_AMOUNT_COMPLEX":410000,"INSRNCE_AMOUNT_BURGLAR":410000,"INSRNCE_AMOUNT_LIAB":110499999,"INSRNCE_AMOUNT_PASSENGER":1000000,"INSRNCE_AMOUNT_OTHER":0,"INSRNCE_AMOUNT_SUM":112319999,"PREMIUM_COMPLEX":6925,"PREMIUM_BURGLAR":1755,"PREMIUM_LIAB":5811,"PREMIUM_PASSENGER":321,"PREMIUM_OTHER":0,"COMPLEX_MAIN":"3","COMPLEX_RECOVERY":0,"COMPLEX_UNCLEAR":0,"COMPLEX_WEATHER":0,"COMPLEX_HUMAN":0,"COMPLEX_DEPRECIATION":0,"COMPLEX_MORTO":0,"COMPLEX_MORTOSTEAL":0,"BURGLAR_CAR":1,"BURGLAR_DEPRECIATION":0,"BURGLAR_CARFEE":0,"BURGLAR_MORTO":0,"BURGLAR_HIGHPRICE":0,"LIAB_BODY":1,"LIAB_FINANCE":1,"LIAB_MULTPEOPLE":1,"LIAB_CONSOLATION":0,"LIAB_PASSENGER":1,"LIAB_ALCOHOL":0,"PASSENGER_DRIVE":1,"PASSENGER_BODY":0,"OTHER_CARFEE":0,"OTHER_ROADSERVICE":0,"AGENT_DIV_NO":"AL422","COUNSEL_DIV_NO":"AGA100A","BROKER_NO":"X3","AGENT_KIND":"X3","CUSTOMER_TYPE":"1","IDNTFCTN_TYPE":1,"SEX":"F","AGE":56,"MARRIAGE":1,"BUSINESS_TYPE":"一般職業","OCCU_TYPE1_CODE":"00","ORIGIN_ISSUE_DUR":2,"CAR_AGE":2,"MODEL_FULL_NO":"03231100","VEHICLE_KIND_NO":"22","KIND_NO":"客貨兩用車","ENGINE_EXHAUST":2300,"ENGINE_EXHAUST_UNIT":"1","MAXIMUM_LOAD":5,"MAXIMUM_LOAD_UNIT":"P","REPLACE_PRICE":729000,"IS_ADD_OUTFIT":0,"BODY_NO":8,"BODY_PREMIUM_RATE":1.1331,"LCNR_NO":8,"LCNR_PREMIUM_RATE":0.6338,"AGE_RATE":0.9,"BODY_CLAIM_TIMES1":0,"BODY_CLAIM_TIMES2":0,"BODY_CLAIM_TIMES3":0,"BODY_CLAIM_CODE":-1,"BODY_CLAIM_RATE":-0.2,"LIAB_CLAIM_TIMES1":0,"LIAB_CLAIM_TIMES2":0,"LIAB_CLAIM_TIMES3":0,"LIAB_CLAIM_CODE":6,"LIAB_CLAIM_RATE":0.2,"SPECIAL_INSRNCE_TYPE":"1","COMPUTER_CALCULAT_CODE":"Y","INSURE_BODY":1,"SHORT_PREMIUM_RATE":1,"ACCEPT_OFFICER":"G220330642","DEPRECIA_RATE":0.25,"TAXI_PRENOCLAIM_YEAR":0,"TAXI_PRENOTOTAL_CLAIM":0,"TAXI_PRE_FACTOR":0,"IS_NEW_CAR":"N","QUESTION_CAR_NO_IND":"N","QUESTION_ENGIN_IND":"N","VEHICLE_KIND_NAME":"客貨兩用車","USE_TYPE":1,"VEHICLE_SIZE":1,"IS_NOT_FORCE":"N","MODEL_SUB_NAME":"Escape","MODEL_NO":"03","MODEL_NAME":"福特(Ford)","ORIGIN_CODE":1,"MOTOR_FLG":"N","RISK_SCORE":0.159,"RISK_LEVEL":0,"SUSPECT_LEVEL":"L","COMPLEX_CNT_L3Y":3,"BURGLAR_CNT_L3Y":3,"LIAB_CNT_L3Y":3,"PASSENGER_CNT_L3Y":3,"OTHERS_CNT_L3Y":1,"FORCE_CNT_L3Y":0,"FORCE_INJURE_CNT_L3Y":0,"COMPLEX_IND_L3Y":1,"BURGLAR_IND_L3Y":1,"LIAB_IND_L3Y":1,"PASSENGER_IND_L3Y":1,"OTHERS_IND_L3Y":1,"INSURE_CAUSE_L1Y":0,"INSURE_CAUSE_L3Y":0,"INSURE_CAUSE_L5Y":0,"INSURE_COMPLEX_L1Y":21650,"INSURE_BURGLAR_L1Y":0,"INSURE_LIAB_L1Y":16460,"INSURE_PASSENGER_L1Y":0,"INSURE_OTHERS_L1Y":0,"INSURE_FORCE_L1Y":0,"INSURE_FORCE_INJURE_L1Y":0,"INSURE_L1Y":38110,"INSURE_COMPLEX_L3Y":21650,"INSURE_BURGLAR_L3Y":0,"INSURE_LIAB_L3Y":16460,"INSURE_PASSENGER_L3Y":0,"INSURE_OTHERS_L3Y":0,"INSURE_FORCE_L3Y":0,"INSURE_FORCE_INJURE_L3Y":0,"INSURE_L3Y":38110,"INSURE_COMPLEX_L5Y":21650,"INSURE_BURGLAR_L5Y":0,"INSURE_LIAB_L5Y":16460,"INSURE_PASSENGER_L5Y":0,"INSURE_OTHERS_L5Y":0,"INSURE_FORCE_L5Y":0,"INSURE_FORCE_INJURE_L5Y":0,"INSURE_L5Y":38110,"INSURE_COMPLEX_CNT_L1Y":1,"INSURE_BURGLAR_CNT_L1Y":0,"INSURE_LIAB_CNT_L1Y":1,"INSURE_PASSENGER_CNT_L1Y":0,"INSURE_OTHERS_CNT_L1Y":0,"INSURE_FORCE_CNT_L1Y":0,"INSURE_FORCE_INJURE_CNT_L1Y":0,"INSURE_CNT_L1Y":2,"INSURE_COMPLEX_CNT_L3Y":1,"INSURE_BURGLAR_CNT_L3Y":0,"INSURE_LIAB_CNT_L3Y":1,"INSURE_PASSENGER_CNT_L3Y":0,"INSURE_OTHERS_CNT_L3Y":0,"INSURE_FORCE_CNT_L3Y":0,"INSURE_FORCE_INJURE_CNT_L3Y":0,"INSURE_CNT_L3Y":2,"INSURE_COMPLEX_CNT_L5Y":1,"INSURE_BURGLAR_CNT_L5Y":0,"INSURE_LIAB_CNT_L5Y":1,"INSURE_PASSENGER_CNT_L5Y":0,"INSURE_OTHERS_CNT_L5Y":0,"INSURE_FORCE_CNT_L5Y":0,"INSURE_FORCE_INJURE_CNT_L5Y":0,"INSURE_CNT_L5Y":2,"CAR_INSURE_COMPLEX_L1Y":21650,"CAR_INSURE_BURGLAR_L1Y":0,"CAR_INSURE_LIAB_L1Y":16460,"CAR_INSURE_PASSENGER_L1Y":0,"CAR_INSURE_OTHERS_L1Y":0,"CAR_INSURE_FORCE_L1Y":0,"CAR_INSURE_FORCE_INJURE_L1Y":0,"CAR_INSURE_L1Y":38110,"CAR_INSURE_COMPLEX_L3Y":21650,"CAR_INSURE_BURGLAR_L3Y":0,"CAR_INSURE_LIAB_L3Y":16460,"CAR_INSURE_PASSENGER_L3Y":0,"CAR_INSURE_OTHERS_L3Y":0,"CAR_INSURE_FORCE_L3Y":0,"CAR_INSURE_FORCE_INJURE_L3Y":0,"CAR_INSURE_L3Y":38110,"CAR_INSURE_COMPLEX_L5Y":21650,"CAR_INSURE_BURGLAR_L5Y":0,"CAR_INSURE_LIAB_L5Y":16460,"CAR_INSURE_PASSENGER_L5Y":0,"CAR_INSURE_OTHERS_L5Y":0,"CAR_INSURE_FORCE_L5Y":0,"CAR_INSURE_FORCE_INJURE_L5Y":0,"CAR_INSURE_L5Y":38110,"CAR_INSURE_COMPLEX_CNT_L1Y":1,"CAR_INSURE_BURGLAR_CNT_L1Y":0,"CAR_INSURE_LIAB_CNT_L1Y":1,"CAR_INSURE_PASSENGER_CNT_L1Y":0,"CAR_INSURE_OTHERS_CNT_L1Y":0,"CAR_INSURE_FORCE_CNT_L1Y":0,"CAR_INSURE_FORCE_INJURE_CNT_L1Y":0,"CAR_INSURE_CNT_L1Y":2,"CAR_INSURE_COMPLEX_CNT_L3Y":1,"CAR_INSURE_BURGLAR_CNT_L3Y":0,"CAR_INSURE_LIAB_CNT_L3Y":1,"CAR_INSURE_PASSENGER_CNT_L3Y":0,"CAR_INSURE_OTHERS_CNT_L3Y":0,"CAR_INSURE_FORCE_CNT_L3Y":0,"CAR_INSURE_FORCE_INJURE_CNT_L3Y":0,"CAR_INSURE_CNT_L3Y":2,"CAR_INSURE_COMPLEX_CNT_L5Y":1,"CAR_INSURE_BURGLAR_CNT_L5Y":0,"CAR_INSURE_LIAB_CNT_L5Y":1,"CAR_INSURE_PASSENGER_CNT_L5Y":0,"CAR_INSURE_OTHERS_CNT_L5Y":0,"CAR_INSURE_FORCE_CNT_L5Y":0,"CAR_INSURE_FORCE_INJURE_CNT_L5Y":0,"CAR_INSURE_CNT_L5Y":2,"IS_TRADE_FORCE":1,"IS_LIMIT_DRIVER":1,"IS_CHANGE_CARNO":0,"INTRDUCE_KIND":"2","RELATION_TYPE":"1"}]'
Column_Type <- fromJSON(Column_Type)
Column_Type <- as.data.frame(Column_Type)


# XG.model.CAR1, XG.model.CAR2, XG.model.CAR3, XG.model.CAR4
XG.model.CAR1 <- car_body_model$XG.model.CAR1
XG.model.CAR2 <- car_body_model$XG.model.CAR2
XG.model.CAR3 <- car_body_model$XG.model.CAR3
XG.model.CAR4 <- car_body_model$XG.model.CAR4
Imp_Featrue <- car_body_model$Imp_Featrue


# function: Dataprocess, Model_Car, Imp_Car_Body
Dataprocess <- function(Dataset) {
...
}

Model_Car <- function(Dataset_trun, Dataset, thre) {
...
}

Imp_Car_Body <- function(result, Dataset, Imp_Featrue) {
...
}
```

server log:
![](https://i.imgur.com/4umBcgZ.jpg)


目前log:
![](https://i.imgur.com/I1PW9Sv.png)


### :wrench: 想法: 回傳結果data type: data.frame，分成不同欄位串成list後回傳結果

```r=
print(result$CONTRACT_NO)
```
結果：
![](https://i.imgur.com/v0VZbca.png)



### :wrench: 將output結果轉為list後, insert到dataframe的結果

結果：
![](https://i.imgur.com/cP5h8rK.png)


- carbody2: output
```json=
{
    "success": "true",
    "errorMessage": "",
    "outputParameters": {
        "result": {
            "CONTRACT_NO": [
                "1410CA0064600486",
                "1701CA0004634851",
                "1606CA0055851896",
                "1605CA0054210972",
                "1207CA0016027165",
                "1405CA0028892450",
                "1507CA0073289341",
                "1211CA0036339797",
                "1302CA0056335831",
                "1301CA0051649631"
            ],
            "RISK_PROB": [
                3.8394,
                4.2414,
                -81246.0859,
                -81244.998,
                -3.4988,
                1214150.9375,
                4.0531,
                -63191.5762,
                -9029.3033,
                4.3039
            ],
            "PRED": [
                1,
                1,
                0,
                0,
                0,
                1,
                1,
                0,
                0,
                1
            ],
            "IMP_NAME1": [
                "車款名稱",
                "車款名稱",
                "車款名稱",
                "車款名稱",
                "車款名稱",
                "車款名稱",
                "車款名稱",
                "車款名稱",
                "車款名稱",
                "車款名稱"
            ],
            "IMP_VALUE1": [
                "無",
                "無",
                "NA",
                "NA",
                "NA",
                "300系",
                "無",
                "NA",
                "NA",
                "無"
            ],
            "IMP_QU1": [
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子"
            ],
            "IMP_NAME2": [
                "廠牌中文名稱",
                "廠牌中文名稱",
                "廠牌中文名稱",
                "廠牌中文名稱",
                "廠牌中文名稱",
                "廠牌中文名稱",
                "廠牌中文名稱",
                "廠牌中文名稱",
                "廠牌中文名稱",
                "廠牌中文名稱"
            ],
            "IMP_VALUE2": [
                "Sym三陽(國產)",
                "Sym三陽(國產)",
                "國瑞(Toyota)",
                "國瑞(Toyota)",
                "現代(Hyundai)",
                "Benz賓士(德國 )",
                "Kymco光陽(國產)",
                "中華(Mitsubishi)",
                "裕隆(Nissan)",
                "YAMAHA山葉(國產)"
            ],
            "IMP_QU2": [
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子"
            ],
            "IMP_NAME3": [
                "營業種類",
                "營業種類",
                "營業種類",
                "營業種類",
                "營業種類",
                "營業種類",
                "營業種類",
                "營業種類",
                "營業種類",
                "營業種類"
            ],
            "IMP_VALUE3": [
                "一般職業",
                "一般職業",
                "一般職業",
                "一般職業",
                "一般職業",
                "一般職業",
                "一般職業",
                "家庭管理",
                "一般職業",
                "一般職業"
            ],
            "IMP_QU3": [
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子"
            ],
            "IMP_NAME4": [
                "車體險保費",
                "車體險保費",
                "車體險保費",
                "車體險保費",
                "車體險保費",
                "車體險保費",
                "車體險保費",
                "車體險保費",
                "車體險保費",
                "車體險保費"
            ],
            "IMP_VALUE4": [
                "0",
                "0",
                "0",
                "0",
                "0",
                "1680",
                "0",
                "0",
                "0",
                "0"
            ],
            "IMP_QU4": [
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件"
            ],
            "IMP_NAME5": [
                "輔導單位代號",
                "輔導單位代號",
                "輔導單位代號",
                "輔導單位代號",
                "輔導單位代號",
                "輔導單位代號",
                "輔導單位代號",
                "輔導單位代號",
                "輔導單位代號",
                "輔導單位代號"
            ],
            "IMP_VALUE5": [
                "CLA100A",
                "CCA0000",
                "DEA0000",
                "61A0102",
                "CLA0000",
                "CNA0000",
                "CJA100A",
                "CIA0000",
                "BMA0000",
                "BEA0000"
            ],
            "IMP_QU5": [
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子"
            ],
            "IMP_NAME6": [
                "輔導單位理賠次數",
                "輔導單位理賠次數",
                "輔導單位理賠次數",
                "輔導單位理賠次數",
                "輔導單位理賠次數",
                "輔導單位理賠次數",
                "輔導單位理賠次數",
                "輔導單位理賠次數",
                "輔導單位理賠次數",
                "輔導單位理賠次數"
            ],
            "IMP_VALUE6": [
                "2314",
                "3530",
                "2109",
                "47",
                "2747",
                "3471",
                "1616",
                "3071",
                "2189",
                "3106"
            ],
            "IMP_QU6": [
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件"
            ],
            "IMP_NAME7": [
                "經紀人代號",
                "經紀人代號",
                "經紀人代號",
                "經紀人代號",
                "經紀人代號",
                "經紀人代號",
                "經紀人代號",
                "經紀人代號",
                "經紀人代號",
                "經紀人代號"
            ],
            "IMP_VALUE7": [
                "X3",
                "X3",
                "X3",
                "X8",
                "X3",
                "X3",
                "X3",
                "6L",
                "X3",
                "X3"
            ],
            "IMP_QU7": [
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子"
            ],
            "IMP_NAME8": [
                "被保人過去三年投保乘客險數量",
                "被保人過去三年投保乘客險數量",
                "被保人過去三年投保乘客險數量",
                "被保人過去三年投保乘客險數量",
                "被保人過去三年投保乘客險數量",
                "被保人過去三年投保乘客險數量",
                "被保人過去三年投保乘客險數量",
                "被保人過去三年投保乘客險數量",
                "被保人過去三年投保乘客險數量",
                "被保人過去三年投保乘客險數量"
            ],
            "IMP_VALUE8": [
                "2",
                "0",
                "2",
                "1",
                "0",
                "1",
                "3",
                "0",
                "0",
                "1"
            ],
            "IMP_QU8": [
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於75%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件"
            ],
            "IMP_NAME9": [
                "近五年總理賠次數",
                "近五年總理賠次數",
                "近五年總理賠次數",
                "近五年總理賠次數",
                "近五年總理賠次數",
                "近五年總理賠次數",
                "近五年總理賠次數",
                "近五年總理賠次數",
                "近五年總理賠次數",
                "近五年總理賠次數"
            ],
            "IMP_VALUE9": [
                "0",
                "1",
                "0",
                "1",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0"
            ],
            "IMP_QU9": [
                "高於75%的歷史案件",
                "高於75%的歷史案件",
                "高於75%的歷史案件",
                "高於75%的歷史案件",
                "高於75%的歷史案件",
                "高於75%的歷史案件",
                "高於75%的歷史案件",
                "高於75%的歷史案件",
                "高於75%的歷史案件",
                "高於75%的歷史案件"
            ],
            "IMP_NAME10": [
                "其他險保費",
                "其他險保費",
                "其他險保費",
                "其他險保費",
                "其他險保費",
                "其他險保費",
                "其他險保費",
                "其他險保費",
                "其他險保費",
                "其他險保費"
            ],
            "IMP_VALUE10": [
                "0",
                "0",
                "0",
                "0",
                "0",
                "750",
                "0",
                "0",
                "0",
                "0"
            ],
            "IMP_QU10": [
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件"
            ],
            "IMP_NAME11": [
                "排氣量",
                "排氣量",
                "排氣量",
                "排氣量",
                "排氣量",
                "排氣量",
                "排氣量",
                "排氣量",
                "排氣量",
                "排氣量"
            ],
            "IMP_VALUE11": [
                "124",
                "124",
                "1497",
                "1781",
                "999",
                "3200",
                "124",
                "1597",
                "1995",
                "124"
            ],
            "IMP_QU11": [
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件"
            ],
            "IMP_NAME12": [
                "被保人過去三年投保竊盜險數量",
                "被保人過去三年投保竊盜險數量",
                "被保人過去三年投保竊盜險數量",
                "被保人過去三年投保竊盜險數量",
                "被保人過去三年投保竊盜險數量",
                "被保人過去三年投保竊盜險數量",
                "被保人過去三年投保竊盜險數量",
                "被保人過去三年投保竊盜險數量",
                "被保人過去三年投保竊盜險數量",
                "被保人過去三年投保竊盜險數量"
            ],
            "IMP_VALUE12": [
                "2",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0"
            ],
            "IMP_QU12": [
                "高於50%的歷史案件",
                "低於50%的歷史案件",
                "低於50%的歷史案件",
                "低於50%的歷史案件",
                "低於50%的歷史案件",
                "低於50%的歷史案件",
                "低於50%的歷史案件",
                "低於50%的歷史案件",
                "低於50%的歷史案件",
                "低於50%的歷史案件"
            ],
            "IMP_NAME13": [
                "第三人責任險保費",
                "第三人責任險保費",
                "第三人責任險保費",
                "第三人責任險保費",
                "第三人責任險保費",
                "第三人責任險保費",
                "第三人責任險保費",
                "第三人責任險保費",
                "第三人責任險保費",
                "第三人責任險保費"
            ],
            "IMP_VALUE13": [
                "590",
                "614",
                "4087",
                "1769",
                "6438",
                "2501",
                "938",
                "3217",
                "1748",
                "679"
            ],
            "IMP_QU13": [
                "低於75%的歷史案件",
                "低於75%的歷史案件",
                "低於75%的歷史案件",
                "低於75%的歷史案件",
                "低於75%的歷史案件",
                "低於75%的歷史案件",
                "低於75%的歷史案件",
                "低於75%的歷史案件",
                "低於75%的歷史案件",
                "低於75%的歷史案件"
            ],
            "IMP_NAME14": [
                "近五年其他險理賠金額(從車角度)",
                "近五年其他險理賠金額(從車角度)",
                "近五年其他險理賠金額(從車角度)",
                "近五年其他險理賠金額(從車角度)",
                "近五年其他險理賠金額(從車角度)",
                "近五年其他險理賠金額(從車角度)",
                "近五年其他險理賠金額(從車角度)",
                "近五年其他險理賠金額(從車角度)",
                "近五年其他險理賠金額(從車角度)",
                "近五年其他險理賠金額(從車角度)"
            ],
            "IMP_VALUE14": [
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0"
            ],
            "IMP_QU14": [
                "高於90%的歷史案件",
                "高於90%的歷史案件",
                "高於90%的歷史案件",
                "高於90%的歷史案件",
                "高於90%的歷史案件",
                "高於90%的歷史案件",
                "高於90%的歷史案件",
                "高於90%的歷史案件",
                "高於90%的歷史案件",
                "高於90%的歷史案件"
            ],
            "IMP_NAME15": [
                "被保人過去三年投保強制險數量",
                "被保人過去三年投保強制險數量",
                "被保人過去三年投保強制險數量",
                "被保人過去三年投保強制險數量",
                "被保人過去三年投保強制險數量",
                "被保人過去三年投保強制險數量",
                "被保人過去三年投保強制險數量",
                "被保人過去三年投保強制險數量",
                "被保人過去三年投保強制險數量",
                "被保人過去三年投保強制險數量"
            ],
            "IMP_VALUE15": [
                "3",
                "2",
                "3",
                "6",
                "0",
                "1",
                "3",
                "0",
                "0",
                "1"
            ],
            "IMP_QU15": [
                "高於75%的歷史案件",
                "高於75%的歷史案件",
                "高於75%的歷史案件",
                "高於75%的歷史案件",
                "低於50%的歷史案件",
                "高於50%的歷史案件",
                "高於75%的歷史案件",
                "低於50%的歷史案件",
                "低於50%的歷史案件",
                "高於50%的歷史案件"
            ]
        }
    },
    "outputFiles": "",
    "consoleOutput": "",
    "changedFiles": ""
}
```



- 2021/09/27
- ## CAR_BODY2
### :bulb: 問題 - 回傳數值不同

原始回傳結果：
![](https://i.imgur.com/vgVQp21.jpg)


警告訊息：
```r=
warning message “NAs introduced by coercion” when using the as.numeric function in R. Let’s apply the as.numeric function to our example vector:
```

### :wrench: 想法： 確認ifelse回傳內容
![](https://i.imgur.com/G2cF097.png)


**參考：[R Warning Message: NAs Introduced by Coercion (Example)](https://statisticsglobe.com/warning-message-nas-introduced-by-coercion-in-r)**

**當字串無法轉為數值時，會出現這個警告**
Example 1: Reproduce the Warning Message: NAs Introduced by Coercion
In this example, I’ll show how to replicate the warning message “NAs introduced by coercion” when using the as.numeric function in R. Let’s apply the as.numeric function to our example vector:
```r=
as.numeric(vec)                                        # Applying as.numeric function
# [1]   50  200   NA   10 1200   NA
# Warning message:
# NAs introduced by coercion
```
As you can see, the warning message “NAs introduced by coercion” is returned and some output values are NA (i.e. missing data or not available data).

The reason for this is that some of the character strings are not properly formatted numbers and hence cannot be converted to the numeric class.*

#### 檢查後：發現結果無異常
![](https://i.imgur.com/Ldyc53P.png)
![](https://i.imgur.com/3g5Zm9g.png)


#### 檢查2：轉換成numeric時狀況
![](https://i.imgur.com/WkOluu4.png)

發現有回傳NA的欄位數 = warning出現的欄位數



### :wrench: 解決方式：將Column_Type引用進來
```r
Column_Type <- car_body_model$Column_Type
```

回傳結果:
```json=
{
    "success": "true",
    "errorMessage": "",
    "outputParameters": {
        "result": {
            "CONTRACT_NO": [
                "1410CA0064600486",
                "1701CA0004634851",
                "1606CA0055851896",
                "1605CA0054210972",
                "1207CA0016027165",
                "1405CA0028892450",
                "1507CA0073289341",
                "1211CA0036339797",
                "1302CA0056335831",
                "1301CA0051649631"
            ],
            "RISK_PROB": [
                0.0063,
                0.0149,
                0.3984,
                0.4497,
                0.326,
                0.4878,
                0.2712,
                0.0383,
                0.1172,
                0.3365
            ],
            "PRED": [
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0
            ],
            "IMP_NAME1": [
                "車款名稱",
                "車款名稱",
                "車款名稱",
                "車款名稱",
                "車款名稱",
                "車款名稱",
                "車款名稱",
                "車款名稱",
                "車款名稱",
                "車款名稱"
            ],
            "IMP_VALUE1": [
                "無",
                "無",
                "NA",
                "NA",
                "NA",
                "300系",
                "無",
                "NA",
                "NA",
                "無"
            ],
            "IMP_QU1": [
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子"
            ],
            "IMP_NAME2": [
                "廠牌中文名稱",
                "廠牌中文名稱",
                "廠牌中文名稱",
                "廠牌中文名稱",
                "廠牌中文名稱",
                "廠牌中文名稱",
                "廠牌中文名稱",
                "廠牌中文名稱",
                "廠牌中文名稱",
                "廠牌中文名稱"
            ],
            "IMP_VALUE2": [
                "Sym三陽(國產)",
                "Sym三陽(國產)",
                "國瑞(Toyota)",
                "國瑞(Toyota)",
                "現代(Hyundai)",
                "Benz賓士(德國 )",
                "Kymco光陽(國產)",
                "中華(Mitsubishi)",
                "裕隆(Nissan)",
                "YAMAHA山葉(國產)"
            ],
            "IMP_QU2": [
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子"
            ],
            "IMP_NAME3": [
                "營業種類",
                "營業種類",
                "營業種類",
                "營業種類",
                "營業種類",
                "營業種類",
                "營業種類",
                "營業種類",
                "營業種類",
                "營業種類"
            ],
            "IMP_VALUE3": [
                "一般職業",
                "一般職業",
                "一般職業",
                "一般職業",
                "一般職業",
                "一般職業",
                "一般職業",
                "家庭管理",
                "一般職業",
                "一般職業"
            ],
            "IMP_QU3": [
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子"
            ],
            "IMP_NAME4": [
                "車體險保費",
                "車體險保費",
                "車體險保費",
                "車體險保費",
                "車體險保費",
                "車體險保費",
                "車體險保費",
                "車體險保費",
                "車體險保費",
                "車體險保費"
            ],
            "IMP_VALUE4": [
                "0",
                "0",
                "0",
                "0",
                "0",
                "1680",
                "0",
                "0",
                "0",
                "0"
            ],
            "IMP_QU4": [
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件"
            ],
            "IMP_NAME5": [
                "輔導單位代號",
                "輔導單位代號",
                "輔導單位代號",
                "輔導單位代號",
                "輔導單位代號",
                "輔導單位代號",
                "輔導單位代號",
                "輔導單位代號",
                "輔導單位代號",
                "輔導單位代號"
            ],
            "IMP_VALUE5": [
                "CLA100A",
                "CCA0000",
                "DEA0000",
                "61A0102",
                "CLA0000",
                "CNA0000",
                "CJA100A",
                "CIA0000",
                "BMA0000",
                "BEA0000"
            ],
            "IMP_QU5": [
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子"
            ],
            "IMP_NAME6": [
                "輔導單位理賠次數",
                "輔導單位理賠次數",
                "輔導單位理賠次數",
                "輔導單位理賠次數",
                "輔導單位理賠次數",
                "輔導單位理賠次數",
                "輔導單位理賠次數",
                "輔導單位理賠次數",
                "輔導單位理賠次數",
                "輔導單位理賠次數"
            ],
            "IMP_VALUE6": [
                "2314",
                "3530",
                "2109",
                "47",
                "2747",
                "3471",
                "1616",
                "3071",
                "2189",
                "3106"
            ],
            "IMP_QU6": [
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件"
            ],
            "IMP_NAME7": [
                "經紀人代號",
                "經紀人代號",
                "經紀人代號",
                "經紀人代號",
                "經紀人代號",
                "經紀人代號",
                "經紀人代號",
                "經紀人代號",
                "經紀人代號",
                "經紀人代號"
            ],
            "IMP_VALUE7": [
                "X3",
                "X3",
                "X3",
                "X8",
                "X3",
                "X3",
                "X3",
                "6L",
                "X3",
                "X3"
            ],
            "IMP_QU7": [
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子",
                "風險因子"
            ],
            "IMP_NAME8": [
                "被保人過去三年投保乘客險數量",
                "被保人過去三年投保乘客險數量",
                "被保人過去三年投保乘客險數量",
                "被保人過去三年投保乘客險數量",
                "被保人過去三年投保乘客險數量",
                "被保人過去三年投保乘客險數量",
                "被保人過去三年投保乘客險數量",
                "被保人過去三年投保乘客險數量",
                "被保人過去三年投保乘客險數量",
                "被保人過去三年投保乘客險數量"
            ],
            "IMP_VALUE8": [
                "2",
                "0",
                "2",
                "1",
                "0",
                "1",
                "3",
                "0",
                "0",
                "1"
            ],
            "IMP_QU8": [
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於75%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件"
            ],
            "IMP_NAME9": [
                "近五年總理賠次數",
                "近五年總理賠次數",
                "近五年總理賠次數",
                "近五年總理賠次數",
                "近五年總理賠次數",
                "近五年總理賠次數",
                "近五年總理賠次數",
                "近五年總理賠次數",
                "近五年總理賠次數",
                "近五年總理賠次數"
            ],
            "IMP_VALUE9": [
                "0",
                "1",
                "0",
                "1",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0"
            ],
            "IMP_QU9": [
                "高於75%的歷史案件",
                "高於75%的歷史案件",
                "高於75%的歷史案件",
                "高於75%的歷史案件",
                "高於75%的歷史案件",
                "高於75%的歷史案件",
                "高於75%的歷史案件",
                "高於75%的歷史案件",
                "高於75%的歷史案件",
                "高於75%的歷史案件"
            ],
            "IMP_NAME10": [
                "其他險保費",
                "其他險保費",
                "其他險保費",
                "其他險保費",
                "其他險保費",
                "其他險保費",
                "其他險保費",
                "其他險保費",
                "其他險保費",
                "其他險保費"
            ],
            "IMP_VALUE10": [
                "0",
                "0",
                "0",
                "0",
                "0",
                "750",
                "0",
                "0",
                "0",
                "0"
            ],
            "IMP_QU10": [
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件",
                "高於50%的歷史案件"
            ],
            "IMP_NAME11": [
                "排氣量",
                "排氣量",
                "排氣量",
                "排氣量",
                "排氣量",
                "排氣量",
                "排氣量",
                "排氣量",
                "排氣量",
                "排氣量"
            ],
            "IMP_VALUE11": [
                "124",
                "124",
                "1497",
                "1781",
                "999",
                "3200",
                "124",
                "1597",
                "1995",
                "124"
            ],
            "IMP_QU11": [
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件",
                "低於90%的歷史案件"
            ],
            "IMP_NAME12": [
                "被保人過去三年投保竊盜險數量",
                "被保人過去三年投保竊盜險數量",
                "被保人過去三年投保竊盜險數量",
                "被保人過去三年投保竊盜險數量",
                "被保人過去三年投保竊盜險數量",
                "被保人過去三年投保竊盜險數量",
                "被保人過去三年投保竊盜險數量",
                "被保人過去三年投保竊盜險數量",
                "被保人過去三年投保竊盜險數量",
                "被保人過去三年投保竊盜險數量"
            ],
            "IMP_VALUE12": [
                "2",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0"
            ],
            "IMP_QU12": [
                "高於50%的歷史案件",
                "低於50%的歷史案件",
                "低於50%的歷史案件",
                "低於50%的歷史案件",
                "低於50%的歷史案件",
                "低於50%的歷史案件",
                "低於50%的歷史案件",
                "低於50%的歷史案件",
                "低於50%的歷史案件",
                "低於50%的歷史案件"
            ],
            "IMP_NAME13": [
                "第三人責任險保費",
                "第三人責任險保費",
                "第三人責任險保費",
                "第三人責任險保費",
                "第三人責任險保費",
                "第三人責任險保費",
                "第三人責任險保費",
                "第三人責任險保費",
                "第三人責任險保費",
                "第三人責任險保費"
            ],
            "IMP_VALUE13": [
                "590",
                "614",
                "4087",
                "1769",
                "6438",
                "2501",
                "938",
                "3217",
                "1748",
                "679"
            ],
            "IMP_QU13": [
                "低於75%的歷史案件",
                "低於75%的歷史案件",
                "低於75%的歷史案件",
                "低於75%的歷史案件",
                "低於75%的歷史案件",
                "低於75%的歷史案件",
                "低於75%的歷史案件",
                "低於75%的歷史案件",
                "低於75%的歷史案件",
                "低於75%的歷史案件"
            ],
            "IMP_NAME14": [
                "近五年其他險理賠金額(從車角度)",
                "近五年其他險理賠金額(從車角度)",
                "近五年其他險理賠金額(從車角度)",
                "近五年其他險理賠金額(從車角度)",
                "近五年其他險理賠金額(從車角度)",
                "近五年其他險理賠金額(從車角度)",
                "近五年其他險理賠金額(從車角度)",
                "近五年其他險理賠金額(從車角度)",
                "近五年其他險理賠金額(從車角度)",
                "近五年其他險理賠金額(從車角度)"
            ],
            "IMP_VALUE14": [
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0"
            ],
            "IMP_QU14": [
                "高於90%的歷史案件",
                "高於90%的歷史案件",
                "高於90%的歷史案件",
                "高於90%的歷史案件",
                "高於90%的歷史案件",
                "高於90%的歷史案件",
                "高於90%的歷史案件",
                "高於90%的歷史案件",
                "高於90%的歷史案件",
                "高於90%的歷史案件"
            ],
            "IMP_NAME15": [
                "被保人過去三年投保強制險數量",
                "被保人過去三年投保強制險數量",
                "被保人過去三年投保強制險數量",
                "被保人過去三年投保強制險數量",
                "被保人過去三年投保強制險數量",
                "被保人過去三年投保強制險數量",
                "被保人過去三年投保強制險數量",
                "被保人過去三年投保強制險數量",
                "被保人過去三年投保強制險數量",
                "被保人過去三年投保強制險數量"
            ],
            "IMP_VALUE15": [
                "3",
                "2",
                "3",
                "6",
                "0",
                "1",
                "3",
                "0",
                "0",
                "1"
            ],
            "IMP_QU15": [
                "高於75%的歷史案件",
                "高於75%的歷史案件",
                "高於75%的歷史案件",
                "高於75%的歷史案件",
                "低於50%的歷史案件",
                "高於50%的歷史案件",
                "高於75%的歷史案件",
                "低於50%的歷史案件",
                "低於50%的歷史案件",
                "高於50%的歷史案件"
            ]
        }
    },
    "outputFiles": "",
    "consoleOutput": "",
    "changedFiles": ""
}
```



### CarBody2 - 比對result結果 （2021/09/28)
RISK_PROB進位方式不同：
![](https://i.imgur.com/qw1IpqG.png)


![](https://i.imgur.com/6lzVZ7C.png)

 
 - 2021/09/29
 ### LIAB - 比對result結果 （2021/09/29)
**原服務回傳結果** (小數點後17位)
 ![](https://i.imgur.com/MUMfR3m.png)
**R container回傳結果**
![](https://i.imgur.com/FnI2xhO.png)

digits = 20
[1] 0.29943393170833598749
digits = 17
0.29943393170833599
digits = 22
0.2994339317083358764648

### car_bo
dy2(單筆) - 比對result結果 (2021/09/29)
**原服務回傳結果**
![](https://i.imgur.com/21VAikw.png)


**R container回傳結果**
![](https://i.imgur.com/NA1MKIh.png)

**在insert進入dataframe前的預測值**
![](https://i.imgur.com/4OLUCiF.png)

**在insert進入dataframe前的預測值**
調整：digits = 22
![](https://i.imgur.com/uJwlgCm.png)

**設定回傳值為小數點17位**
```r=
response$outputParameters$result$"RISK_PROB" <- list(format(response$outputParameters$result$"RISK_PROB", digits = 17))
```
**回傳結果** (是字串)
![](https://i.imgur.com/0zV5LHf.png)

**若將字串轉回numeric, 又會變為小數點第四位**
```r=
response$outputParameters$result$"RISK_PROB" <- list(format(response$outputParameters$result$"RISK_PROB", digits = 17))
response$outputParameters$result$"RISK_PROB" <- list(as.numeric(response$outputParameters$result$"RISK_PROB"))
```

**回傳結果** (小數點四位)
![](https://i.imgur.com/2taW1Er.png)



### 想法 :wrench: 確認dataframe回傳小數點位數限制
```是否限制回傳長度？都只有到小數點第3~第4位數```
1. char to numeric
2. set options(digits=17)
![](https://i.imgur.com/goppUmY.png)


### pd_model 
**原R服務結果**
![](https://i.imgur.com/FjPD39q.png)

**R container化後結果**
![](https://i.imgur.com/GW0nrSH.png)


-----


### 輸出結果比對 - pd_model
單筆比對結果一致
- **[單筆] 原R服務結果**
![](https://i.imgur.com/FjPD39q.png)
- **[單筆] R container化後結果**
![](https://i.imgur.com/jLrG3aw.png)

### 輸出結果比對 - report_car_risk
單筆比對結果一致
- **[單筆] 原R服務結果**
![](https://i.imgur.com/uxrcXxX.png)
- **[單筆] R container化後結果**
![](https://i.imgur.com/FuWA2Bi.png)


### 輸出結果比對 - car_body2
單筆比對結果一致
- **[單筆] 原R服務結果**
![](https://i.imgur.com/ubIlgdZ.png)

- **[單筆] R container化後結果**
![](https://i.imgur.com/RcytiyJ.png)


多筆比對結果有點誤差
- **[多筆] 原R服務結果**
![](https://i.imgur.com/UXujTaI.png)


- **[多筆] R container化後結果** (digits = 22)
![](https://i.imgur.com/dlJn8TV.png)


- **[多筆] *Text compare比對結果** (digits = 22)
![](https://i.imgur.com/t98MfHX.png)



### 輸出結果比對 - LIAB
單筆比對結果一致
- **[單筆] 原R服務結果**
![](https://i.imgur.com/GSNkmWF.png)

- **[單筆] R container化後結果**
![](https://i.imgur.com/SXE28dC.png)



多筆比對結果有點誤差
- **[多筆] 原R服務結果**
![](https://i.imgur.com/EXCFBxv.png)


- **[多筆] R container化後結果** (digits = 22)
![](https://i.imgur.com/XwIaQcx.png)


- **Text compare比對結果** (digits = 22)
![](https://i.imgur.com/uTI9HdZ.png)




"0.30218155682086944580",
                "0.90126579999923706055",
                "0.66449895501136779785",
                "0.81338810920715332031",
                "0.39510716497898101807",
                "0.78745244443416595459",
                "0.94867062568664550781",
                "0.50752438604831695557",
                "0.55740346014499664307",
                "1.04268303513526916504"