Adding necessary packages to library
library(pastecs)
library(ggplot2)
library(tidyr)
##
## Attaching package: 'tidyr'
## The following object is masked from 'package:pastecs':
##
## extract
library(car)
## Loading required package: carData
#Read in data
myData <- read.table("./Medical_Cost.csv", header = TRUE, sep = ",", dec = ".")
head(myData)
## age sex bmi children smoker region charges
## 1 19 female 27.900 0 yes southwest 16884.924
## 2 18 male 33.770 1 no southeast 1725.552
## 3 28 male 33.000 3 no southeast 4449.462
## 4 33 male 22.705 0 no northwest 21984.471
## 5 32 male 28.880 0 no northwest 3866.855
## 6 31 female 25.740 0 no southeast 3756.622
Variables:
Unit: A single beneficiary (person with insurance)
Sample size: 1338 beneficiaries
Source Link: https://www.kaggle.com/datasets/pritambhakat/medical-cost-dataset
#Cast Gender, smoker, and region as factor
myData$GenderFactor <- as.factor(myData$sex)
myData$SmokerFactor <- as.factor(myData$smoker)
myData$RegionFactor <- as.factor(myData$region)
str(myData)
## 'data.frame': 1338 obs. of 10 variables:
## $ age : int 19 18 28 33 32 31 46 37 37 60 ...
## $ sex : chr "female" "male" "male" "male" ...
## $ bmi : num 27.9 33.8 33 22.7 28.9 ...
## $ children : int 0 1 3 0 0 0 1 3 2 0 ...
## $ smoker : chr "yes" "no" "no" "no" ...
## $ region : chr "southwest" "southeast" "southeast" "northwest" ...
## $ charges : num 16885 1726 4449 21984 3867 ...
## $ GenderFactor: Factor w/ 2 levels "female","male": 1 2 2 2 2 1 1 1 2 1 ...
## $ SmokerFactor: Factor w/ 2 levels "no","yes": 2 1 1 1 1 1 1 1 1 1 ...
## $ RegionFactor: Factor w/ 4 levels "northeast","northwest",..: 4 3 3 2 2 3 3 2 1 2 ...
#Removing NAs (there are no NAs)
cleanedData <- drop_na(myData)
#View Cleaned Data
head(cleanedData)
## age sex bmi children smoker region charges GenderFactor
## 1 19 female 27.900 0 yes southwest 16884.924 female
## 2 18 male 33.770 1 no southeast 1725.552 male
## 3 28 male 33.000 3 no southeast 4449.462 male
## 4 33 male 22.705 0 no northwest 21984.471 male
## 5 32 male 28.880 0 no northwest 3866.855 male
## 6 31 female 25.740 0 no southeast 3756.622 female
## SmokerFactor RegionFactor
## 1 yes southwest
## 2 no southeast
## 3 no southeast
## 4 no northwest
## 5 no northwest
## 6 no southeast
Age
#Calculate descriptive statistics
round(stat.desc(cleanedData[ , c(-2,-5,-6,-8,-9,-10)]), 2)
## age bmi children charges
## nbr.val 1338.00 1338.00 1338.00 1338.00
## nbr.null 0.00 0.00 574.00 0.00
## nbr.na 0.00 0.00 0.00 0.00
## min 18.00 15.96 0.00 1121.87
## max 64.00 53.13 5.00 63770.43
## range 46.00 37.17 5.00 62648.55
## sum 52459.00 41027.62 1465.00 17755824.99
## median 39.00 30.40 1.00 9382.03
## mean 39.21 30.66 1.09 13270.42
## SE.mean 0.38 0.17 0.03 331.07
## CI.mean.0.95 0.75 0.33 0.06 649.47
## var 197.40 37.19 1.45 146652372.15
## std.dev 14.05 6.10 1.21 12110.01
## coef.var 0.36 0.20 1.10 0.91
Age
The median age is 39. This means that half of the beneficiaries are younger than 39 and half of them are 39 or older.
BMI
The mean BMI is 30.66. This means that the average BMI is about 30.66.
Children
The standard deviation of the number of dependents (children) a beneficiary has is 1.21. This means that 68.3% of beneficiares have within 1.21 children away from the mean number of children, 95.45% within 2.42, and 99.73% within 3.63. Standard deviation is a measure of how varied the number of dependents is.
Charges
The coefficient of variation for the amount charged is .91. The coefficient of variation measures how variable the amount charged is. .91 is a large coefficient of variation so the amount is very variable.
Age
ggplot(cleanedData, aes(x=age)) +
geom_histogram(binwidth = 5, colour="black", fill = "darkseagreen3") +
ggtitle("Distribution of Age") +
xlab("Age (years)") +
ylab("Frequency") +
theme_dark()
Description
You can see that the distribution of Age is slightly right skewed. The minimum value is 18 and the maximum is 64. The data could potentially be bimodal since there is a mode around 20, but the data also increases again close to 50.
Sex
ggplot(cleanedData, aes(x=GenderFactor)) +
geom_histogram(colour="black", fill = "cornflowerblue", stat = "count") +
ggtitle("Distibution of Gender") +
xlab("Gender") +
ylab("Frequency") +
theme_grey()
## Warning in geom_histogram(colour = "black", fill = "cornflowerblue", stat =
## "count"): Ignoring unknown parameters: `binwidth`, `bins`, and `pad`
Description
There seems to be a similar amount of females and males.
BMI
ggplot(cleanedData, aes(x=bmi)) +
geom_histogram(binwidth = 2, colour="black", fill = "slateblue") +
ggtitle("Distribution of BMI") +
xlab("BMI (kg / m ^ 2)") +
ylab("Frequency") +
theme_minimal()
Description
The distribution of BMI appears to be normal with a slight right skew. The mode and mean both seem to be around 28 or 30.
Number of Children/Dependents
ggplot(cleanedData, aes(x=children)) +
geom_histogram(binwidth = 1, colour="black", fill = "midnightblue") +
ggtitle("Distribution of Number of Children") +
xlab("Children") +
ylab("Frequency") +
theme_light()
Description
The distribution is heavily right skewed.
Smoker/Nonsmoker
ggplot(cleanedData, aes(x=SmokerFactor)) +
geom_histogram(colour="black", fill = "hotpink", stat = "count") +
ggtitle("Distibution of Smokers and Nonsmokers") +
xlab("Smoker Status") +
ylab("Frequency") +
theme_linedraw()
## Warning in geom_histogram(colour = "black", fill = "hotpink", stat = "count"):
## Ignoring unknown parameters: `binwidth`, `bins`, and `pad`
Description
There are more non-smokers than smokers.
Region
ggplot(cleanedData, aes(x=RegionFactor)) +
geom_histogram(colour="black", fill = "deeppink4", stat = "count") +
ggtitle("Distibution of Region") +
xlab("Region") +
ylab("Frequency") +
theme_classic()
## Warning in geom_histogram(colour = "black", fill = "deeppink4", stat =
## "count"): Ignoring unknown parameters: `binwidth`, `bins`, and `pad`
Description
There is a relatively even distribution between regions; however, there are slightly more beneficiaries from the southeast.
Charges
ggplot(cleanedData, aes(x=charges)) +
geom_histogram(binwidth = 5000, colour="black", fill = "darkorange") +
ggtitle("Distribution of Charges") +
xlab("Charges ($USD)") +
ylab("Frequency") +
theme_grey()
Description
The distribution of charges is heavily right skewed. This distribution also appears to be bimodal. There is a mode around 8000 and also at 40000.
Other Graphs
ggplot(cleanedData, aes(x=SmokerFactor, y=charges)) +
geom_boxplot(binwidth = 5000, colour="black", fill = "palegreen") +
ggtitle("Charges by Smoker Status") +
theme_dark()
## Warning in geom_boxplot(binwidth = 5000, colour = "black", fill = "palegreen"):
## Ignoring unknown parameters: `binwidth`
Description
When you compare charges to whether or not the beneficiary smokes, you can see that smokers tend to have much higher medical costs. The charges of smokers all seem to be more than the bottom 75% (3rd Quartile) of non-smokers.
ggplot(cleanedData, aes(x=charges)) +
geom_histogram(binwidth = 5000, colour="black", fill = "violet") +
facet_wrap(~RegionFactor:GenderFactor, ncol = 2) +
ylab("Frequency") +
theme_dark()
Description
Comparing region and gender to medical charges, there doesn’t seem to be any significant differences. All are right skewed and have modes around 8000. Some also do seem to have a second mode around 40000, but not as much as the 8000 mode.
scatterplot(charges ~ age,
ylab = "Charges ($USD)",
xlab = "Age (Years)",
smooth = FALSE,
data = cleanedData)
scatterplot(charges ~ age | SmokerFactor,
ylab = "Charges ($USD)",
xlab = "Age (Years)",
smooth = FALSE,
data = cleanedData)
Description
Overall, there doesn’t seem to be any trends in the scatter plot between age and medical charges. There could be a slight positive relationship, but it does look unrelated. When you differentiate even further by smoker status, you can see two positive trends between smokers and non-smokers between age and charges with the smokers having higher charges than non-smokers.