-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcal_user_coef.R
77 lines (71 loc) · 2.41 KB
/
cal_user_coef.R
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
#' A big wrapper of get user insurance discount coef.
#'
#' This function wrap all the functions to get the raw risk factors speicified
#' score, daymileage, discount coef. Before use this function, pleage make sure
#' that your data and cofiguration are correct.
#'
#' @param df Raw risk factors data.frame/tibble with the correct col names.
#' @param risk_names Correct col names to calculate.
#' @param score_config_list A List contains all riskFactors value and score
#' index, read from .RDS file.
#' @param rf_weight Correct col names of risk factors weight, sum to 1.
#' @param discount_index Specified score and coef.
#'
#' @return a \code{\link[tibble:as_tibble]{tibble}} / data.frame
#' @author \href{https://github.com/BruceZhaoR}{Wei Zhao}
#' @export
#'
#' @examples
#' \dontrun{
#' cal_user_coef(pj_rf, "mielage", score_config_list, rf_weight, discount_index)
#'
#' risk_names <- c("acc_count_phk","act_radius","dec_count_phk","high_curv_tr","holiday_tr",
#' "interstate_r","lane_change_phk","late_night_tr","long_tr","main_act_prov" ,
#' "mileage","speeding_lvl", "speeding_phk", "trip_dis_e", "turn_count_phk")
#' cal_user_coef(pj_rf, risk_names, score_config_list, rf_weight, discount_index)
#' }
cal_user_coef <- function(df, risk_names, score_config_list,
rf_weight, discount_index) {
day_mileage <- round(df$day_mileage, 2)
if (length(risk_names) == 1) {
tmp <- df$risk_names
idx <- grep(risk_names, names(score_config_list))
stopifnot(length(idx) == 2)
# since one risk fator, the weight is 1.
sum_score <- round(get_score_vec(
tmp, score_config_list[[idx[1]]],
score_config_list[[idx[2]]]
),
digits = 2
)
} else {
rf_df <- df[, risk_names]
rf_score <- rf2score(rf_df, score_config_list)
sum_score <- round(sum_rf_score(rf_score, rf_weight)$score,
digits = 2
)
}
discount_coef <- round(get_score_vec(
sum_score, discount_index$score,
discount_index$coef
),
digits = 4
)
discount <- round(1 - discount_coef, digits = 6)
n <- length(discount)
delta <- round(c(
discount[1],
discount[-1] - discount[1:(n - 1)]
))
result <- data.frame(
score = sum_score,
day_mileage = day_mileage,
discount_coef = discount_coef,
discount = discount,
delta = delta
)
if (requireNamespace("tibble", quietly = TRUE)) {
return(tibble::as_tibble(result))
}
return(result)
}