Price elasticity is a measure of the responsiveness of demand for a product or service to changes in its price. It is a useful metric for businesses to understand how sensitive their customers are to changes in pricing. In this blog post, we will explore how to calculate price elasticity of demand in R with examples.

## Understand Price Elasticity Concept

Before we dive into the calculation, let’s first understand the concept of price elasticity. Price elasticity is a ratio of the percentage change in quantity demanded to the percentage change in price. Mathematically, it is expressed as:

*Price Elasticity = (% Change in Quantity Demanded) / (% Change in Price)*

There are different types of price elasticities, namely, perfectly elastic, perfectly inelastic, unitary elastic, and relatively elastic or inelastic. The value of price elasticity ranges from -1 to infinity. If the price elasticity is greater than 1, then the demand is elastic, and a small change in price will have a significant impact on demand. If the price elasticity is less than 1, then the demand is inelastic, and a change in price will have a small effect on demand.

Now that we understand the concept of price elasticity, let’s move on to the implementation in R.

## Calculate Price Elasticity in R

To calculate price elasticity in R, we first need to estimate the demand function using regression analysis. The demand function describes the relationship between the price of a product and the quantity demanded. The regression equation takes the form:

Q = a – bP

where Q is the quantity demanded, P is the price of the product, a is the intercept, and b is the slope coefficient.

To estimate the demand function, we need data on price and quantity. Let’s take an example dataset of a product and use it to estimate the demand function and calculate the price elasticity.

### Example Dataset

Let’s take an example dataset of a product that has been sold at different prices, and the quantity sold at each price has been recorded. We will use this data to estimate the demand function and calculate the price elasticity.

1 2 3 4 5 6 7 8 9 10 11 |
Price Quantity 2.50 1000 3.00 900 3.50 800 4.00 700 4.50 600 5.00 500 5.50 400 6.00 300 6.50 200 7.00 100 |

Estimating the Demand Function

We can estimate the demand function using the *lm() *function in R. The *lm()* function fits a linear regression model to the data.

1 2 3 4 5 |
# Read the dataset df <- read.csv("example_data.csv") # Estimate the demand function model <- lm(Quantity ~ Price, data = df) |

Now that we have estimated the demand function, we can extract the intercept and slope coefficients from the model.

1 2 3 |
# Extract the intercept and slope coefficients a <- coef(model)[1] b <- coef(model)[2] |

We can now use the formula for price elasticity to calculate the value.

1 2 3 4 5 |
# Calculate the price elasticity p <- 4.50 # new price q <- a - b * p # new quantity demanded pe <- abs((q / ((q - b * p) / p))) |

The value of price elasticity for this example dataset and a new price of 4.50 is 0.83. This suggests that the demand is inelastic, and a change in price will have a small effect on demand.

## What if Price Elasticity is Negative?

When price elasticity is negative, it indicates an inverse relationship between the price of a product and the quantity demanded. In other words, as the price increases, the quantity demanded decreases. This is a common scenario for most products in the market.

Let’s use the same example dataset from the previous blog post to illustrate this scenario in R.

Example Dataset:

1 2 3 4 5 6 7 8 9 10 11 |
Price Quantity 2.50 1000 3.00 900 3.50 800 4.00 700 4.50 600 5.00 500 5.50 400 6.00 300 6.50 200 7.00 100 |

Estimating the Demand Function:

We can estimate the demand function using the *lm()* function in R.

1 2 3 4 5 |
# Read the dataset df <- read.csv("example_data.csv") # Estimate the demand function model <- lm(Quantity ~ Price, data = df) |

Now that we have estimated the demand function, we can extract the intercept and slope coefficients from the model.

1 2 3 |
# Extract the intercept and slope coefficients a <- coef(model)[1] b <- coef(model)[2] |

Calculating Price Elasticity:

We can calculate the price elasticity using the following formula:

1 |
pe <- - b * (mean(df$Price) / mean(df$Quantity)) |

The negative sign in the formula indicates an inverse relationship between price and quantity demanded. The value of price elasticity for this example dataset is -1.85. This suggests that a 1% increase in price will lead to a 1.85% decrease in quantity demanded, and vice versa.

In conclusion, when price elasticity is negative, it indicates an inverse relationship between price and quantity demanded. This is a common scenario for most products in the market. We can easily calculate price elasticity of demand in R using the *lm() *function and the formula mentioned above.