Economics for Tech People — Supply (Part 2)
By: Tyler Harris
Tags: Technology, Education, R, Programming, Economics

The Code

Load Libraries

To work through the project, we will need the readxl and tidyverse packages. If you do not have them installed in your R environment, simply remove the “#” sign before the “install.packages…” lines of code [1]. Once they are installed, you will not need to install them again on your machine.

Here’s the code:

# Load Libraries # If you do not have readxl or tidyverse installed, uncomment the following lines

# install.packages("readxl")

# install.packages("tidyverse")# Load Libraries

require(readxl)

require(tidyverse)

Check Data Import

Sometimes Excel files are temperamental when they interact with R, especially RStudio. Use the code below to import the data and check it looks okay.

Here’s the code:

# Import Data

Supply_Data <- read_excel("Supply_Data.xlsx") # Check data import

head(Supply_Data)

Here’s the output:

Screenshot of the first six rows of our supply data in RStudio on a Mac

If you import the data and the Price and Qs_# are not the column labels and are the first row of data, you will probably see something like X1 X2 X3, etc. as the column headings. If that happens, you can follow the same process that I described in “The Setup” section of the previous article Economics for Tech People — Demand (Part 1). That will show you how to import the data a different way to get caught up to this article.

Quick Test Plot

It is always a good idea to do a quick graph just to make the the data looks good and is cooperating. It does not have to be pretty, but it can help diagnose problems early. Since we are looking at supply curves, we should see a general trend of having higher quantity supplied (Qs_#) values as the price increases because of the Law of Supply [2]. Let’s pick one of our hypothetical firms and take a look.

Here’s the code:

# Test plot

plot(Supply_Data$Qs_2, Supply_Data$Price, type = "l", main = "Test Plot", xlab = "Quantity", ylab = "Price")

Here’s the output:

Quick plot of one of the Quantity columns vs. price to check the data in RStudio on a Mac

For bonus points, go ahead and try changing the firm number and making graphs of different firms on your own. I chose firm 2 here, but any number between 1 and 10 will work and give different looking curves.

Plot All Individual Supply Curves

The wonderful part about good code is that it can be reused and modified quickly and easily. To accomplish the goal of plotting all the individual supply curves, we will use the same stack() function in conjunction with the data frame object class inside of the facet_wrap() function within ggplot2 [1]. Renaming the columns helps with our sanity and readability because R will automatically rename the columns into something we do not want them to be called [1].

We do this because ggplot2 likes data in a certain way, so we can get a better data structure to make use of its functions rather than trying to make the functions do something they were not intended to do directly.

Here’s the code:

# Wrangle the data into a ggplot friendly structure

Wrangled_Supply_Data <- data.frame(Price = Supply_Data$Price, stack(Supply_Data[2:11]))

names(Wrangled_Supply_Data)[2] <- "Quantity"

names(Wrangled_Supply_Data)[3] <- "Qs_num" # Check the data

head(Wrangled_Supply_Data)

Here’s the output:

First six rows of the wrangled and labeled supply data in a ggplot friendly structure in RStudio on a Mac

Now that we have wrangled our data into an appropriate structure, facet_wrap() will cooperate much better with us.

Here’s the code:

# Plot the individual supply curves

ggplot(data = Wrangled_Supply_Data, aes(x = Quantity, y = Price)) +

geom_line(color = "darkred", size = 1) +

geom_point(color = "darkred") +

facet_wrap(. ~ Qs_num)

Here’s the output:

Screenshot of a plot of all the individual supply curves in the data set in RStudio on a Mac

Let’s think about what this means in the context of many firms supplying similar SaaS solutions in this market. Each of the 10 firms has a different combination of quantity supplied at each price [2].

According to the Law of Supply, suppliers want to supply more units as the price increases [2]. Intuitively that makes sense. The whole point of being in business is to earn money, so selling more units at higher prices is going to be the plan for any profit-seeking enterprise [2]. We that reflected for each firm.

We also see that often our firms will only start supplying after a certain price. Profit-seeking businesses are not charities, so they will only start to supply units once it becomes worth it for the firms [2].

Firms, especially those supplying SaaS services in a competitive market, have a certain level of cost to start selling and have increasing costs over time, which is the rationale behind needing higher prices to justify supplying more units [2].

Market Supply

Just like the market demand in the previous article, the market supply is the sum of all the individual firms’ supply curves [2]. For each price, we add the quantities of all 10 firms together to get the final numbers for the market supply curve [2].

Here’s the code:

# Create market supply

Market_Supply <- data.frame(Price = Supply_Data$Price, Market_Supply = rowSums(Supply_Data[2:11])) # Check the data

head(Market_Supply)

Here’s the output:

Screenshot of the market supply in RStudio on a Mac

Plot the Market Supply

We will continue using ggplot and add a few styling components to visualize the market supply.

Here’s the code:

# Plot market supply curve

ggplot(data = Market_Supply, aes(x = Market_Supply, y = Price)) +

geom_line(color = "darkred", size = 1) +

geom_point(color = "darkred") +

geom_vline(xintercept = 0) +

geom_hline(yintercept = 0) +

ggtitle("Market Supply Curve") +

theme(plot.title = element_text(hjust = 0.5))

Here’s the output:

Screenshot of the plot of the market supply in RStudio on a Mac

Just like the market demand curve in the first article, I see three distinct areas in the curve where changing the price changes the quantity supplied in dramatically different ways.

This leads us to the idea of elasticity.

Elasticity

The fundamental idea with elasticity is all about determining how much the quantity supplied changes for each unit of price change [3]. In the first article about demand, the math is explained in much more detail. What we are doing here is taking a look at different regions of the curve.

When the supply curve is relatively inelastic, it means that the quantity supplied changes less per unit than price [3]. When the supply curve is relatively elastic, the quantity supplied changes more per unit than price [3].

This curve has three distinct regions. There is a relatively inelastic region at the beginning, a middle section where the transition happens, and a relatively elastic region at the higher quantities [3].

Again, just like the article on demand, I assigned elasticity zones to illustrate the point concisely.

Here’s the code:

# Add Elasticity zones

# 10-5.5 zone 1

# 5-4 zone 2

# 3.5-0 zone 3 Market_Supply$Elasticity_Zone <- as.character(c(1,1,1,1,1,1,1,1,1,1,2,2,2,3,3,3,3,3,3,3,3))

Market_Supply

Here’s the output:

Screenshot of the market supply with elasticity zones in RStudio on a Mac

One of my favorite little tricks here is to assign the elasticity zones as characters rather than numbers with the as.character() function. This is going to make ggplot cooperate with the zones because it realizes that they are not numbers with which to do math and are categorical in nature [1].

Plot Market Supply with Elasticity Zones

We are going to add on to our previous market supply code by adding color = Elasticity_Zone to the aes() function so that ggplot knows to assign a different color to each zone [1]. We also will add the geom_smooth() function with the method = “lm” parameter to have it make linear models for each elasticity zone [1].

Here’s the code:

# Plot market supply with elasticity

ggplot(data = Market_Supply, aes(x = Market_Supply, y = Price, color = Elasticity_Zone)) +

geom_line(size = 1) +

geom_point() +

geom_smooth(method = "lm") +

geom_vline(xintercept = 0) +

geom_hline(yintercept = 0) +

ggtitle("Market Supply with Elasticity Zones") +

theme(plot.title = element_text(hjust = 0.5))

Here’s the output:

Screenshot of the elasticity zones in the market supply in RStudio on a Mac

Linear Models and Elasticity

Since we took a look at the specifics of linear models with the lm() function in the article about demand, this time we will use generalized linear models using the glm() function to examine qualities about the three elasticity zones.

Let’s take a look at the code and output for the three elasticity zones and a generalized linear model of the entire curve to better understand the concept.

Here’s the code:

### Create Linear Models ### # Filter Data

Zone_1_glm_data <- Market_Supply %>%

filter(Elasticity_Zone == 1) # Create linear model

Zone_1_glm <- glm(Market_Supply ~ Price, data = Zone_1_glm_data) # Create and print summary

summary(Zone_1_glm)

Here’s the output:

Screenshot of summary statistics for the generalized linear model of Elasticity Zone 1 in RStudio on a Mac

Here’s the code:

# Filter Data

Zone_2_glm_data <- Market_Supply %>%

filter(Elasticity_Zone == 2) # Create linear model

Zone_2_glm <- glm(Market_Supply ~ Price, data = Zone_2_glm_data) # Create and print summary

summary(Zone_2_glm)

Here’s the output:

Screenshot of summary statistics for the generalized linear model of Elasticity Zone 2 in RStudio on a Mac

Here’s the code:

# Filter Data

Zone_3_glm_data <- Market_Supply %>%

filter(Elasticity_Zone == 3) # Create linear model

Zone_3_glm <- glm(Market_Supply ~ Price, data = Zone_3_glm_data) # Create and print summary

summary(Zone_3_glm)

Here’s the output:

Screenshot of summary statistics for the generalized linear model of Elasticity Zone 3 in RStudio on a Mac

Here’s the code:

# Compare against a GLM for the entire market supply # Create linear model

All_supply_glm <- glm(Market_Supply ~ Price, data = Market_Supply) # Create and print summary

summary(All_supply_glm)

Here’s the output:

Screenshot of summary statistics for the generalized linear model of all of market supply in RStudio on a Mac

We want to look at two components in all of these outputs.

First, the Estimate column for the Price coefficient tells us the slope of the line. Simply, the higher the number, the more relatively inelastic the curve is [1][3]. The smaller the number, the more relatively elastic the curve is [1][3]. When referencing the first three outputs, we have good predictive power over the quantity supplied at different sections of the curve [1].

Second, we want to look at the AIC (Akaike Information Criterion) value at the bottom. The closer the AIC is to 0, the better fit of a model we have. In the first three elasticity zones, we have extremely small AIC values indicating that the model tracks closely with the line [1]. When trying to get a linear model for all the data, the AIC value is ~10–15x higher. That means that we have a significantly worse fit [1].

We can see how ridiculous it is to assume an entire market’s elasticity when dealing with non-textbook data.

Here’s the code:

# Look at the linear model line on the graph without looking at elasticity of different parts of the curve

# Plot market supply with elasticity

ggplot(data = Market_Supply, aes(x = Market_Supply, y = Price)) +

geom_line(size = 1) +

geom_point() +

geom_smooth(method = "lm", color = "darkred") +

geom_vline(xintercept = 0) +

geom_hline(yintercept = 0) +

ggtitle("Linear Model of Market Supply without Elasticity Zones") +

theme(plot.title = element_text(hjust = 0.5))

Here’s the output:

Poorly fit linear model visualization over the market supply data in RStudio on a Mac

Maximize Total Revenue

Unlike the answer we got in the demand article, maximizing total revenue from a supplier perspective is all about selling more units at higher prices [3]. When we take a look at the total revenue generated at each price level, we can see this idea reflect the Law of Supply and its predictions [3].

Here’s the code:

# Maximize Total Revenue

Market_Supply$Total_Revenue = Market_Supply$Price * Market_Supply$Market_Supply # Check the data

Market_Supply

Here’s the output:

Screenshot of the total revenue for the market supply data in RStudio on a Mac

Plot the Total Revenue Curve

When visualizing this data, it becomes quite apparent that once we hit the relatively elastic portion of the supply curve that as we sell more units at higher prices, total revenue dramatically increases [3].

Here’s the code:

# Plot market supply with elasticity

ggplot(data = Market_Supply, aes(x = Price, y = Total_Revenue, color = Elasticity_Zone)) +

geom_line(size = 1) +

geom_point() +

geom_vline(xintercept = 0) +

geom_hline(yintercept = 0) +

ggtitle("Total Revenue Curve for Market Supply with Elasticity Zones") +

theme(plot.title = element_text(hjust = 0.5))

Here’s the output: