---
title: "Insurance - Data Analysis (II)"
author: "Digvijay Singh, R Team, FOSSEE, IIT Bombay"
date: "05/03/2022"
output: html_document
---

## 1) Loading libraries
```{r,warning=F}
suppressPackageStartupMessages(library(tidyverse)) # For data manipulation and analysis
suppressPackageStartupMessages(library(skimr)) # Data summary
```

## 2) Loading data
```{r}
data <- suppressMessages(read_csv("updated_insurance.csv"))
```

## 3) Basic statistics
```{r}
# Mean, standard deviation, minimum, maximum, range, quartiles, IQR, skewness and kurtosis
statistics <- function(x)
{
  mean <- mean(x)
  standard_deviation <- sd(x)
  min <- min(x)
  max <- max(x)
  range <- max - min
  first_quartile <- quantile(x)[2]
  third_quartile <- quantile(x)[4]
  IQR <- third_quartile - first_quartile
  skewness <- mean((x-mean(x))^3)/(sd(x)^3)
  kurtosis <- (mean((x-mean(x))^4))/sd(x)^4
  
  return(cat("\t","Mean: ",mean,"\n\t","Standard deviation: ",standard_deviation,"\n\t","Minimum value: ",min,"\n\t","Maximum value: ",max,"\n\t","Range: ",range,"\n\t","First Quartile: ",first_quartile,"\n\t","Third Quartile: ",third_quartile,"\n\t","Interquartile Range: ",IQR,"\n\t","Skewness: ",skewness,"\n\t","Kurtosis: ",kurtosis))
}
# Age
statistics(data$age)
# BMI
statistics(data$bmi)
# Charges
statistics(data$charges)
```

## 4) Histogram
```{r}
# Distribution of the "charges" variable
hist(data$charges,col="green",border="black",prob = TRUE,xlab = "",main = "Charges")
```

## 5) Data summary
```{r}
skim(data)
```

## 6) Correlation
```{r}
# Correlation between all continuous variables
cor(data[,c("age","bmi","charges")])
```

## 7) Scatter plot
```{r}

# Spoken Tutorials:
# 17. Introduction to ggplot2
# 18. Aesthetic Mapping in ggplot2
# 19. Data Manipulation using dplyr Package
# 20. More Functions in dplyr Package
# 21. Pipe Operator

# BMI and charges
# Scatter plot
ggplot(data = data,aes(y=charges,x=bmi)) + geom_point() + labs(x = "BMI", y = "Charges") + theme_bw()

# Age and charges
# Scatter plot
ggplot(data = data,aes(y=charges,x=age)) + geom_point() + labs(x = "Age", y = "Charges") + theme_bw()

# Significant variables
# 1) age_category
# 2) smoker
# 3) bmi_category

# Finding patterns using single discrete variable
# Finding patterns using "age_category" variable
ggplot(data = data,aes(y=charges,x=age)) + geom_point(aes(color=age_category)) + labs(x = "Age", y = "Charges") + theme_bw()

# Finding patterns using "smoker" variable
ggplot(data = data,aes(y=charges,x=age)) + geom_point(aes(color=smoker)) + labs(x = "Age", y = "Charges") + theme_bw()

# Finding patterns using "bmi_category" variable
ggplot(data = data,aes(y=charges,x=age)) + geom_point(aes(color=bmi_category)) + labs(x = "Age", y = "Charges") + theme_bw()

# Finding patterns using multiple discrete variables
# Finding patterns using "smoker" and "bmi_category" variables
ggplot(data = data %>% filter(bmi_category=="Obese"),aes(y=charges,x=age)) + geom_point(aes(color=smoker)) + labs(x = "Age", y = "Charges") + theme_bw()
ggplot(data = data %>% filter(bmi_category=="Non-obese"),aes(y=charges,x=age)) + geom_point(aes(color=smoker)) + labs(x = "Age", y = "Charges") + theme_bw()

# Four categories are discovered
# 1) Smoker and obese
# 2) Non-smoker and obese
# 3) Smoker and non-obese
# 4) Non-smoker and non-obese
```

## 8) Scatter plot and regression
```{r}
# 1) Smoker and obese category
smoker_obese <- data %>% filter(smoker=="yes"&bmi_category=="Obese")
# Regression
smoker_obese_lm <- lm(charges~age,data = smoker_obese)
summary(smoker_obese_lm)
# Scatter plot with regression line
ggplot(data = smoker_obese,aes(y=charges,x=age)) + geom_point() + labs(x = "Age", y = "Charges") + theme_bw() + geom_smooth(method='lm', formula = y~x)
# Residuals plot
qqnorm(rstandard(smoker_obese_lm), ylab="Standardized Residuals",xlab="Normal Scores")
qqline(rstandard(smoker_obese_lm))
# Squared residuals plot
plot(y=smoker_obese_lm$residuals^2,x=as.numeric(names(smoker_obese_lm$residuals^2)),ylab="Squared Residuals",xlab="Index")
# Histogram of residuals
hist(smoker_obese_lm$residuals,main = "Histogram of Residuals",xlab="",prob=T)

# 2) Non-smoker and obese category
non_smoker_obese <- data %>% filter(smoker=="no"&bmi_category=="Obese")
# Regression
non_smoker_obese_lm <- lm(charges~age,data = non_smoker_obese)
summary(non_smoker_obese_lm)
# Scatter plot with regression line
ggplot(data = non_smoker_obese,aes(y=charges,x=age)) + geom_point() + labs(x = "Age", y = "Charges") + theme_bw() + geom_smooth(method='lm', formula = y~x)
# Residuals plot
qqnorm(rstandard(non_smoker_obese_lm), ylab="Standardized Residuals",xlab="Normal Scores")
qqline(rstandard(smoker_obese_lm))
# Squared residuals plot
plot(y=non_smoker_obese_lm$residuals^2,x=as.numeric(names(non_smoker_obese_lm$residuals^2)),ylab="Squared Residuals",xlab="Index")
# Histogram of residuals
hist(non_smoker_obese_lm$residuals,main = "Histogram of Residuals",xlab="",prob=T)

# 3) Smoker and non-obese category
smoker_non_obese <- data %>% filter(smoker=="yes"&bmi_category=="Non-obese")
# Regression
smoker_non_obese_lm <- lm(charges~age,data = smoker_non_obese)
summary(smoker_non_obese_lm)
# Scatter plot with regression line
ggplot(data = smoker_non_obese,aes(y=charges,x=age)) + geom_point() + labs(x = "Age", y = "Charges") + theme_bw() + geom_smooth(method='lm', formula = y~x)
# Residuals plot
qqnorm(rstandard(smoker_non_obese_lm), ylab="Standardized Residuals",xlab="Normal Scores")
qqline(rstandard(smoker_non_obese_lm))
# Squared residuals plot
plot(y=smoker_non_obese_lm$residuals^2,x=as.numeric(names(smoker_non_obese_lm$residuals^2)),ylab="Squared Residuals",xlab="Index")
# Histogram of residuals
hist(smoker_non_obese_lm$residuals,main = "Histogram of Residuals",xlab="",prob=T)

# 4) Non-smoker and non-obese category
non_smoker_non_obese <- data %>% filter(smoker=="no"&bmi_category=="Non-obese")
# Regression
non_smoker_non_obese_lm <- lm(charges~age,data = non_smoker_non_obese)
summary(non_smoker_non_obese_lm)
# Scatter plot with regression line
ggplot(data = non_smoker_non_obese,aes(y=charges,x=age)) + geom_point() + labs(x = "Age", y = "Charges") + theme_bw() + geom_smooth(method='lm', formula = y~x)
# Residuals plot
qqnorm(rstandard(non_smoker_non_obese_lm), ylab="Standardized Residuals",xlab="Normal Scores")
qqline(rstandard(non_smoker_non_obese_lm))
# Squared residuals plot
plot(y=non_smoker_non_obese_lm$residuals^2,x=as.numeric(names(non_smoker_non_obese_lm$residuals^2)),ylab="Squared Residuals",xlab="Index")
# Histogram of residuals
hist(non_smoker_non_obese_lm$residuals,main = "Histogram of Residuals",xlab="",prob=T)
```