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

## 1) Loading libraries
```{r,warning=F,message=F}
suppressPackageStartupMessages(library(tidyverse)) # For data manipulation and analysis
suppressPackageStartupMessages(library(lessR)) # For creating pivot tables
```

## 2) Loading data
```{r}

# Spoken Tutorials:
# 5. Introduction to Data Frames in R

# Source: https://github.com/PacktPublishing/Machine-Learning-with-R-Second-Edition/blob/master/Chapter%2006/insurance.csv
data <- read.csv("insurance.csv")
```

## 3) Discrete data analysis

### 3.1) Univariate frequency/proportion distribution, bar plot and pie chart
```{r}

# Spoken Tutorials:
# 9. Indexing and Slicing Data Frames
# 15. Plotting Histograms and Pie Chart 
# 16. Plotting Bar Charts and Scatter Plot

# Color source: http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf

# 3.1.1) Gender
# Univariate frequency/proportion distribution
prop.table(table(data$sex))
# Bar plot
barplot(height = prop.table(table(data$sex)), col = c("green","blue"), main = "Gender", xlab = "", ylab = "Proportion")
# Pie chart
pie(x = prop.table(table(data$sex)), labels = names(prop.table(table(data$sex))), col = c("green","blue"), radius = 1, main = "Gender")

# 3.1.2) Children
# Univariate frequency/proportion distribution
prop.table(table(data$children))
# Bar plot
barplot(height = prop.table(table(data$children)), col = c("darkolivegreen1","green","limegreen","lawngreen","darkseagreen4","forestgreen"), main = "Children", xlab = "", ylab = "Proportion")
# Pie chart
pie(x = prop.table(table(data$children)), labels = names(prop.table(table(data$children))), col = c("darkolivegreen1","green","limegreen","lawngreen","darkseagreen4","forestgreen"), radius = 1, main = "Children")

# 3.1.3) Smoker
# Univariate frequency/proportion distribution
prop.table(table(data$smoker))
# Bar plot
barplot(height = prop.table(table(data$smoker)), col = c("lightcyan","grey"), main = "Smoker", xlab = "", ylab = "Proportion")
# Pie chart
pie(x = prop.table(table(data$smoker)), labels = names(prop.table(table(data$smoker))), col = c("lightcyan","grey"), radius = 1, main = "Smoker")

# 3.1.4) Region
# Univariate frequency/proportion distribution
prop.table(table(data$region))
# Bar plot
barplot(height = prop.table(table(data$region)), col = c("blue","slateblue1","steelblue2"), main = "Region", xlab = "", ylab = "Proportion")
# Pie chart
pie(x = prop.table(table(data$region)), labels = names(prop.table(table(data$region))), col = c("blue","slateblue1","steelblue2","darkviolet"), radius = 1, main = "Region")
```

### 3.2) Bivariate distribution and grouped bar plot
```{r}

# Spoken Tutorials:
# 23. Functions in R

# Function for cross tabulation (proportion)
cross_tab_proportion <- function(x,y)
{
  temp_table <- prop.table(table(x,y))
  temp_col <- colSums(temp_table)
  temp_row <- rowSums(temp_table)
  temp_sum <- sum(temp_table)
  temp <- as.data.frame(rbind(cbind(temp_table,temp_row),c(temp_col,temp_sum)))
  rownames(temp)[nrow(temp)] <- "total"
  colnames(temp)[ncol(temp)] <- "total"
  return(temp)
}

# 3.2.1) Gender and smoker
# Cross tabulation
cross_tab_proportion(data$sex,data$smoker)
# Grouped bar plot
table_data <- prop.table(table(data$sex,data$smoker))
barplot(table_data, main = "Gender", xlab = "", ylab = "Proportion",
        col = c("blue", "red", "blue", "red"), beside = TRUE)
legend("topright", rownames(table_data), fill = c("blue", "red", "blue", "red"), xpd=TRUE, inset=c(0, -.1), horiz = T, bty = "n")

# 3.2.2) Children and smoker
# Cross tabulation
cross_tab_proportion(data$children,data$smoker)
# Grouped bar plot
table_data <- prop.table(table(data$children,data$smoker))
barplot(table_data, main = "Children", xlab = "", ylab = "Proportion",
        col = c("dodgerblue","blue","slateblue1","steelblue2","darkviolet","deepskyblue4"), beside = TRUE)
legend("topright", rownames(table_data), fill = c("dodgerblue","blue","slateblue1","steelblue2","darkviolet","deepskyblue4"), xpd=TRUE, inset=c(0, -.1), horiz = T, bty = "n")

# 3.2.3) Region and smoker
# Cross tabulation
cross_tab_proportion(data$region,data$smoker)
# Grouped bar plot
table_data <- prop.table(table(data$region,data$smoker))
barplot(table_data, main = "Region", xlab = "", ylab = "Proportion",
        col = c("darkorange4","peru","tan1","sandybrown"), beside = TRUE)
legend("topright", rownames(table_data), fill = c("darkorange4","peru","tan1","sandybrown"), xpd=TRUE, inset=c(0, -.3), ncol = 2, bty = "n")
```

### 3.3) Stacked bar plot
```{r}

# Spoken Tutorials:
# 19. Data Manipulation using dplyr Package
# 20. More Functions in dplyr Package
# 21. Pipe Operator

# 3.3.1) Gender and smoker
male <- data %>% filter(sex=="male")
female <- data %>% filter(sex=="female")
stack_data <- prop.table(table(data$sex,data$smoker))
rownames(stack_data) <- c("male","female")
barplot(stack_data, col=c("red","green"), main = "Gender", xlab = "", ylab = "Count")
legend("topright", rownames(stack_data), cex = .625, fill = c("red","green"), xpd=TRUE, inset=c(0, -.15), horiz = T)
```

### 3.4) Bivariate distribution (count) and Chi-squared test
```{r}
# Function for cross tabulation (count)
cross_tab_count <- function(x,y)
{
  temp_table <- table(x,y)
  temp_col <- colSums(temp_table)
  temp_row <- rowSums(temp_table)
  temp_sum <- sum(temp_table)
  temp <- as.data.frame(rbind(cbind(temp_table,temp_row),c(temp_col,temp_sum)))
  rownames(temp)[nrow(temp)] <- "total"
  colnames(temp)[ncol(temp)] <- "total"
  return(temp)
}

# 3.4.1) Gender and smoker
# Cross tabulation
cross_tab_count(data$sex,data$smoker)
# Chi-squared test
chisq.test(data$sex,data$smoker,correct = F)

# 3.4.2) Children and smoker
# Cross tabulation
cross_tab_count(data$children,data$smoker)
# Chi-squared test
chisq.test(data$children,data$smoker)

# 3.4.3) Region and smoker
# Cross tabulation
cross_tab_count(data$region,data$smoker)
# Chi-squared test
chisq.test(data$region,data$smoker)
```

## 4) Continuous data analysis

### 4.1) 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))
}
# 4.1.1) Age
statistics(data$age)
# 4.1.2) BMI
statistics(data$bmi)
# 4.1.3) Charges
statistics(data$charges)
```

## 5) Mixed data analysis

### 5.1) Data classification
```{r}

# Spoken Tutorials:
# 22. Conditional Statements

# 5.1.1) BMI

# Source: https://www.cdc.gov/healthyweight/assessing/bmi/adult_bmi/index.html
bmi_selection <- function(x)
{
  if(x < 30){return("Non-obese")}else 
        if(x >= 30){return("Obese")}
}
bmi_category <- NULL
for(i in 1:nrow(data))
{
  bmi_category <- append(bmi_category,bmi_selection(data$bmi[i]))
}
data <- cbind(data,bmi_category)
head(data)

# 5.1.2) Age

# Source: https://www.census.gov/prod/cen2010/briefs/c2010br-03.pdf
age_selection <- function(x)
{
  if(x < 18){return("Under 18 years")}else
    if(x < 45){return("18 to 44 years")}else
      if(x < 65){return("45 to 64 years")}else
        if(x > 65){return("65 years and over")}
}
age_category <- NULL
for(i in 1:nrow(data))
{
  age_category <- append(age_category,age_selection(data$age[i]))
}
data <- cbind(data,age_category)
head(data)
```

### 5.2) Pivot tables
```{r}
# 5.2.1) Table of age categories and gender containing mean of charges
pivot(data,mean,charges,age_category,sex) # "age_category" is significant.
# 5.2.2) Table of smoker and gender containing mean of charges
pivot(data,mean,charges,smoker,sex) # "smoker" is significant.
# 5.2.3) Table of smoker and age categories containing mean of charges
pivot(data,mean,charges,smoker,age_category) # Both "smoker" and "age_category" are significant.
# 5.2.4) Table of smoker and region containing mean of charges
pivot(data,mean,charges,smoker,region) # "smoker" is significant.
# 5.2.5) Table of smoker and children containing mean of charges
pivot(data,mean,charges,smoker,children) # "smoker" is significant.
# 5.2.6) Table of smoker and bmi categories containing mean of charges
pivot(data,mean,charges,smoker,bmi_category) # "smoker" is significant but "bmi_category" is significant for smokers.
# 5.2.7) Table of age categories and bmi categories containing mean of charges
pivot(data,mean,charges,age_category,bmi_category) # Both "age_category" and "bmi_category" are significant.

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

## 6) Saving modified data
```{r}
write.csv(data,"updated_insurance.csv",row.names = F)
```