## I start with sim_data_wide
sim_data_wide <- tidyr::spread(sim_data, quarter, pd)
## and calculate wide
wide1 <- with(sim_data_wide, cbind(PC_1 = P_1,
PC_2 = 1-(1-P_1)*(1-P_2),
PC_3 = 1-(1-P_1)*(1-P_2)*(1-P_3),
PC_4 =
Does this help?
sim_wide2 <- (
sim_data
%>% arrange( borrower_id, quarter )
%>% group_by( borrower_id )
%>% mutate( cumpd = 1 - cumprod( 1 - pd ) )
%>% ungroup()
%>% mutate( qlbl = paste0( "PC_", quarter ) )
%>% select( borrower_id, qlbl, cumpd )
%>% spread( qlbl, cumpd )
)
On May 9, 2020
Dear Axel,
Assuming that you're not wedded to using mutate():
> D1 <- 1 - as.matrix(sim_data_wide[, 2:11])
> D2 <- matrix(0, 10, 10)
> colnames(D2) <- paste0("PC_", 1:10)
> for (i in 1:10) D2[, i] <- 1 - apply(D1[, 1:i, drop=FALSE], 1, prod)
> all.equal(D2, as.matrix(sim_data_wide[, 22:31]))
[1]
Hello,
Is there a less verbose approach to obtaining the PC_i variables inside the
mutate?
library(tidyverse)
sim_data <- data.frame(borrower_id = sort(rep(1:10, 20)),
quarter = rep(1:20, 10),
pd = runif(length(rep(1:20, 10 # conditional probs
4 matches
Mail list logo