![](/img/trans.png)
[英]Pass rows from dataframe as arguments to custom regression function using dplyr/purrr
[英]Mapping using a custom function with 2 or more arguments using purrr
我正在R中构建一个保险评级者。我想构建一个具有多个参数的单个(有时很复杂)的函数,然后在每个保单持有人(数据行)上迭代该函数。 我将为此项目构建大约200个这些功能。 该函数运行良好,它实际上只是在费率表中查找值,并在何时,什么时候以及如何需要时加或乘。 我的问题是,当我有2个或多个直接从数据帧输入到函数中的参数时,如何映射,循环或以其他方式迭代此函数?
我以为我的编码可以用,但是后来我意识到map(或map_dbl)只拉入了我函数中第二个参数的第一个元素。
library(dplyr)
library(readr)
library(purrr)
#dummy data frames that represent a rating table, and a policy
#holder dataset
data_frame_Rate_Table<-data.frame("Policy_Class"=c("red", "white","blue"),"Rate"=c(3,9,19),"Factor_1"= 1:3,"Factor_2"=7:9)
data_frame_Policyholders<-data.frame("Policy_number"=1:10,"Policy_Class"=rep(c("red","red","white","blue","blue"),2),"Risk"=c(rep("High",5),rep("Low",5)),"Lapse"=rep(c("Y","N"),5))
function_example<-function(x,y,z){
Policy_Class<-x
Risk<-y
Lapse<-z
Rate<-ifelse(Policy_Class=="red",
data_frame_Rate_Table[data_frame_Rate_Table['Policy_Class']==Policy_Class,"Rate"]*data_frame_Rate_Table[data_frame_Rate_Table['Policy_Class']==Policy_Class,"Factor_1"]+
(ifelse(Risk=="High",3,1))*data_frame_Rate_Table[data_frame_Rate_Table['Policy_Class']==Policy_Class,"Factor_2"]+ifelse(Lapse=="Y",10,0),
ifelse(Policy_Class=="white",
data_frame_Rate_Table[data_frame_Rate_Table['Policy_Class']==Policy_Class,"Rate"]*data_frame_Rate_Table[data_frame_Rate_Table['Policy_Class']==Policy_Class,"Factor_1"]+
(ifelse(Risk=="High",5,1))*data_frame_Rate_Table[data_frame_Rate_Table['Policy_Class']==Policy_Class,"Factor_2"]+ifelse(Lapse=="Y",15,0),
ifelse(Policy_Class=="blue",
data_frame_Rate_Table[data_frame_Rate_Table['Policy_Class']==Policy_Class,"Rate"]*data_frame_Rate_Table[data_frame_Rate_Table['Policy_Class']==Policy_Class,"Factor_1"]+
(ifelse(Risk=="High",10,1))*data_frame_Rate_Table[data_frame_Rate_Table['Policy_Class']==Policy_Class,"Factor_2"]+ifelse(Lapse=="Y",33,0))))
Rate
}
我试过了:
result<-map_dbl(data_frame_Policyholders$Class,function_example, data_frame_Policyholders$Risk,data_frame_Policyholders$Lapse)
但这不是我所需要的。
预期结果是:
#copy and paste this coding into R to get the actual
#values that should go into the vector
function_example("red","High","Y")
function_example("red","High","N")
function_example("red","Low","Y")
function_example("red","Low","N")
function_example("white","High","Y")
function_example("white","High","N")
function_example("white","Low","Y")
function_example("white","Low","N")
function_example("blue","High","Y")
function_example("blue","High","N")
function_example("blue","Low","Y")
function_example("blue","Low","N")
但我显然无法输入每种组合
我需要一个函数来遍历每个保单持有人,并以排队的数字矢量产生结果。 purrr
做到吗? 有没有更好的方法去这里?
好吧,第一件事是将数据读取为字符而不是因子。 其次,由于您的函数有多个输入,因此需要pmap
而不是仅map
library(dplyr)
library(purrr)
data_frame_Policyholders %>%
mutate(new = pmap_dbl(list(Policy_Class, Risk, Lapse), function_example))
# Policy_number Policy_Class Risk Lapse new
#1 1 red High Y 34
#2 2 red High N 24
#3 3 white High Y 73
#4 4 blue High N 147
#5 5 blue High Y 180
#6 6 red Low N 10
#7 7 red Low Y 20
#8 8 white Low N 26
#9 9 blue Low Y 99
#10 10 blue Low N 66
数据
data_frame_Rate_Table<- data.frame(Policy_Class = c("red", "white","blue"),
Rate=c(3,9,19),"Factor_1"= 1:3,Factor_2=7:9, stringsAsFactors = FALSE)
data_frame_Policyholders <- data.frame(Policy_number . = 1:10,
Policy_Class=rep(c("red","red","white","blue","blue"),2),
Risk=c(rep("High",5),rep("Low",5)),
Lapse=rep(c("Y","N"),5), stringsAsFactors = FALSE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.