Classwork 14

Relationship Between Two Numeric Variables

Author

Byeong-Hak Choe

Published

November 9, 2026

NoteNote

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

library(tidyverse)
library(ggthemes)

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")

The filtered table contains 3,015 New York customer records: 1,123 renters, 1,217 homeowners with a mortgage or loan, and 675 homeowners who own free and clear. We keep customers aged at least 25 with recorded income above $10,000 and below $250,000. These selected records do not represent all New York residents.

Variable descriptions

Each row represents one customer. The table describes the columns in custdata and its filtered subset, ny_customers.

Variable Description
custid Unique customer identifier, stored as text.
sex Reported sex: Female or Male.
is_employed Employment status: TRUE = employed; FALSE = unemployed; NA = unknown or not applicable.
income Customer income in U.S. dollars.
marital_status Marital status, such as married, never married, divorced/separated, or widowed.
housing_type Housing situation, such as renting or owning a home.
recent_move Whether the customer moved in the past year: TRUE = yes; FALSE = no.
num_vehicles Number of vehicles in the household; 6 means six or more.
age Customer age in years.
state_of_res State of residence, including the District of Columbia.
gas_usage Monthly gas-bill field; values 4–999 represent dollar amounts, with special codes below.
health_ins Health-insurance status: TRUE = insured; FALSE = uninsured.

For gas_usage, 1 means included in rent or condo fees, 2 means included in the electricity payment, and 3 means no charge or gas not used. In any column, NA represents a missing or not-applicable value.

Filling-in-the-blank: 1. Age and recorded income

Make a scatterplot with age on the horizontal axis and recorded income on the vertical axis. Set alpha = 0.2 in the point layer so individual points appear lighter and overlapping points appear darker.

ggplot(data = ny_customers,
       mapping = aes(x = ____,
                     y = ____)) +
  ____(alpha = 0.2)

Describe the overall shape or direction, and identify variation among customers of the same age. Where do the darker areas show the most overlap? Point out one unusual or extreme observation worth investigating, without assuming it is an error. State the units on both axes.

ggplot(data = ny_customers,
       mapping = aes(x = age,
                     y = income)) +
  geom_point(alpha = 0.2)

There is no strong straight-line relationship: recorded incomes vary widely across ages, and the overall straight fit slopes only slightly downward. One point represents one customer’s recorded age in years and income in US dollars.

At age 54, 85 customers have incomes ranging from $10,800 to $200,000, showing substantial variation at the same age. The customer aged 60 with income $248,000 is a high-income observation worth checking in context. An unusual point is not automatically a data error.

At alpha = 0.2, single points appear lighter. Where points overlap, they appear darker. The darker band below about $50,000 shows where many records have similar ages and incomes.

Filling-in-the-blank: 2. Curved and straight fitted summaries

Complete three versions of the scatterplot. Keep alpha = 0.2 for the points so the fitted summaries are easy to see. The first version adds the default smooth curve, the second requests a straight fitted line, and the third keeps that line but hides its shaded ribbon.

ggplot(data = ny_customers,
       mapping = aes(x = age,
                     y = income)) +
  geom_point(alpha = 0.2) +
  ____()

ggplot(data = ny_customers,
       mapping = aes(x = age,
                     y = income)) +
  geom_point(alpha = 0.2) +
  geom_smooth(method = "____")

ggplot(data = ny_customers,
       mapping = aes(x = age,
                     y = income)) +
  geom_point(alpha = 0.2) +
  geom_smooth(method = "lm", se = ____)

Compare the shape of the default curve with the straight line: does either show a consistent rise in income with age? Be cautious at the oldest ages, where fewer records may make the curve less stable. At a given age, what does each fitted summary estimate? What does the shaded ribbon represent? Does hiding it mean the estimate is certain?

Default smooth curve and ribbon:

ggplot(data = ny_customers,
       mapping = aes(x = age,
                     y = income)) +
  geom_point(alpha = 0.2) +
  geom_smooth()

Straight fitted line and ribbon:

ggplot(data = ny_customers,
       mapping = aes(x = age,
                     y = income)) +
  geom_point(alpha = 0.2) +
  geom_smooth(method = "lm")

The same straight line with its ribbon hidden:

ggplot(data = ny_customers,
       mapping = aes(x = age,
                     y = income)) +
  geom_point(alpha = 0.2) +
  geom_smooth(method = "lm", se = FALSE)

The blanks are geom_smooth, lm, and FALSE. In these selected records, the default curve rises from younger ages into middle age, then falls through later adulthood. It turns upward again at the sparsely observed oldest ages, where its shape is less stable. The fitted straight line has a modest downward tilt. Thus the line misses the curve’s main rise-and-fall pattern; neither summary passes through every point.

At a given age, each fitted summary estimates average recorded income near that age according to its method. The shaded ribbon represents uncertainty around the estimated average, not the full spread of individual incomes. se = FALSE hides the ribbon; it does not make the estimate certain or change the data.

Filling-in-the-blank: 3. Color and shape identify housing type

Use color and shape to distinguish the three housing types, with alpha = 0.3 for the points. The supplied smoother maps ribbon fill and line type to housing type so each group has its own curve and matching ribbon.

ggplot(data = ny_customers,
       mapping = aes(x = age,
                     y = income,
                     color = ____,
                     shape = ____)) +
  geom_point(alpha = ____, size = 2) +
  geom_smooth(mapping = aes(fill = housing_type,
                            linetype = housing_type),
              alpha = 0.15) +
  scale_color_tableau() +
  scale_fill_tableau()

Where do the three housing groups overlap, and where do you see high or low recorded incomes? Compare the shapes and heights of the group curves at ages observed in all three groups. Explain how color, shape, line type, and transparency help you read the graph, and what the shaded ribbons show. Do these plots establish that housing type causes income? Explain.

ggplot(data = ny_customers,
       mapping = aes(x = age,
                     y = income,
                     color = housing_type,
                     shape = housing_type)) +
  geom_point(alpha = 0.3, size = 2) +
  geom_smooth(mapping = aes(fill = housing_type,
                            linetype = housing_type),
              alpha = 0.15) +
  scale_color_tableau() +
  scale_fill_tableau()

The three housing groups overlap substantially in age and recorded income. Each includes both low- and high-income records. The highest income, $248,000 at age 60, is in the free-and-clear group; a rented customer has a $245,000 record at age 48. Point color and shape identify housing type, while the matching line color, line type, and ribbon fill identify each group’s separate smooth curve. At alpha = 0.3, overlapping points remain partly visible; the faint ribbons show uncertainty around the fitted group averages, not the spread of individual incomes.

The renter curve generally declines with age, while the two homeowner curves rise into middle age and then fall. Compare those curves only where the groups have observations at similar ages; the far-right bends rely on fewer older customers. These are observational records. Neither the curves nor the differences between groups show that housing type causes income, and these selected customers do not represent all New York residents.

Interpretation: 4. Mapping or setting?

Explain the difference between aes(color = housing_type, shape = housing_type) and geom_point(color = "steelblue", shape = 16, alpha = 0.3). Use alpha = 0.3 in the point layer for both versions. Which lets housing type determine color and shape and creates a legend? Which gives every point the same color and shape?

Filling-in-the-blank: 5. Separate housing-type panels and smooth curves

Make one panel per housing type, with a default smooth curve and its shaded ribbon in each panel. Keep the points partly transparent and use the default shared axis scales.

ggplot(data = ny_customers,
       mapping = aes(x = age,
                     y = income)) +
  geom_point(alpha = 0.3) +
  geom_smooth() +
  ____(~ ____)

Compare where the curves rise and fall and how widely incomes vary in each panel. Why are shared axes useful? Do the curves establish that housing type causes a difference in income, or that age itself causes income to change? Explain.

Reading the fitted straight lines

Continue the lecture’s discussion of direction, variation, and unusual points by comparing fitted straight lines across the housing groups. Lecture 8 discussion

Run this supplied version with a separate fitted straight line in each panel.

ggplot(data = ny_customers,
       mapping = aes(x = age,
                     y = income)) +
  geom_point(alpha = 0.3) +
  geom_smooth(method = lm) +
  facet_wrap(~ housing_type)
  1. How does the fitted direction vary across groups? Describe each line as upward, downward, or approximately flat.
  2. To what degree does the slope vary? Compare fitted income at ages 40 and 60 in each panel. Which line changes most over that same 20-year age interval?

As you compare the lines, also examine the vertical spread, uncertainty ribbons, and unusual points. Describe both the fitted pattern and the individual observations.

Discussion of color and facets

To see how the relationship between two numeric variables varies across groups, compare each group’s fitted pattern together with the spread of its observations.

In Q3, color, point shape, line type, and ribbon fill place the housing groups in one panel. This makes it easy to compare fitted incomes at the same age and see where the groups overlap. When many points and ribbons overlap, some details can be harder to follow.

In Q5, facets give each housing group its own panel. This makes each group’s shape, vertical spread, unusual observations, and uncertainty ribbon easier to inspect. Shared age and income scales make comparisons across panels meaningful. Keep those scales the same when comparing how steeply the lines rise or fall. Comparing groups at the same age takes more effort because readers must look back and forth between panels. In the color plot, overlaid groups make overlap and small differences in fitted income easier to see.

The default curves in Q3 and the first Q5 plot summarize the same group relationships. The straight-line follow-up in Q5 focuses attention on direction and slope. For a comparison that changes only the display from colors to facets, use the same fitted method, variables, and observations in both views.

Back to top