Health insurance charges patients using many characteristics and variables of the person being insured. One question that could be asked is is whether a person’s sex plays a role in determining coverage. This report will try to unravel the question: Does the data set provide evidence that men are charged significantly more for health insurance than women?
A simple comparison of average charges can look convincing, but it can be misleading if men and women differ in other ways that affect cost (i.e. age, weight, habits, etc.) This report will start with a simple comparison, then will progress into linear regression while adding controls one step at a time so we can see if/how the outcome will change
The data set contains 1,338 observations (one per insured person) and 7 variables. This information comes from the Medical Cost Personal data set, compiled from U.S. demographic statistics and is also distributed through Kaggle as well as the textbook Machine Learning with R by Brett Lantz. Each row describes one insured individual and the charge billed to them.
Before doing any analysis, these are the packages that we’ll need. Each one is commented with its function
library(tidyverse) # data manipulation and plotting
library(DT) # interactive display of the data set
library(broom) # tidy summaries of regression models
library(knitr) # clean tables in the report
| Variable | Type | Description |
|---|---|---|
age |
numeric | Age of the insured person (years) |
sex |
categorical | Sex of the insured: female or male |
bmi |
numeric | Body mass index (kg/m²); roughly 18.5–24.9 is considered ideal |
children |
numeric | Number of children/dependents covered by the plan |
smoker |
categorical | Whether the insured smokes: yes or no |
region |
categorical | Residential region in the U.S.: northeast,
northwest, southeast,
southwest |
charges |
numeric | Individual medical costs billed by health insurance (US dollars) |
Next, I’m going to import the data and display it as an interactive table, so the raw observations can be scrolled, sorted, and searched.
insurance <- read_csv("~/Downloads/insurance.csv")
datatable(insurance, options = list(pageLength = 8, scrollX = TRUE))
We begin with the most direct comparison possible. The table below reports the count, mean, median, and standard deviation of charges for both men and women.
insurance %>%
group_by(sex) %>%
summarize(n = n(),
mean_charges = mean(charges),
median_charges = median(charges),
sd_charges = sd(charges)) %>%
kable(digits = 0, caption = "Charges by sex")
| sex | n | mean_charges | median_charges | sd_charges |
|---|---|---|---|---|
| female | 662 | 12570 | 9413 | 11129 |
| male | 676 | 13957 | 9370 | 12971 |
On average, men are charged about $13,957 and women about $12,570, a gap of roughly $1,387. The medians, however, are very close (about $9,400 each), and the standard deviations are large (each over $11,000). That tells us that charges are strongly right-skewed: that most people are charged modestly, while a minority have very high charges that pull the mean up. To see this directly, I’ll compare the full distributions using a boxplot.
ggplot(insurance, aes(x = sex, y = charges, fill = sex)) +
geom_boxplot(show.legend = FALSE, alpha = 0.7) +
labs(title = "Insurance Charges by Sex",
x = "Sex", y = "Charges (US dollars)")
The boxes for the two sexes are almost on top of each other, and both groups have many high outliers. Men’s upper tail extends a bit further, which is where the higher mean comes from. Based on this comparison alone, it would be a little early to conclude that men are charged more. The typical (median) man and woman pay very similar amounts, and a difference in means in a skewed sample could come from a few extreme observations or from differences between the groups other than sex.
Therefore, in order to test whether the $1,387 gap is larger than
chance alone would produce, we fit a simple linear regression of charges
on sex. The coefficient on sexmale is the difference in
mean charges between men and women.
m1 <- lm(charges ~ sex, data = insurance)
summary(m1)
##
## 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
The coefficient for sexmale is $1,387 (standard error
$661), with a p-value of about 0.036. Since this value is below the 0.05
cutoff, we can conclude that this difference is statistically
significant. However, the model explains almost nothing given that the
R² is 0.0033, meaning that sex accounts for only about 0.3% of the
variation in charges, and the RSE is about $12,095, which means that
predictions based on sex alone are typically off by over $12,000.
Statistical significance tells us the gap is unlikely to be pure chance.
It does not tell us the gap is caused by sex or that it is large in a
practical sense.
There is one main reason to not to solely rely on this outcome and that is the possibility of confounding variables. Men and women may differ in other characteristics that drive charges (age, BMI, habits). If so, the sex coefficient is hiding their effects. Therefore, if we want to find a more accurate model that will tell us more information, we are going to have to account for the possibility of the existence of confounding variables(s).
A confounding variable is going to be related to both the explanatory variable (sex) and to the outcome (charges). If it is ignored, its effect is wrongly credited to the explanatory variable.
Age is a natural variable to consider since older people tend to have more health problems and therefore higher medical costs. Also, if the men in the data were on average older than the women, then that could also impact the relationship in some way. Hence, this will require the implementing of two conditions: (1) age must be related to charges, and (2) age must differ by sex.
insurance %>%
group_by(sex) %>%
summarize(mean_age = mean(age), median_age = median(age)) %>%
kable(digits = 1, caption = "Age by sex")
| sex | mean_age | median_age |
|---|---|---|
| female | 39.5 | 40 |
| male | 38.9 | 39 |
cor(insurance$age, insurance$charges)
## [1] 0.2990082
Next we plot charges against age, with a separate fitted line for each sex.
ggplot(insurance, aes(x = age, y = charges, color = sex)) +
geom_point(alpha = 0.3, size = 1.5) +
geom_smooth(method = "lm", se = FALSE, linewidth = 1.2) +
scale_y_continuous(labels = scales::dollar) +
labs(title = "Insurance Charges vs. Age by Sex",
x = "Age (years)", y = "Charges (US dollars)", color = "Sex")
The first condition is clearly relevant, indicating that charges rise with age and the lines for men and women slope upward in nearly the same way. The scatterplot also shows distinct horizontal bands of points, which seems to hint at other important variables that we haven’t yet considered.
On the other hand, the second condition does not seem to hold much weight. The average age of women (39.5) and men (38.9) is nearly identical. So although age is a plausible confounding variable in theory, it does not look like it explains the sex difference here. We confirm this by controlling for age directly.
To control for age, I;m going add it to the model The coefficient on
sexmale now represents the difference in expected charges
between a man and a woman of the same age.
m2 <- lm(charges ~ sex + age, data = insurance)
summary(m2)
##
## 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 male coefficient is $1,539 (p ≈ 0.015), which is slightly larger than before, making it a little more significant. Each additional year of age is associated with about $259 more in charges (p < 0.001). This aligns with the age comparison due to the fact that men and women have about the same average age, so controlling for age isn’t going to shrink the gap. The model improves (R² rises to 0.093 and the RSE falls to about $11,539), but 91% of the variation in charges is still unexplained.
That leaves a reason for caution since most of the variation is unexplained, other important factors are being left out, and if any of them differ between men and women, the sex coefficient may still be biased. The skewed residuals have not gone away either. So we turn to the health variables in the data. With that being said it does look like we are slowly starting to get closer to our answer.
Next, we will try to see if the health variables in the data makes any difference.
Two variables in the data describe a person’s health, that being BMI and smoker status. Before adding them to the model, I’ll check to see whether men and women differ on them.
insurance %>%
group_by(sex) %>%
summarize(mean_bmi = mean(bmi),
pct_smokers = 100 * mean(smoker == "yes")) %>%
kable(digits = 1, caption = "Health characteristics by sex")
| sex | mean_bmi | pct_smokers |
|---|---|---|
| female | 30.4 | 17.4 |
| male | 30.9 | 23.5 |
Men have a slightly higher average BMI (30.9 vs. 30.4) and a noticeably higher share of smokers (23.5% vs. 17.4%). Now we look at how smoking and BMI relate to charges.
ggplot(insurance, aes(x = bmi, y = charges, color = smoker)) +
geom_point(alpha = 0.5) +
labs(title = "Smokers Are Charged Far More, and BMI Matters More for Smokers",
x = "BMI", y = "Charges (US dollars)", color = "Smoker") +
scale_y_continuous(labels = scales::dollar) +
theme_minimal()
Looking at this scatterplot, it is pretty apparent that smoking is a dominant driver of charges; nearly all of the highest charges belong to smokers, and among smokers there is a visible jump in charges once BMI exceeds about 30. This makes smoking a much stronger candidate for confounding variable than age. Men smoke more often and smokers are charged far more, so smoking could produce a “male premium” even if sex has no direct effect.
With that being said, I’m now going to add both health variables to the model.
m3 <- lm(charges ~ sex + age + bmi + smoker, data = insurance)
summary(m3)
##
## 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
This outcome exhibits a huge difference compared to previous
variables. The coefficient on sexmale is now -$109 with a
p-value of 0.74. Holding age, BMI, and smoking status fixed, there is no
evidence of any difference in charges between men and women. In
contrast:
The fit is much better, with this scatterplot having an adjusted R² = 0.7467, meaning that these variables together explain about 75% of the variation in charges, which is way more insightful than the age variable that only accounted for about 9% of the variation. Also, the RSE drops from about $11,500 to about $6,094, further proving that this model is more impactful than before.
This model is far more credible than the earlier ones, but it is still not as good as it could be. To check its assumptions, we plot the residuals against the fitted values. If the model is sufficient, we would see a shapeless cloud around zero.
ggplot(augment(m3), aes(x = .fitted, y = .resid)) +
geom_point(alpha = 0.5) +
geom_hline(yintercept = 0, color = "red") +
labs(title = "Residuals vs. Fitted Values (Model with Health Controls)",
x = "Fitted charges (US dollars)", y = "Residual (US dollars)") +
theme_minimal()
The residuals seem to show clear structure rather than random scatter. Thsi plot indicates a slight curved pattern, including a band of large positive residuals, and the spread changes across the range of fitted values. This indicates the model’s assumptions are not fully met. For example, the effect of BMI on charges seems to depend on smoking status which is something that the earlier scatterplot suggested.
To see whether fixing this can help change the conclusion, I’m going to try to allow smoking and BMI to interact.
m3b <- lm(charges ~ sex + age + bmi * smoker, data = insurance)
tidy(m3b) %>% kable(digits = 3, caption = "Model with a BMI x smoker interaction")
| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
| (Intercept) | -2071.077 | 840.644 | -2.464 | 0.014 |
| sexmale | -473.495 | 269.612 | -1.756 | 0.079 |
| age | 266.372 | 9.612 | 27.713 | 0.000 |
| bmi | 7.969 | 25.044 | 0.318 | 0.750 |
| smokeryes | -20193.152 | 1666.491 | -12.117 | 0.000 |
| bmi:smokeryes | 1435.608 | 53.242 | 26.964 | 0.000 |
glance(m3b) %>% select(r.squared, adj.r.squared, sigma) %>% kable(digits = 4)
| r.squared | adj.r.squared | sigma |
|---|---|---|
| 0.8367 | 0.836 | 4903.556 |
The interaction term makes a big difference to the fit: R² rises to about 0.837 and the RSE falls to about $4,904. The sex coefficient is now about −$474 with a p-value of about 0.08. That is closer to the 5% cutoff than before, but it is still not significant, and its sign is negative, meaning that if anything women are charged slightly more than men with the same age, BMI, and smoking status. So the better-fitting model still doesn’t quite give us enough support for the claim that men are charged more.
The final two variables have not been used yet are the number of children and the region. Therefore, I’m going to try to add both variables to the model to see whether either of them has a significant effect on charges.
m4 <- lm(charges ~ sex + age + bmi + smoker + children + region, data = insurance)
summary(m4)
##
## 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
Children does appear to have a significant effect, with each additional dependent being associated with about $476 more in charges (p ≈ 0.0006). Region gives us a little bit more of a mixed answer. The southeast and southwest are each charged about $1,000 less than the northeast baseline (p ≈ 0.03 and 0.045), while the northwest is not distinguishable from the northeast (p ≈ 0.46). Since region is one categorical variable spread over three coefficients, I think a better test would include a model showing whether region as a whole improves the fit. We will be sure to compare the model without region to the model with it to see if there is actually any significance.
m_noregion <- lm(charges ~ sex + age + bmi + smoker + children, data = insurance)
bind_rows(
glance(m_noregion) %>% mutate(model = "Without region"),
glance(m4) %>% mutate(model = "With region")
) %>%
select(model, r.squared, adj.r.squared, sigma) %>%
kable(digits = 4, col.names = c("Model", "R²", "Adjusted R²", "RSE"),
caption = "Does adding region improve the model?")
| Model | R² | Adjusted R² | RSE |
|---|---|---|---|
| Without region | 0.7497 | 0.7488 | 6069.725 |
| With region | 0.7509 | 0.7494 | 6062.102 |
Adding the region variable raises R² only from about 0.7497 to 0.7509 and lowers the RSE from about $6,070 to $6,062. These changes are quite small, so it’s evident that region as a whole doesn’t add a lot of much explanatory power once age, BMI, smoking, and children are in the model. Therefore, I would probably conclude that region has at most a weak effect on charges. Children, by contrast, is clearly much more significant.
In regards for our question, adding these variables barely changes the sex coefficient, which is now $131 (p ≈ 0.69). The fit improves only slightly (R² from 0.7475 to 0.7509, RSE about $6,062), so the sex result appears to not depend on whether children and region are included.
Finally, I’m goingto summarize what we’ve learned with a simple bar chart of average charges for each combination of smoking status and sex.
insurance %>%
group_by(smoker, sex) %>%
summarize(mean_charges = mean(charges), .groups = "drop") %>%
ggplot(aes(x = smoker, y = mean_charges, fill = sex)) +
geom_col(position = "dodge") +
scale_y_continuous(labels = scales::dollar) +
labs(title = "Average Charges by Smoking Status and Sex",
x = "Smoker", y = "Average charges (US dollars)", fill = "Sex") +
theme_minimal(base_size = 12)
The gap between smokers and non-smokers is enormous: smokers are charged roughly $31,000 to $33,000 on average, as opposed to about $8,000 to $9,000 for non-smokers. The difference between men and women within each smoking group is small by comparison. Among non-smokers, women are actually charged slightly more (about $8,800 vs. $8,100), while among smokers men are charged somewhat more (about $33,000 vs. $30,700). Since men are more likely to be smokers, they land in the high-charge group more often, which is what created the overall “male premium” in the raw averages. Smoking, not sex, is the main driver of charges in this data set.
Does the data set provide evidence that men are charged significantly more for health insurance than women? No.
The raw numbers show that men are charged about $1,387 more on average, and a simple regression finds this difference statistically significant (p ≈ 0.036). Controlling for age did not remove the gap (it grew slightly to $1,539, p ≈ 0.015), because men and women in the data have almost the same average age. The turning point came when we controlled for smoking** and BMI. Men in this data set are significantly more likely to smoke (23.5% vs. 17.4%), and smoking adds about $23,800 to expected charges. Once we compare men and women with the same age, BMI, and smoking status, the estimated difference becomes *$109 (p ≈ 0.74), and it stays near zero and insignificant (−$131, p ≈ 0.69) after also controlling for children and region. In other words, the initial “male premium” came from men smoking more, not from sex itself.