First clear console and load packages

## Clear Console (Use at the beginning or the end) 
rm(list=ls()); pacman::p_unload(); while (!is.null(dev.list())) dev.off();cat("\014")
## The following packages are a base install and will not be unloaded:
## 
## The following packages were not previously loaded:
## NA

# Empirical Project 7 Working in R # These code downloads have been constructed as supplements to the full Doing Economics projects (https://core-econ.org/doing-economics/). You’ll need to download the data before running the code that follows.

Getting started in R

For this project you will need the following packages:

If you need to install either of these packages, run the following code:

if (!requireNamespace("pacman")) install.packages("pacman")
pacman::p_load(readxl, tidyverse)
# install.packages(c("readxl","tidyverse"))

Part 7.1 Drawing supply and demand diagrams

R walk-through 7.1 Importing data into R and creating tables and charts

First we import the data with the read_excel function, using the na = "NA" option to indicate how missing data is recorded.

# Set working directory
setwd("C:/Users/O.GOLSEVEN/OneDrive/SQU - NREC6007 - Agricultural and Food Price Analysis/Project_7 - Supply and Demand")

wm_data <- read_excel("doing-economics-excel-7-1-datafile.xlsx", sheet = "Sheet1", na = "NA")

str(wm_data)
## tibble [22 x 10] (S3: tbl_df/tbl/data.frame)
##  $ Year   : num [1:22] 1930 1931 1932 1933 1934 ...
##  $ log.q  : num [1:22] 1.93 1.89 1.83 1.75 1.78 ...
##  $ log.h  : num [1:22] 1.9 1.88 1.76 1.74 1.78 ...
##  $ log.p  : num [1:22] 2.07 2 1.9 1.97 2.02 ...
##  $ log.pc : num [1:22] 0.976 0.753 0.814 1.007 1.092 ...
##  $ log.pv : num [1:22] 0.367 1.184 1.124 0.993 0.641 ...
##  $ log.w  : num [1:22] 1.46 1.36 1.23 1.2 1.27 ...
##  $ log.n  : num [1:22] 2.09 2.09 2.1 2.1 2.1 ...
##  $ log.y_n: num [1:22] 2.78 2.71 2.59 2.56 2.61 ...
##  $ log.pf : num [1:22] 1.1 1.11 1.13 1.15 1.14 ...

Let’s use the exp function to create the variables p and q from their log counterparts (renamed as log.p and log.q respectively). We also transform the harvest variable (renamed as log.h) and save it as h. The harvest will be at most as large as the crop (q).

names(wm_data) <- c("Year", "log.q", "log.h", "log.p", 
  "log.pc", "log.pv", "log.w", "log.n", "log.yn", "log.pf")
wm_data$p <- 10^(wm_data$log.p)  # Price
wm_data$h <- 10^(wm_data$log.h)  # Harvest quantity
wm_data$q <- 10^(wm_data$log.q)  # Crop quantity

Let’s use plot to produce the chart for the prices, with Year as the horizontal axis variable (xlab) and price (p) as the vertical axis variable (ylab).

# type: "p" = points, "l" = lines, "o" = points and lines
plot(wm_data$Year, wm_data$p, type = "o",
  xlab = "Year", ylab = "Price")  

Now we create the line chart for harvest and crop quantities (the variables h and q, respectively). First, we plot the crop quantities as a dashed line (lty = "dashed"), then use lines to add a solid line for the harvest data. The legend function adds a chart legend at the specified coordinates (the first two arguments in the function).

# type: "p" = points, "l" = lines, "o" = points and lines
plot(wm_data$Year, wm_data$q, type = "o",
  pch = 1, lty = "dashed", 
  xlab = "Year", ylab = "Price")  

# Add the harvest data
lines(wm_data$Year, wm_data$h, type = "o", pch = 16)

# Add a legend
legend(1947.5, 55, legend = c("Crop", "Harvest"),
  col = c("black", "black"), pch = c(1, 16), 
  lty = c("dashed", "solid"), cex = 0.8)

Now let’s do part 7.1.2 Plot supply and demand curves: Q Log Q Supply(logP) Demand(logP) Supply(P) Demand(P) 20
25

95
100
Figure 7.4 Calculating supply and demand.

lnP = −2.0 + 1.7 * lnQ (Supply) lnP = 8.5 - 0.82 * lnQ (Demand)

Convert the values of Q to natural log format (second column of your table) and use these values, along with the numbers in the equations above, to calculate the corresponding values of log P for supply (third column) and demand (fourth column). Convert the log P numbers into the actual prices (fifth and sixth columns).

Q <- seq(20,100,by=5); Q
##  [1]  20  25  30  35  40  45  50  55  60  65  70  75  80  85  90  95 100
lnQ <- log(Q); lnQ
##  [1] 2.995732 3.218876 3.401197 3.555348 3.688879 3.806662 3.912023 4.007333
##  [9] 4.094345 4.174387 4.248495 4.317488 4.382027 4.442651 4.499810 4.553877
## [17] 4.605170
Supply_lnP <- -2.0 + 1.7 * lnQ; Supply_lnP
##  [1] 3.092745 3.472089 3.782036 4.044092 4.271095 4.471326 4.650439 4.812466
##  [9] 4.960386 5.096458 5.222442 5.339730 5.449445 5.552507 5.649676 5.741591
## [17] 5.828789
Demand_lnP <- 8.5 -0.82 * lnQ; Demand_lnP
##  [1] 6.043500 5.860522 5.711018 5.584615 5.475119 5.378537 5.292141 5.213987
##  [9] 5.142637 5.077002 5.016234 4.959660 4.906738 4.857026 4.810156 4.765821
## [17] 4.723760
Supply_P <- exp(Supply_lnP); Supply_P
##  [1]  22.03748  32.20394  43.90532  57.05934  71.60000  87.47266 104.63092
##  [8] 123.03470 142.64881 163.44203 185.38633 208.45638 232.62909 257.88329
## [15] 284.19950 311.55962 339.94686
Demand_P <- exp(Demand_lnP); Demand_P
##  [1] 421.3650 350.9072 302.1786 266.2976 238.6788 216.7050 198.7686 183.8255
##  [9] 171.1666 160.2928 150.8421 142.5453 135.1977 128.6411 122.7508 117.4275
## [17] 112.5908
wm_data2 <- data.frame(Q, lnQ, Supply_lnP, Demand_lnP,Supply_P,Demand_P)

Plot your calculated supply and demand curves on a line chart, with price (P) on the vertical axis and quantity (Q) on the horizontal axis. Make sure to label your curves (for example, using a legend).

# type: "p" = points, "l" = lines, "o" = points and lines
plot(wm_data2$Q, wm_data2$Supply_P, type = "o", pch = 1,col="red", ylim = c(0,500), xlim = c(20,100),
  xaxs = "i", yaxs = "i",xlab = "Quantity", ylab = "Price")  

# Add the harvest data
lines(wm_data2$Q, wm_data2$Demand_P, type = "o", col="blue", pch = 16)

# Add a legend
legend(80, 100, legend = c("Supply", "Demand"),
  col = c("red", "blue"), pch = c(1, 16), 
  lty = c("dashed", "solid"), cex = 0.8)

Now let’s do part 7.1.3 logP=−2.0 + 1.7*logQ + 0.4

Add the new supply curve to your line chart and interpret the outcomes, as follows: Create a new column in your table from Question 2 called ‘New supply (log P)’, showing the supply in terms of log prices after the shock. Make another column called ‘New supply (P)’ showing the supply in terms of the actual price in dollars. Add the New supply (P) values to your line chart and verify that your chart looks as expected. Make sure to label the new supply curve. Consumer and producer surplus are explained in Sections 7.6 and 7.11 of Economy, Society, and Public Policy. From your chart, what can you say about the change in total surplus, consumer surplus, and producer surplus as a result of the supply shock? (Hint: You may find the following information useful: the old equilibrium point is Q = 64.5, P = 161.3; the new equilibrium point is Q = 55.0, P = 183.7).

Supply_lnPn <- -1.6 + 1.7 * wm_data2$lnQ
Supply_Pn <- exp(Supply_lnPn)
wm_data2n <- data.frame (wm_data2,Supply_lnPn,Supply_Pn)

# type: "p" = points, "l" = lines, "o" = points and lines
plot(wm_data2n$Q, wm_data2n$Supply_P, type = "o", pch = 16, col="red", ylim = c(0,500), xlim = c(20,100),
  xaxs = "i", yaxs = "i",xlab = "Quantity", ylab = "Price")  

#Add the new supply Data
lines(wm_data2n$Q, wm_data2n$Supply_Pn, type = "o", col="red", pch = 1)

# Add the Demand data
lines(wm_data2n$Q, wm_data2n$Demand_P, type = "o", col="blue", pch = 16)

# Add a legend
legend(80, 110, legend = c("New Supply", "Supply","Demand"),
  col = c("red", "red", "blue"), pch = c(1, 16), 
  lty = c("dashed", "solid", "solid"), cex = 0.8)

Now let’s do part 7.2.1

Use the supply and demand equations from Part 7.1 which are shown here, and carry out the following: Calculate the price elasticity of supply (the percentage change in quantity supplied divided by the percentage change in price) and comment on its size (in absolute value). (Hint: You will have to rearrange the equation so that log Q is in terms of log P.) Calculate the price elasticity of demand in the same way and comment on its size (in absolute value).

lnP = −2.0 + 1.7 * lnQ (Supply) –> lnQ = (2.0 + lnP) / 1.7 –> dlnQ/dlnP = 1/1.7 lnP = 8.5 - 0.82 * lnQ (Demand) –> lnQ = (8.5 - lnP) / 0.82 –> dlnQ/dlnP = -1/0.82

Es <- 1.0/1.7; cat("The Price Elasticity of Supply is", round(Es,1)) 
## The Price Elasticity of Supply is 0.6
Ed <- -1.0/0.82; cat("The Price Elasticity of Demand is", round(Ed,2))
## The Price Elasticity of Demand is -1.22

[End of walk-through]