library(tidyverse)
custdata <- read_csv("https://bcdanl.github.io/data/custdata_rev.csv")
ny_customers <- custdata |>
filter(state_of_res == "New York",
income > 10000, income < 250000,
age >= 25,
housing_type == "Rented" |
housing_type == "Homeowner with mortgage/loan" |
housing_type == "Homeowner free and clear")Classwork 15
Visualizing Data with Log Transformations
Complete the questions and submit your responses on the corresponding Brightspace page.
Complete the code in an R script and keep your brief written interpretations with it. This page contains its own setup and data definitions; no earlier classwork is required.
Setup and data
Use the same ny_customers subset as in Classwork 14. It keeps New York customers aged at least 25 in the three listed housing groups, with recorded income strictly above $10,000 and below $250,000. Each row represents one customer record.
| Variable | Meaning |
|---|---|
income |
Recorded customer income in U.S. dollars |
age |
Customer age in years |
The filter keeps 3,015 New York customer records with recorded income strictly above $10,000 and below $250,000. A base-10 logarithm is defined for all incomes in this subset.
Filling-in-the-blank: 1. Compare income histograms
Make one histogram of income in dollars and one of the base-10 logarithm of income. Income is on the horizontal axis here so the bar heights can show counts.
ggplot(data = ny_customers,
mapping = aes(x = income)) +
geom_histogram(fill = "steelblue", color = "white")
ggplot(data = ny_customers,
mapping = aes(x = ____(income))) +
geom_histogram(fill = "steelblue", color = "white")What does one bar count in each histogram? Compare where most of the records fall and which tail is longer in each plot.
The missing function is log10(). Both histograms count customer records. The displayed figures add readable axis labels and dollar formatting; those details are not needed to complete the question.
Histogram of income in dollars:
ggplot(data = ny_customers,
mapping = aes(x = income)) +
geom_histogram(fill = "steelblue", color = "white")Histogram of log-transformed income:
ggplot(data = ny_customers,
mapping = aes(x = log10(income))) +
geom_histogram(fill = "steelblue", color = "white")The original histogram is concentrated at lower dollar amounts: 1,773 of 3,015 incomes are below $50,000, and a long right tail reaches toward $248,000. The log histogram has a broader center around log values 4.7 (about $50,000), with higher dollar gaps compressed. Its left and right tails are roughly similar in length, with only slight right skew. The transformation changes the display, not the 3,015 records or their stored income values. Each bar counts customer records, not dollars.
Filling-in-the-blank: 2. Compare income by age
Put age on the horizontal axis in both plots. Put income in dollars on the vertical axis in the first plot, and the base-10 logarithm of income on the vertical axis in the second plot. Each plot uses a steel blue default smooth curve and a dashed orange straight fit. Each line has a matching lightly shaded ribbon.
ggplot(data = ny_customers,
mapping = aes(x = age,
y = income)) +
geom_point(alpha = 0.2) +
geom_smooth(color = "steelblue", fill = "steelblue", alpha = 0.15) +
geom_smooth(method = "lm", color = "orange", fill = "orange",
alpha = 0.15, linetype = "dashed")
ggplot(data = ny_customers,
mapping = aes(x = age,
y = ____(income))) +
geom_point(alpha = 0.2) +
geom_smooth(color = "steelblue", fill = "steelblue", alpha = 0.15) +
geom_smooth(method = "lm", color = "orange", fill = "orange",
alpha = 0.15, linetype = "dashed")Use this original-scale plot as a reference while you make the transformed plot.
At a given age, how does the log transformation change the vertical spacing between lower incomes and between higher incomes? In each plot, compare the shape of the blue curve with the orange straight line. What do the two fits estimate at a given age? State what the vertical axis shows in each plot.
Doubling on a log axis. Consider these illustrative recorded incomes:
| Recorded income (USD) | 12,500 | 25,000 | 50,000 | 100,000 |
|---|---|---|---|---|
| Approximate base-10 logarithm of income | 4.10 | 4.40 | 4.70 | 5.00 |
What original income does a transformed value of 5 represent? From one value to the next, are the percentage increases equal? Are the dollar increases equal? Which scatterplot places these doubling steps at approximately equal vertical distances?
The missing function in the transformed scatterplot is again log10(). Each scatterplot includes a steel blue default smooth curve and an orange dashed straight fit, each with a matching translucent ribbon. The displayed figures add readable axis labels and dollar formatting.
Scatterplot with income in dollars:
ggplot(data = ny_customers,
mapping = aes(x = age,
y = income)) +
geom_point(alpha = 0.2) +
geom_smooth(color = "steelblue", fill = "steelblue", alpha = 0.15) +
geom_smooth(method = "lm", color = "orange", fill = "orange",
alpha = 0.15, linetype = "dashed")Scatterplot with log-transformed income:
ggplot(data = ny_customers,
mapping = aes(x = age,
y = log10(income))) +
geom_point(alpha = 0.2) +
geom_smooth(color = "steelblue", fill = "steelblue", alpha = 0.15) +
geom_smooth(method = "lm", color = "orange", fill = "orange",
alpha = 0.15, linetype = "dashed")Both scatterplots show substantial income variation among customers of the same age. For example, the 71 customers recorded as age 50 have incomes from $10,670 to $243,800. The blue default curves rise from the youngest ages into roughly the 40s, decline through older ages, and turn upward at the sparsely represented oldest ages. The late upward turns deserve caution because few customers are that old. The orange dashed straight lines slope slightly downward overall and cannot show those bends.
At a given age, both summaries in the original plot estimate average income in dollars; both summaries in the transformed plot estimate the average base-10 logarithm of income, whose vertical values are logarithms rather than dollars. Each faint blue or orange ribbon shows uncertainty around its fitted average, not the full spread of individual incomes. The broad rise-then-fall pattern of the blue curve remains visible after the transformation, while higher dollar gaps take less vertical space.
The original scatterplot puts a $12,500-to-$50,000 increase in a shorter vertical interval than a $50,000-to-$200,000 increase, even though both are fourfold changes. On the log-income plot, those two fourfold changes span the same vertical distance. Lower incomes spread farther apart and higher dollar gaps compress. Every included income is above $10,000; the smallest is $10,020.
Doubling example
A transformed value of 5 represents $100,000, because log10(100000) = 5. Incomes double from $12,500 to $25,000 to $50,000 to $100,000, so each step is a 100% increase. The dollar increases are $12,500, $25,000, and $50,000.
The approximate base-10 log-income values are 4.10, 4.40, 4.70, and 5.00. Each doubling takes about 0.30 units of vertical space in the log-income scatterplot; the vertical dollar gaps widen in the original scatterplot. The same values also have roughly equal horizontal spacing in the log-income histogram because income is mapped to that chart’s horizontal axis.
