Does the data set show that men have significantly higher insurance charges than women? This project compares average changes, then control for age, BMI, smoking, children, as well as the region.
tidyverse and modelr are libraries used for
this project
library(tidyverse)
library(modelr)
library(DT)
The insurance dataset contains related health and
demographic information for insured people.
insurance <- read_csv("D:/Coding with R Class/Excel Files/insurance.csv")
dim(insurance)
## [1] 1338 7
sum(is.na(insurance))
## [1] 0
There are 1,338 observations as well as 7 variables, with no missing values.
| Variable | Type | Description |
|---|---|---|
| age | Numeric | Age in years |
| sex | Categorical | Female or male |
| bmi | Numeric | Body mass index |
| children | Numeric count | Children or dependents |
| smoker | Categorical | Smoking status: yes or no |
| region | Categorical | Northeast, northwest, southeast, or southwest U.S. |
| charges | Numeric | Medical charges in USD |
datatable(insurance, options = list(scrollX = TRUE))
insurance %>%
group_by(sex) %>%
summarize(count = n(), average_charges = mean(charges))
## # A tibble: 2 × 3
## sex count average_charges
## <chr> <int> <dbl>
## 1 female 662 12570.
## 2 male 676 13957.
By first looking at the data, the women are averaging $12,569.58, while men average $13,956.75, with overall difference of $1,387.17. Men also have higher average charges, but the means alone do not show whether the difference is actually significant or are variables able to explain it.
ggplot(insurance) +
geom_boxplot(aes(sex, charges), fill = "orange") +
labs(title = "Insurance Charges by Sex",
x = "Sex", y = "Charges (dollars)")
Looking at the chart, the groups have somewhat overlap and similar
medians. Both have large charges, so the mean cannot describe everyone’s
experience.
sex_model <- lm(charges ~ sex, data = insurance)
summary(sex_model)
##
## Call:
## lm(formula = charges ~ sex, data = insurance)
##
## Residuals:
## Min 1Q Median 3Q Max
## -12835 -8435 -3980 3476 51201
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 12569.6 470.1 26.740 <2e-16 ***
## sexmale 1387.2 661.3 2.098 0.0361 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 12090 on 1336 degrees of freedom
## Multiple R-squared: 0.003282, Adjusted R-squared: 0.002536
## F-statistic: 4.4 on 1 and 1336 DF, p-value: 0.03613
Since the female is the reference category, the intercept represents
the average insurance charges for women. The sexmale
coefficient represents the difference between the average charges for
men and women, which is $1,387.17.
The P-value of 0.0361 is below the significance level of 0.05, suggesting that the difference is significant under the current model. However, we do not have accounted for any other variables that could influence the difference.
The residual represents the difference between the actual and the predicted insurance charges – positive means that the model underestimates the actual charges; negative means that it overestimates them.
sd(insurance$charges)
## [1] 12110.01
The residual is $12,095, which is very high considering that the standard deviation of insurance charges was $12,110. To me, this suggests that using the sex alone will do very little to improve the predictions.
R-squared measures the proportion of the variation, while the adjusted R-square accounts for the number of predictors included. With an R-squared of only 0.00254, the model shows little improvement over the simple use with average insurance charges.
sex_resids <- insurance %>%
add_residuals(sex_model)
ggplot(sex_resids) +
geom_histogram(aes(resid), binwidth = 2000,
fill = "orange", color = "black") +
labs(title = "Residuals from the Sex-Only Model",
x = "Residual (dollars)", y = "Number of Records")
Looking at the distribution of the residuals, we can see that it’s not normal and has a long right tail. To me this represents the understatement of insurance charges for some individuals – although the original difference is significant, the model fit and the distribution give me reason to investigate additional variables before coming to conclusion.
Age is the first additional variable I’ve decided to investigate, considering that in real life age has an impact on insurance pricing. If men were generally older than women, the older individuals had higher insurance charges, this could potentially explain the original difference.
insurance %>%
group_by(sex) %>%
summarize(average_age = mean(age))
## # A tibble: 2 × 2
## sex average_age
## <chr> <dbl>
## 1 female 39.5
## 2 male 38.9
Women are averaging 39.5 years, while men average 38.9 years. Interestingly men have a slightly younger age on average, suggesting that their higher insurance charges cannot be explained by simply being older.
age_model <- lm(charges ~ sex + age, data = insurance)
summary(age_model)
##
## Call:
## lm(formula = charges ~ sex + age, data = insurance)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8821 -6947 -5511 5443 48203
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2343.62 994.35 2.357 0.0186 *
## sexmale 1538.83 631.08 2.438 0.0149 *
## age 258.87 22.47 11.523 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 11540 on 1335 degrees of freedom
## Multiple R-squared: 0.09344, Adjusted R-squared: 0.09209
## F-statistic: 68.8 on 2 and 1335 DF, p-value: < 2.2e-16
After controlling for age, the difference between men and women increases to $1,538.83 – with a p-value of 0.0149. The difference remains significant.
The model also shows that each additional year between the men and women increases $258.87 in insurance charges, holding sex constant.
The standard error decreases to $11,539, while the adjusted r-squared increases to 0.09209. I would say although including the age improves the model, approximately 9.2% of the variation is explained after accounting for the number of predictions, overall leaving most of the variation unexplained.
age_resids <- insurance %>%
add_residuals(age_model)
ggplot(age_resids) +
geom_point(aes(age, resid), alpha = 0.3) +
geom_hline(yintercept = 0) +
labs(title = "Residuals After Controlling for Age",
x = "Age (years)", y = "Residual (dollars)")
Looking at the chart, we can identify several groups of residuals, as well as a number of large positive residuals across different ages – this suggests that there are still other important factors influencing the insurance charges.
BMI and smoking are second two additional variables that could affect and possibly explain differences in insurance charges - before including them, I’ve decided to compare their distributions between both men and women.
insurance %>%
group_by(sex) %>%
summarize(average_bmi = mean(bmi),
percent_smokers = 100 * mean(smoker == "yes"))
## # A tibble: 2 × 3
## sex average_bmi percent_smokers
## <chr> <dbl> <dbl>
## 1 female 30.4 17.4
## 2 male 30.9 23.5
About 23.5% of men are smokers, while 17.4% of women smoke. Men also have a higher average BMI of 30.9, compared to 30.4 for women.
health_model <- lm(charges ~ sex + age + bmi + smoker, data = insurance)
summary(health_model)
##
## Call:
## lm(formula = charges ~ sex + age + bmi + smoker, data = insurance)
##
## Residuals:
## Min 1Q Median 3Q Max
## -12364.7 -2972.2 -983.2 1475.8 29018.3
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -11633.49 947.27 -12.281 <2e-16 ***
## sexmale -109.04 334.66 -0.326 0.745
## age 259.45 11.94 21.727 <2e-16 ***
## bmi 323.05 27.53 11.735 <2e-16 ***
## smokeryes 23833.87 414.19 57.544 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 6094 on 1333 degrees of freedom
## Multiple R-squared: 0.7475, Adjusted R-squared: 0.7467
## F-statistic: 986.5 on 4 and 1333 DF, p-value: < 2.2e-16
After controlling the model for age, BMI, and the smoking status, we can see a definite change in the results – men are now estimated to have $109.04 lower insurance charges than women, with a corresponding p-value of 0.745. Therefore, the difference is no longer statistically significant.
While holding all other variables constant, each individual year of age is associated with an increase of $259.45 in insurance charges, while each additional BMI unit is associated with an increase of $323.05.
The most noticeable result is the difference between smokers and non-smokers. Smokers are estimated to have $23,833.87 higher insurance charges than non-smokers, holding the other variables constant. All three variables are statistically significant, with p-values below 2e-16.
Lastly, the most noticeable result is the difference between the smokers and nonsmokers, where smokers have $23,833.87 higher insurance charges than non-smokers. All three variables are statistically significant, with p-values below 2e-16. To me, this seems like smoking is an important factor in explaining the difference between men and women and insurance.
The standard error decreases to $6,094, while adjusted R-squared increases to 0.74674. It provides a significant improvement to explaining insurance charges.
health_resids <- insurance %>%
add_predictions(health_model) %>%
add_residuals(health_model)
ggplot(health_resids) +
geom_point(aes(pred, resid), alpha = 0.3) +
geom_hline(yintercept = 0) +
labs(title = "Residuals After Age and Health Adjustment",
x = "Predicted Charges (dollars)", y = "Residual (dollars)")
ggplot(health_resids) +
geom_histogram(aes(resid), binwidth = 1500,
fill = "orange", color = "white") +
labs(title = "Residual Distribution After Health Adjustment",
x = "Residual (dollars)", y = "Number of Records")
The model has been improved considerably, but the residuals visuals still show the distinct groups of observations, with an uneven distribution, and a long right tail. While I have more confidence in this model (compared to prior two) these patterns suggest the model’s assumption might not have been satisfied.
To conclude my analysis, I’ve decided to also induce last two variables: the number of children/dependents and the region which the insured individual resides.
full_model <- lm(charges ~ sex + age + bmi + smoker + children + region, data = insurance)
summary(full_model)
##
## Call:
## lm(formula = charges ~ sex + age + bmi + smoker + children +
## region, data = insurance)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11304.9 -2848.1 -982.1 1393.9 29992.8
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -11938.5 987.8 -12.086 < 2e-16 ***
## sexmale -131.3 332.9 -0.394 0.693348
## age 256.9 11.9 21.587 < 2e-16 ***
## bmi 339.2 28.6 11.860 < 2e-16 ***
## smokeryes 23848.5 413.1 57.723 < 2e-16 ***
## children 475.5 137.8 3.451 0.000577 ***
## regionnorthwest -353.0 476.3 -0.741 0.458769
## regionsoutheast -1035.0 478.7 -2.162 0.030782 *
## regionsouthwest -960.0 477.9 -2.009 0.044765 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 6062 on 1329 degrees of freedom
## Multiple R-squared: 0.7509, Adjusted R-squared: 0.7494
## F-statistic: 500.8 on 8 and 1329 DF, p-value: < 2.2e-16
With all variables, each additional child or dependent is associated with an increase of $475.50 in insurance charges. With a corresponding p-value of 0.000577, this relationship is statistically significant.
Northeast is the reference region:
| Region | Adjusted difference from northeast | p-value |
|---|---|---|
| Northwest | −$352.96 | 0.4588 |
| Southeast | −$1,035.02 | 0.0308 |
| Southwest | −$960.05 | 0.0448 |
By using the northeast as the reference region, we can see that the individuals living in the southeast and southwest have significantly lower estimated insurance charges, while the difference between the northwest and northeast is not statistically significant. It’s also important to note that these results represent the individual comparison against the northeast rather than the overall test for the region.
Overall, after all variables are included, men are estimated to have $131.31 lower insurance charges than women, with a corresponding p-value of 0.6933. This further supports the results in prior model, which suggests that there is no significant difference between men and women after controlling for the other variables.
With an R-squared of 0.7509, approximately 75.1% of the variation in insurance charges is explained by the model.
By including the children and region, the model improvement is actually small compared to the improvement sown when including BMI and smoking status.
The F-test has a p-value below the 2.2e-16, indicating that the model as a whole provides a statistically significant improvement over simply predicting the average insurance charges for everyone.
full_resids <- insurance %>%
add_predictions(full_model) %>%
add_residuals(full_model)
ggplot(full_resids) +
geom_point(aes(pred, resid), alpha = 0.3) +
geom_hline(yintercept = 0) +
labs(title = "Residuals from the Full Model",
x = "Predicted Charges (dollars)", y = "Residual (dollars)")
ggplot(full_resids) +
geom_histogram(aes(resid), binwidth = 1500,
fill = "orange", color = "white") +
labs(title = "Residual Distribution from the Full Model",
x = "Residual (dollars)", y = "Number of Records")
By looking at final residual visualization, we can see that some patterns previously identified are still present. Model overall explains a large proportion of the variation, the residuals are approximately normally distributed and don’t have a constant spread. I would say that applying the 68% and 95% rules would not be appropriate.
Initially men had a significantly higher average insurance charges than women, with a difference of $1,387.17. After controlling the BMI and smoking status, the difference was no longer significant. After accounting for all variables, men were estimated to have $131.31 lower insurance charges, with a p-value of 0.6933, consequently giving insufficient evidence to conclude that men have higher insurance charges that women. Lastly, the final model explains about 75% of the variation in insurance charges.