library(ggplot2)
library(reshape2)
## Warning: package 'reshape2' was built under R version 4.5.2
library(corrplot)
## Warning: package 'corrplot' was built under R version 4.5.1
## corrplot 0.95 loaded
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.5.1
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
catastrophe_data <- data.frame(
Event_Type = c("Bushfire", "Cyclone", "Earthquake", "Flood", "Hail", "Storm"),
Count_of_Type = c(37, 37, 5, 57, 31, 72),
Domestic_Building_Claims = c(10496, 93679, 15759, 443967, 131460, 82283),
Domestic_Content_Claims = c(15069, 48237, 802, 231243, 24531, 22327),
Domestic_Motor_Claims = c(1824, 9075, 14, 77683, 278216, 81605),
Domestic_Other_Claims = c(732, 792, 10, 4493, 527, 189),
Commercial_Property_Claims = c(9810, 21456, 1512, 57380, 11217, 9188),
Commercial_Motor_Claims = c(1541, 1141, 356, 10454, 21384, 4414),
Commercial_BI_Claims = c(1360, 1510, 21, 5325, 839, 397),
Commercial_Other_Claims = c(1047, 318, 62, 3078, 219, 199),
Commercial_Crop_Claims = c(2, 217, 0, 549, 105, 984)
)
# Create dummy variables for each event type
event_dummies <- model.matrix(~ Event_Type - 1, data = catastrophe_data)
event_dummies_df <- as.data.frame(event_dummies)
# Combine with claim data
full_data <- cbind(event_dummies_df, catastrophe_data[, -1]) # Remove Event_Type column
# Get list of event types and claim types
event_types <- colnames(event_dummies_df)
claim_types <- colnames(catastrophe_data)[3:ncol(catastrophe_data)] # All columns except Event_Type and Count_of_Type
# Calculate correlations between each event type and each claim type
correlation_results <- data.frame()
for(event in event_types) {
for(claim in claim_types) {
cor_value <- cor(full_data[[event]], full_data[[claim]])
correlation_results <- rbind(correlation_results,
data.frame(Event_Type = event,
Claim_Type = claim,
Correlation = cor_value))
}
}
print("Correlations between Event Types and Claim Types:")
## [1] "Correlations between Event Types and Claim Types:"
print(correlation_results)
## Event_Type Claim_Type Correlation
## 1 Event_TypeBushfire Domestic_Building_Claims -0.362577407
## 2 Event_TypeBushfire Domestic_Content_Claims -0.237046297
## 3 Event_TypeBushfire Domestic_Motor_Claims -0.335513995
## 4 Event_TypeBushfire Domestic_Other_Claims -0.114367627
## 5 Event_TypeBushfire Commercial_Property_Claims -0.209809820
## 6 Event_TypeBushfire Commercial_Motor_Claims -0.300787196
## 7 Event_TypeBushfire Commercial_BI_Claims -0.054905828
## 8 Event_TypeBushfire Commercial_Other_Claims 0.095688793
## 9 Event_TypeBushfire Commercial_Crop_Claims -0.388175923
## 10 Event_TypeCyclone Domestic_Building_Claims -0.109366603
## 11 Event_TypeCyclone Domestic_Content_Claims -0.049695041
## 12 Event_TypeCyclone Domestic_Motor_Claims -0.302147657
## 13 Event_TypeCyclone Domestic_Other_Claims -0.096854932
## 14 Event_TypeCyclone Commercial_Property_Claims 0.073745699
## 15 Event_TypeCyclone Commercial_Motor_Claims -0.324814931
## 16 Event_TypeCyclone Commercial_BI_Claims -0.016658734
## 17 Event_TypeCyclone Commercial_Other_Claims -0.212289706
## 18 Event_TypeCyclone Commercial_Crop_Claims -0.116768367
## 19 Event_TypeEarthquake Domestic_Building_Claims -0.346556724
## 20 Event_TypeEarthquake Domestic_Content_Claims -0.317634224
## 21 Event_TypeEarthquake Domestic_Motor_Claims -0.343842925
## 22 Event_TypeEarthquake Domestic_Other_Claims -0.325103723
## 23 Event_TypeEarthquake Commercial_Property_Claims -0.411848605
## 24 Event_TypeEarthquake Commercial_Motor_Claims -0.371969361
## 25 Event_TypeEarthquake Commercial_BI_Claims -0.396324884
## 26 Event_TypeEarthquake Commercial_Other_Claims -0.320441278
## 27 Event_TypeEarthquake Commercial_Crop_Claims -0.390700645
## 28 Event_TypeFlood Domestic_Building_Claims 0.956917444
## 29 Event_TypeFlood Domestic_Content_Claims 0.984024324
## 30 Event_TypeFlood Domestic_Motor_Claims 0.013560204
## 31 Event_TypeFlood Domestic_Other_Claims 0.983386467
## 32 Event_TypeFlood Commercial_Property_Claims 0.948419274
## 33 Event_TypeFlood Commercial_Motor_Claims 0.234610809
## 34 Event_TypeFlood Commercial_BI_Claims 0.956092349
## 35 Event_TypeFlood Commercial_Other_Claims 0.953719427
## 36 Event_TypeFlood Commercial_Crop_Claims 0.302335394
## 37 Event_TypeHail Domestic_Building_Claims 0.005639556
## 38 Event_TypeHail Domestic_Content_Claims -0.183599674
## 39 Event_TypeHail Domestic_Motor_Claims 0.936336620
## 40 Event_TypeHail Domestic_Other_Claims -0.174202668
## 41 Event_TypeHail Commercial_Property_Claims -0.175552340
## 42 Event_TypeHail Commercial_Motor_Claims 0.891168669
## 43 Event_TypeHail Commercial_BI_Claims -0.187750733
## 44 Event_TypeHail Commercial_Other_Claims -0.254113947
## 45 Event_TypeHail Commercial_Crop_Claims -0.258152768
## 46 Event_TypeStorm Domestic_Building_Claims -0.144056267
## 47 Event_TypeStorm Domestic_Content_Claims -0.196049088
## 48 Event_TypeStorm Domestic_Motor_Claims 0.031607752
## 49 Event_TypeStorm Domestic_Other_Claims -0.272857516
## 50 Event_TypeStorm Commercial_Property_Claims -0.224954207
## 51 Event_TypeStorm Commercial_Motor_Claims -0.128207989
## 52 Event_TypeStorm Commercial_BI_Claims -0.300452169
## 53 Event_TypeStorm Commercial_Other_Claims -0.262563288
## 54 Event_TypeStorm Commercial_Crop_Claims 0.851462310
Correlation Heatmap
# Create correlation matrix for heatmap
cor_matrix <- matrix(NA, nrow = length(event_types), ncol = length(claim_types))
rownames(cor_matrix) <- gsub("Event_Type", "", event_types)
colnames(cor_matrix) <- claim_types
for(i in 1:length(event_types)) {
for(j in 1:length(claim_types)) {
cor_matrix[i, j] <- cor(full_data[[event_types[i]]], full_data[[claim_types[j]]])
}
}
print("Correlation Matrix (Event Types vs Claim Types):")
## [1] "Correlation Matrix (Event Types vs Claim Types):"
print(round(cor_matrix, 3))
## Domestic_Building_Claims Domestic_Content_Claims
## Bushfire -0.363 -0.237
## Cyclone -0.109 -0.050
## Earthquake -0.347 -0.318
## Flood 0.957 0.984
## Hail 0.006 -0.184
## Storm -0.144 -0.196
## Domestic_Motor_Claims Domestic_Other_Claims
## Bushfire -0.336 -0.114
## Cyclone -0.302 -0.097
## Earthquake -0.344 -0.325
## Flood 0.014 0.983
## Hail 0.936 -0.174
## Storm 0.032 -0.273
## Commercial_Property_Claims Commercial_Motor_Claims
## Bushfire -0.210 -0.301
## Cyclone 0.074 -0.325
## Earthquake -0.412 -0.372
## Flood 0.948 0.235
## Hail -0.176 0.891
## Storm -0.225 -0.128
## Commercial_BI_Claims Commercial_Other_Claims Commercial_Crop_Claims
## Bushfire -0.055 0.096 -0.388
## Cyclone -0.017 -0.212 -0.117
## Earthquake -0.396 -0.320 -0.391
## Flood 0.956 0.954 0.302
## Hail -0.188 -0.254 -0.258
## Storm -0.300 -0.263 0.851
# Correlation Heatmap
melted_cormat <- melt(cor_matrix)
ggplot(melted_cormat, aes(x = Var2, y = Var1, fill = value)) +
geom_tile() +
geom_text(aes(label = round(value, 2)), color = "black", size = 4) +
scale_fill_gradient2(low = "skyblue", high = "blue", mid = "white",
midpoint = 0, limit = c(-1, 1), space = "Lab",
name = "Correlation") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1),
plot.title = element_text(hjust = 0.5)) +
labs(title = "Correlation Heatmap: Event Types vs Claim Types",
x = "Claim Types", y = "Event Types") +
coord_fixed()
