Question 1
mu <- 2 sigma <- sqrt(0.25)
dnorm(0.5, mean = mu, sd = sigma) # (i) f(0.5) pnorm(2.5, mean = mu, sd = sigma) # (ii) F(2.5) qnorm(0.95, mean = mu, sd = sigma) # (iii) F^-1(0.95) pnorm(3, mean = mu, sd = sigma) - pnorm(1, mean = mu, sd = sigma) # (iv) Pr(1 <= X <= 3)
Question 2
set.seed(2026) # reproducibility x <- rexp(1000, rate = 2) # 1000 Exp(rate = 2) variables mean(x) # sample mean (theoretical mean = 1/2)
hist(x, breaks = 30, freq = FALSE, col = “steelblue”, border = “white”, main = “Histogram of 1000 Exp(rate = 2) Random Variables”, xlab = “x”, ylab = “Density”) curve(dexp(x, rate = 2), add = TRUE, col = “darkorange”, lwd = 2) # true density abline(v = mean(x), lty = 2, lwd = 2) # sample mean legend(“topright”, legend = c(“True density”, “Sample mean”), col = c(“darkorange”, “black”), lty = c(1, 2), lwd = 2, bty = “n”)
Question 3 (i): Import and describe
library(ggplot2)
claims <- read.csv(file.choose(), stringsAsFactors = FALSE) # pick insurance_claims CSV
head(claims, 10) # a) first ten observations dim(claims) # rows, columns nrow(claims); ncol(claims) # b) observations and variables str(claims) # c) data types sapply(claims, class)
num_vars <- names(claims)[sapply(claims, is.numeric)] summary(claims[, num_vars]) # d) descriptive statistics round(sapply(claims[, num_vars], function(v) c(n = sum(!is.na(v)), mean = mean(v), sd = sd(v), min = min(v), Q1 = quantile(v, .25), median = median(v), Q3 = quantile(v, .75), max = max(v))), 2)
Question 3 (ii): Cleaning
colSums(is.na(claims)) # genuine NAs colSums(claims == “?”, na.rm = TRUE) # “?” used as a missing-value code table(claims$authorities_contacted) # “None” is a real category, not missing
claims[claims == “?”] <- NA # convert “?” to NA # collision_type is missing only for theft / parked car: not applicable claims\(collision_type[is.na(claims\)collision_type)] <- “Not applicable” claims\(property_damage[is.na(claims\)property_damage)] <- “Unknown” claims\(police_report_available[is.na(claims\)police_report_available)] <- “Unknown”
sum(duplicated(claims)) # fully duplicated rows sum(duplicated(claims$policy_number)) # duplicated policy numbers
sort(unique(claims\(auto_make)) # spelling errors claims\)auto_make[claims\(auto_make == "Suburu"] <- "Subaru" claims\)auto_make[claims\(auto_make == "Accura"] <- "Acura" claims\)auto_model[claims$auto_model == “Forrestor”] <- “Forester”
table(claims\(umbrella_limit < 0) # an umbrella limit cannot be negative claims\)umbrella_limit <- abs(claims$umbrella_limit)
sum(claims\(total_claim_amount != claims\)injury_claim + claims\(property_claim + claims\)vehicle_claim)
claims\(policy_bind_date <- as.Date(claims\)policy_bind_date, format = “%d/%m/%Y”) claims\(incident_date <- as.Date(claims\)incident_date, format = “%d/%m/%Y”) sum(claims\(policy_bind_date > claims\)incident_date) # policy bound after incident? claims\(bind_after_incident <- claims\)policy_bind_date > claims$incident_date # flag
cat_vars <- c(“policy_state”, “policy_csl”, “insured_sex”, “insured_education_level”, “insured_occupation”, “insured_hobbies”, “insured_relationship”, “incident_type”, “collision_type”, “authorities_contacted”, “incident_state”, “incident_city”, “property_damage”, “police_report_available”, “auto_make”, “auto_model”, “fraud_reported”) claims[cat_vars] <- lapply(claims[cat_vars], factor) claims\(incident_severity <- factor(claims\)incident_severity, levels = c(“Trivial Damage”, “Minor Damage”, “Major Damage”, “Total Loss”), ordered = TRUE) claims\(policy_number <- as.character(claims\)policy_number) # identifiers claims\(insured_zip <- as.character(claims\)insured_zip) str(claims)
iqr_out <- function(v) { q <- quantile(v, c(.25, .75)); f <- 1.5 * diff(q) sum(v < q[1] - f | v > q[2] + f) } sapply(claims[, c(“months_as_customer”, “age”, “policy_annual_premium”, “capital.gains”, “capital.loss”, “total_claim_amount”, “injury_claim”, “property_claim”, “vehicle_claim”)], iqr_out) boxplot(claims$total_claim_amount, horizontal = TRUE, main = “Total claim amount”, xlab = “Amount”) # Outliers are plausible values, so they are flagged and retained.
Question 3 (iii): Analysis
library(ggplot2) theme_set(theme_minimal()) claims\(fraud <- factor(claims\)fraud_reported, levels = c(“N”, “Y”), labels = c(“Non-fraud”, “Fraud”))
summary(claims\(total_claim_amount) sd(claims\)total_claim_amount) library(e1071); skewness(claims\(total_claim_amount) # install.packages("e1071") ggplot(claims, aes(total_claim_amount)) + geom_histogram(bins = 30, fill = "steelblue", colour = "white") + geom_vline(xintercept = mean(claims\)total_claim_amount), colour = “darkorange”, linetype = 2) + labs(title = “Distribution of total claim amount”, x = “Total claim amount”, y = “Frequency”) ggplot(claims, aes(y = total_claim_amount)) + geom_boxplot(fill = “lightblue”) + labs(title = “Boxplot of total claim amount”, y = “Total claim amount”)
cor.test(claims\(age, claims\)total_claim_amount) # Pearson cor.test(claims\(age, claims\)total_claim_amount, method = “spearman”) summary(lm(total_claim_amount ~ age, data = claims)) claims\(age_group <- cut(claims\)age, c(18, 29, 39, 49, 70), labels = c(“19-29”, “30-39”, “40-49”, “50+”)) aggregate(total_claim_amount ~ age_group, data = claims, FUN = function(v) c(n = length(v), mean = mean(v), median = median(v))) ggplot(claims, aes(age, total_claim_amount)) + geom_point(alpha = .4, colour = “steelblue”) + geom_smooth(method = “lm”, colour = “darkorange”) + labs(title = “Policyholder age vs total claim amount”, x = “Age (years)”, y = “Total claim amount”)
aggregate(total_claim_amount ~ incident_type, data = claims, FUN = function(v) c(n = length(v), mean = mean(v), median = median(v), sd = sd(v))) aggregate(total_claim_amount ~ incident_severity, data = claims, FUN = function(v) c(n = length(v), mean = mean(v), median = median(v), sd = sd(v))) round(tapply(claims\(total_claim_amount, list(claims\)incident_type, claims$incident_severity), mean)) kruskal.test(total_claim_amount ~ incident_type, data = claims) kruskal.test(total_claim_amount ~ incident_severity, data = claims) ggplot(claims, aes(incident_type, total_claim_amount)) + geom_boxplot(fill = “lightblue”) + labs(title = “Total claim amount by incident type”, x = NULL, y = “Total claim amount”) ggplot(claims, aes(incident_severity, total_claim_amount)) + geom_boxplot(fill = “navajowhite”) + labs(title = “Total claim amount by incident severity”, x = NULL, y = “Total claim amount”)
aggregate(total_claim_amount ~ policy_state, data = claims, FUN = function(v) c(n = length(v), mean = mean(v), median = median(v), total = sum(v))) round(prop.table(table(claims\(policy_state, claims\)fraud), 1) * 100, 1) kruskal.test(total_claim_amount ~ policy_state, data = claims) chisq.test(table(claims\(policy_state, claims\)fraud)) ggplot(claims, aes(policy_state)) + geom_bar(fill = “steelblue”) + labs(title = “Number of claims by policy state”, x = “Policy state”, y = “Claims”) ggplot(claims, aes(policy_state, total_claim_amount)) + stat_summary(fun = mean, geom = “col”, fill = “seagreen”) + labs(title = “Mean claim amount by policy state”, x = “Policy state”, y = “Mean total claim”)
table(claims\(fraud); round(prop.table(table(claims\)fraud)) * 100, 1) aggregate(total_claim_amount ~ fraud, data = claims, FUN = function(v) c(n = length(v), mean = mean(v), median = median(v))) wilcox.test(total_claim_amount ~ fraud, data = claims) round(prop.table(table(claims\(incident_severity, claims\)fraud), 1) * 100, 1) chisq.test(table(claims\(incident_severity, claims\)fraud)) round(prop.table(table(claims\(incident_type, claims\)fraud), 1) * 100, 1) chisq.test(table(claims\(incident_type, claims\)fraud)) ggplot(claims, aes(fraud, fill = fraud)) + geom_bar(show.legend = FALSE) + labs(title = “Reported fraud”, x = NULL, y = “Claims”) ggplot(claims, aes(incident_severity, fill = fraud)) + geom_bar(position = “fill”) + labs(title = “Share of fraudulent claims by severity”, x = NULL, y = “Proportion”, fill = NULL)
Question 4
before <- c(122, 124, 120, 119, 119, 120, 122, 125, 124, 123, 122, 121) after <- c(123, 125, 120, 124, 118, 122, 123, 128, 124, 125, 124, 120)
d <- after - before mean(d); sd(d) shapiro.test(d) # normality of differences
t.test(after, before, paired = TRUE, alternative = “greater”) qt(0.95, df = 11) # critical value