USER
and this: LIC_template_v1 <- Schadenreserve_Einzelvertrag_out2 %>%
mutate(
sum_non_inv = sum(other_part_of_IBNS),
split = other_part_of_IBNS/sum_non_inv,
total_ontop = total * split
) %>%
group_by(GIC) %>%
mutate(
sum_non_inv_gic = sum(other_part_of_IBNS),
split_gic = other_part_of_IBNS/sum_non_inv_gic,
omega_ontop = ifelse(GIC==1007,omega*split_gic,0)
) %>%
ungroup() %>%
mutate(
other_part_of_IBNS = other_part_of_IBNS+total_ontop+omega_ontop
)
LIC_template <- LIC_template_v1 %>%
mutate(
`source system` = "PROL",
expectation = 50,
`reporting date` = reporting_date,
company = 101,
`Trading Partner` = "",
`Settlement Date` = Settlement_Date,
`Risk Adjustment` = 0.00,
`Accident Year` = Accident_Year
) %>%
group_by(Currency,GIC,Accident_Year,`source system`,expectation,`reporting date`) %>%
mutate(
`Non-InvC part of Claims` = sum(other_part_of_IBNS),
`Investment Component` = sum(Investment_component)
) %>%
ungroup() %>%
select(`source system`,expectation,Currency,`reporting date`,company,GIC,`Trading Partner`,`Accident Year`,`Settlement Date`,
`Non-InvC part of Claims`,`Risk Adjustment`,`Investment Component`) %>%
distinct() %>%
arrange(GIC,`Accident Year`)
ASSISTANT
Let's break down the provided R code step by step to understand its functionality:
1. **Calculating Summaries and Splits:**
```r
LIC_template_v1 <- Schadenreserve_Einzelvertrag_out2 %>%
mutate(
sum_non_inv = sum(other_part_of_IBNS),
split = other_part_of_IBNS / sum_non_inv,
total_ontop = total * split
) %>%
group_by(GIC) %>%
mutate(
sum_non_inv_gic = sum(other_part_of_IBNS),
split_gic = other_part_of_IBNS / sum_non_inv_gic,
omega_ontop = ifelse(GIC == 1007, omega * split_gic, 0)
) %>%
ungroup() %>%
mutate(
other_part_of_IBNS = other_part_of_IBNS + total_ontop + omega_ontop
)
```
- The code starts with `Schadenreserve_Einzelvertrag_out2` data frame.
- It calculates the total sum of the column `other_part_of_IBNS` for the entire data set and stores it in `sum_non_inv`.
- `split` is calculated as the ratio of `other_part_of_IBNS` to `sum_non_inv`.
- `total_ontop` is derived by multiplying `total` by `split`.
- The data frame is then grouped by `GIC`.
- Within each `GIC` group, it calculates the sum of `other_part_of_IBNS` and saves it as `sum_non_inv_gic`.
- `split_gic` is calculated as the ratio of `other_part_of_IBNS` to `sum_non_inv_gic`.
- `omega_ontop` is calculated for rows where `GIC` equals `1007`, otherwise it's set to zero.
- Ungroups the data frame.
- The `other_part_of_IBNS` column is updated to be the sum of itself, `total_ontop`, and `omega_ontop`.
2. **Creating LIC Template:**
```r
LIC_template <- LIC_template_v1 %>%
mutate(
`source system` = "PROL",
expectation = 50,
`reporting date` = reporting_date,
company = 101,
`Trading Partner` = "",
`Settlement Date` = Settlement_Date,
`Risk Adjustment` = 0.00,
`Accident Year` = Accident_Year
) %>%
group_by(Currency, GIC, Accident_Year, `source system`, expectation, `reporting date`) %>%
mutate(
`Non-InvC part of Claims` = sum(other_part_of_IBNS),
`Investment Component` = sum(Investment_component)
) %>%
ungroup() %>%
select(
`source system`, expectation, Currency, `reporting date`, company, GIC, `Trading Partner`, `Accident Year`, `Settlement Date`,
`Non-InvC part of Claims`, `Risk Adjustment`, `Investment Component`
) %>%
distinct() %>%
arrange(GIC, `Accident Year`)
```
- The code adds several new columns with constant values or based on existing ones: `source system`, `expectation`, `reporting date`, `company`, `Trading Partner`, `Settlement Date`, `Risk Adjustment`, `Accident Year`.
- Groups the data frame by `Currency`, `GIC`, `Accident_Year`, `source system`, `expectation`, and `reporting date`.
- Inside the group, it calculates `Non-InvC part of Claims` as the sum of `other_part_of_IBNS` and `Investment Component` as the sum of `Investment_component`.
- Ungroups the data frame.
- Selects the relevant columns for the final data frame: `source system`, `expectation`, `Currency`, `reporting date`, `company`, `GIC`, `Trading Partner`, `Accident Year`, `Settlement Date`, `Non-InvC part of Claims`, `Risk Adjustment`, `Investment Component`.
- The `distinct()` function ensures there are no duplicate rows.
- Finally, it sorts the data frame by `GIC` and `Accident Year`.
**Summary:** The provided R code processes the `Schadenreserve_Einzelvertrag_out2` data frame by creating splits and distributing totals across specified columns. It then creates a new data frame `LIC_template` with additional columns, calculates summaries grouped by specific keys, ensures no duplicates, and arranges the final data in a sorted order. This would typically be used in preparing summary reports or templates for further analysis or reporting.