\(~\)

1 - Import the data into Stata. How many variables are there? Provide a quick description of the data.

\(~\)

library(haven)
library(xtable)
library(tidyverse)
library(stargazer)
library(reshape2)
library(ivreg)
library(kableExtra)

data2 <- read_dta("/Users/bastienpatras/Dropbox/My Mac (bastien’s MacBook Pro)/Downloads/TS6 Estimation of MNL models-20210326/data2.dta") 

# stargazer(as.data.frame(data2))

\(~\)

2 - Estimate an aggregate multinomial logit (MNL) model using OLS.

\(~\)

a - You will have to create the dependent variable.

\(~\)

# Building the dependent variable

data2bis <- data2 %>%
  arrange(market) %>%
  group_by(market) %>%
  mutate(sinside = sum(sjm),
         s0m = 1-sinside)

# Output 

head(data2bis) %>% 
  kbl(booktabs = T) %>%
  kable_styling(latex_options = "striped", full_width = T) 
market firm prodid sjm x pjm w sinside s0m
1 1 1 0.0197264 0.8058020 5.145977 0.3593405 0.9967563 0.0032437
1 1 2 0.0058390 0.5415874 5.265573 0.9450304 0.9967563 0.0032437
1 1 3 0.0099732 0.8788868 5.720443 0.8353403 0.9967563 0.0032437
1 1 4 0.0738325 0.9475350 5.087033 0.1210243 0.9967563 0.0032437
1 2 1 0.0958654 0.8327903 4.264134 0.3758931 0.9967563 0.0032437
1 2 2 0.1799215 0.9987936 4.366472 0.7063378 0.9967563 0.0032437
# xtable(head(data2bis))

\(~\)

b - For your OLS results, calculate the average (across markets) estimated own-price elasticities for each product.

\(~\)

# Building data by demand and price

pjmbis_11 <- as.data.frame(data2bis) %>%
          mutate(demandj = log(sjm)-log(s0m)) %>%
          filter(prodid == 1) %>%
          rename(demandj_1 = demandj,
                 pjm_1 = pjm) %>%
          select(demandj_1, pjm_1)

pjmbis_22 <- as.data.frame(data2bis) %>%
          mutate(demandj = log(sjm)-log(s0m)) %>%
          filter(prodid == 2) %>%
          rename(demandj_2 = demandj,
                 pjm_2 = pjm) %>%
          select(demandj_2, pjm_2)

pjmbis_33 <- as.data.frame(data2bis) %>%
          mutate(demandj = log(sjm)-log(s0m)) %>%
          filter(prodid == 3) %>%
          rename(demandj_3 = demandj,
                 pjm_3 = pjm) %>%
          select(demandj_3, pjm_3)

pjmbis_44 <- as.data.frame(data2bis) %>%
          mutate(demandj = log(sjm)-log(s0m)) %>%
          filter(prodid == 4) %>%
          rename(demandj_4 = demandj,
                 pjm_4 = pjm) %>%
          select(demandj_4, pjm_4)

dta <- cbind(pjmbis_11, pjmbis_22, pjmbis_33, pjmbis_44)

# OLS estimations by products across markets

m11 <- lm(demandj_1 ~ pjm_1, data = dta)
m22 <- lm(demandj_2 ~ pjm_2, data = dta)
m33 <- lm(demandj_3 ~ pjm_3, data = dta)
m44 <- lm(demandj_4 ~ pjm_4, data = dta)

# Storing betas from OLS estimations 

beta_1 <- summary(m11)$coefficients[2]
beta_2 <- summary(m22)$coefficients[2]
beta_3 <- summary(m33)$coefficients[2]
beta_4 <- summary(m44)$coefficients[2]

# Fitted demand from OLS estimations

fitted_1 <- mean(fitted(m11))
fitted_2 <- mean(fitted(m22))
fitted_3 <- mean(fitted(m33))
fitted_4 <- mean(fitted(m44))

# Computing elasticities

mean_1 <- mean(dta$pjm_1)
mean_2 <- mean(dta$pjm_2)
mean_3 <- mean(dta$pjm_3)
mean_4 <- mean(dta$pjm_4)

e_1 <- beta_1*mean_1/fitted_1
e_2 <- beta_2*mean_2/fitted_2
e_3 <- beta_3*mean_3/fitted_3
e_4 <- beta_4*mean_4/fitted_4

# Butilding the table

Table.1.1 <- tibble(`Product ID` = c(1, 2, 3, 4),
       `OLS Estimates` = c(beta_1, beta_2, beta_3, beta_4),
       `Average price of product 3` = c(mean_1, mean_2, mean_3, mean_4),
       `Fitted demand` = c(fitted_1, fitted_2, fitted_3, fitted_4),
        Elasticities = c(e_1, e_2, e_3, e_4))

# Output 

Table.1.1 %>% 
  kbl(booktabs = T) %>%
  kable_styling(latex_options = "striped", full_width = T) 
Product ID OLS Estimates Average price of product 3 Fitted demand Elasticities
1 0.0262509 4.281237 1.730964 0.0649271
2 0.0283262 4.263290 1.726696 0.0699388
3 0.0739183 4.257158 1.688815 0.1863331
4 0.0786851 4.276978 1.696768 0.1983384

\(~\)

b - The average estimated cross-price elasticity of each product with respect to an increase in the price of product 3. Use the fitted value of the market share(s) when calculating your elasticities.

\(~\)

  1. OLS estimation cross price elasticities wiht \(i \in \{1, 2, 4\}\) and \(j=3\):

\(~\)

\[\text{demand}_i =\beta_0 + \beta_i\times \text{price}_{j} + u_i \] \(~\)

  1. Cross price elasticities

\(~\)

\[ \epsilon_{j\neq i} = \hat{\beta}_i \times \frac{\overline{\text{price}}_{j}}{\widehat{\text{demand}_i}_i} \] \(~\)

First method

\(~\)

# Define the transformation function delta

delta <- function(x) {
  x = x
}

# Building data by demand

pjmbis_1 <- as.data.frame(data2bis) %>%
          mutate(demandj = log(sjm)-log(s0m)) %>%
          filter(prodid == 1) %>%
          rename(demandj_1 = demandj) %>%
          select(demandj_1)

pjmbis_2 <- as.data.frame(data2bis) %>%
          mutate(demandj = log(sjm)-log(s0m)) %>%
          filter(prodid == 2) %>%
          rename(demandj_2 = demandj) %>%
          select(demandj_2)

pjmbis_3 <- as.data.frame(data2bis) %>%
          mutate(demandj = log(sjm)-log(s0m),
          pjm_3 = ifelse(prodid == 3, delta(pjm), pjm)) %>%
          filter(prodid == 3) %>%
          rename(demandj_3 = demandj) %>%
          select(pjm_3, demandj_3)

pjmbis_4 <- as.data.frame(data2bis) %>%
          mutate(demandj = log(sjm)-log(s0m)) %>%
          filter(prodid == 4) %>%
          rename(demandj_4 = demandj) %>%
          select(demandj_4)

dta <- cbind(pjmbis_1, pjmbis_2, pjmbis_3, pjmbis_4)

# OLS estimations 

m1 <- lm(demandj_1 ~ pjm_3, data = dta)
m2 <- lm(demandj_2 ~ pjm_3, data = dta)
m4 <- lm(demandj_4 ~ pjm_3, data = dta)

# Storing betas from OLS estimations 

beta_1 <- summary(m1)$coefficients[2]
beta_2 <- summary(m2)$coefficients[2]
beta_4 <- summary(m4)$coefficients[2]

# Fitted demand from OLS estimations

fitted_1 <- mean(fitted(m1))
fitted_2 <- mean(fitted(m2))
fitted_4 <- mean(fitted(m4))

# Computing elasticities

mean <- mean(dta$pjm_3)

e_1 <- beta_1*mean/fitted_1
e_2 <- beta_2*mean/fitted_2
e_4 <- beta_4*mean/fitted_4

# Butilding the table

Table.1 <- tibble(`Product ID` = c(1, 2, 4),
       `OLS Estimates` = c(beta_1, beta_2, beta_4),
       `Average price of product 3` = c(mean, mean, mean),
       `Fitted demand` = c(fitted_1, fitted_2, fitted_4),
        Elasticities = c(e_1, e_2, e_4))

# Output.2 

Table.1 %>% 
  kbl(booktabs = T) %>%
  kable_styling(latex_options = "striped", full_width = T) 
Product ID OLS Estimates Average price of product 3 Fitted demand Elasticities
1 0.1454881 4.257158 1.730964 0.3578155
2 0.0518710 4.257158 1.726696 0.1278876
4 0.1829796 4.257158 1.696768 0.4590922

\(~\)

3 - Estimate the same MNL model using the cost-shifter as instrumental variable.

\(~\)

  1. Own price elasticities instrumented with cost shifter

\(~\)

# Building data by demand and price

pjmbis_11 <- as.data.frame(data2bis) %>%
          mutate(demandj = log(sjm)-log(s0m)) %>%
          filter(prodid == 1) %>%
          rename(demandj_1 = demandj,
                 pjm_1 = pjm,
                 w_1 = w) %>%
          select(demandj_1, pjm_1, w_1)

pjmbis_22 <- as.data.frame(data2bis) %>%
          mutate(demandj = log(sjm)-log(s0m)) %>%
          filter(prodid == 2) %>%
          rename(demandj_2 = demandj,
                 pjm_2 = pjm,
                 w_2 = w) %>%
          select(demandj_2, pjm_2, w_2)

pjmbis_33 <- as.data.frame(data2bis) %>%
          mutate(demandj = log(sjm)-log(s0m)) %>%
          filter(prodid == 3) %>%
          rename(demandj_3 = demandj,
                 pjm_3 = pjm,
                 w_3 = w) %>%
          select(demandj_3, pjm_3, w_3)

pjmbis_44 <- as.data.frame(data2bis) %>%
          mutate(demandj = log(sjm)-log(s0m)) %>%
          filter(prodid == 4) %>%
          rename(demandj_4 = demandj,
                 pjm_4 = pjm,
                 w_4 = w) %>%
          select(demandj_4, pjm_4, w_4)

dta <- cbind(pjmbis_11, pjmbis_22, pjmbis_33, pjmbis_44)

 # IV estimations

m1_IV <- ivreg(demandj_1 ~ pjm_1 | w_1, data = dta)
m2_IV <- ivreg(demandj_2 ~ pjm_2 | w_2, data = dta)
m3_IV <- ivreg(demandj_3 ~ pjm_3 | w_3, data = dta)
m4_IV <- ivreg(demandj_4 ~ pjm_4 | w_4, data = dta)

# Storing betas from OLS estimations 

beta_1 <- summary(m1_IV)$coefficients[2]
beta_2 <- summary(m2_IV)$coefficients[2]
beta_3 <- summary(m3_IV)$coefficients[2]
beta_4 <- summary(m4_IV)$coefficients[2]

# Fitted demand from OLS estimations

fitted_1 <- mean(fitted(m1_IV))
fitted_2 <- mean(fitted(m2_IV))
fitted_3 <- mean(fitted(m3_IV))
fitted_4 <- mean(fitted(m4_IV))

# Computing elasticities

mean_1 <- mean(dta$pjm_1)
mean_2 <- mean(dta$pjm_2)
mean_3 <- mean(dta$pjm_3)
mean_4 <- mean(dta$pjm_4)

e_1 <- beta_1*mean_1/fitted_1
e_2 <- beta_2*mean_2/fitted_2
e_3 <- beta_3*mean_3/fitted_3
e_4 <- beta_4*mean_4/fitted_4

# Butilding the table

Table.1.w <- tibble(`Product ID` = c(1, 2, 3, 4),
       `OLS Estimates` = c(beta_1, beta_2, beta_3, beta_4),
       `Average price by product` = c(mean_1, mean_2, mean_3, mean_4),
       `Fitted demand` = c(fitted_1, fitted_2, fitted_3, fitted_4),
        Elasticities = c(e_1, e_2, e_3, e_4))

# Output 

Table.1.w %>% 
  kbl(booktabs = T) %>%
  kable_styling(latex_options = "striped", full_width = T) 
Product ID OLS Estimates Average price by product Fitted demand Elasticities
1 -1.162429 4.281237 1.730964 -2.875066
2 -1.162093 4.263290 1.726696 -2.869259
3 -1.072985 4.257158 1.688815 -2.704777
4 -1.045985 4.276978 1.696768 -2.636574

\(~\)

  1. Cross price elsticities instrumented with cost shifter

\(~\)

# Define the transformation function delta

delta <- function(x) {
  x = x
}

# Building data by demand

pjmbis_1 <- as.data.frame(data2bis) %>%
          mutate(demandj = log(sjm)-log(s0m)) %>%
          filter(prodid == 1) %>%
          rename(demandj_1 = demandj) %>%
          select(pjm, demandj_1)

pjmbis_2 <- as.data.frame(data2bis) %>%
          mutate(demandj = log(sjm)-log(s0m)) %>%
          filter(prodid == 2) %>%
          rename(demandj_2 = demandj) %>%
          select(pjm, demandj_2)

pjmbis_3 <- as.data.frame(data2bis) %>%
          mutate(demandj = log(sjm)-log(s0m),
          pjm_3 = ifelse(prodid == 3, delta(pjm), pjm)) %>%
          filter(prodid == 3) %>%
          select(pjm_3, demandj, w)

pjmbis_4 <- as.data.frame(data2bis) %>%
          mutate(demandj = log(sjm)-log(s0m)) %>%
          filter(prodid == 4) %>%
          rename(demandj_4 = demandj) %>%
          select(pjm, demandj_4)

dta <- cbind(pjmbis_1, pjmbis_2, pjmbis_3, pjmbis_4)

# First stage estimations

Firststage <- lm(pjm_3 ~ w, data = dta)
Fitted.Firststage <- fitted(Firststage)

# Second stage estimations 

m1_OLS <- lm(demandj_1 ~ Fitted.Firststage, data = dta)
m2_OLS <- lm(demandj_2 ~ Fitted.Firststage, data = dta)
m4_OLS <- lm(demandj_4 ~ Fitted.Firststage, data = dta)

# IV estimations

m1_IV <- ivreg(demandj_1 ~ pjm_3 | w, data = dta)
m2_IV <- ivreg(demandj_2 ~ pjm_3 | w, data = dta)
m4_IV <- ivreg(demandj_4 ~ pjm_3 | w, data = dta)

# Storing betas from OLS estimations 

beta_1_OLS <- summary(m1_OLS)$coefficients[2]
beta_2_OLS <- summary(m2_OLS)$coefficients[2]
beta_4_OLS <- summary(m4_OLS)$coefficients[2]

# Storing betas from IV estimations 

beta_1_IV <- summary(m1_IV)$coefficients[2]
beta_2_IV <- summary(m2_IV)$coefficients[2]
beta_4_IV <- summary(m4_IV)$coefficients[2]

# Fitted demand from OLS estimations

fitted_1 <- mean(fitted(m1_IV))
fitted_2 <- mean(fitted(m2_IV))
fitted_4 <- mean(fitted(m4_IV))

# Computing elasticities

mean <- mean(dta$pjm_3)

e_1 <- beta_1_IV*mean/fitted_1
e_2 <- beta_2_IV*mean/fitted_2
e_4 <- beta_4_IV*mean/fitted_4

# Output

Table.2 <- tibble(`2SLS using OLS` = c(beta_1_OLS, beta_2_OLS, beta_4_OLS),
       `IV Estimates` = c(beta_1_IV, beta_2_IV, beta_4_IV),
       `Average price of product 3` = c(mean, mean, mean),
       `Fitted demand` = c(fitted_1, fitted_2, fitted_4),
        Elasticities = c(e_1, e_2, e_4))

# Output.2 

Table.2 %>% 
  kbl(booktabs = T) %>%
  kable_styling(latex_options = "striped", full_width = T) 
2SLS using OLS IV Estimates Average price of product 3 Fitted demand Elasticities
0.0109248 0.0109248 4.257158 1.730964 0.0268686
-0.0950487 -0.0950487 4.257158 1.726696 -0.2343421
0.2051712 0.2051712 4.257158 1.696768 0.5147706
# xtable(Table.2, digits = 4)

\(~\)

4 - Estimate the MNL model again using the demand shifter as the instrumental variable.

\(~\)

  1. Own price elasticities instrumented by demand shifter :

\(~\)

# Building data by demand and price

pjmbis_11 <- as.data.frame(data2bis) %>%
          mutate(demandj = log(sjm)-log(s0m)) %>%
          filter(prodid == 1) %>%
          rename(demandj_1 = demandj,
                 pjm_1 = pjm,
                 x_1 = x) %>%
          select(demandj_1, pjm_1, x_1)

pjmbis_22 <- as.data.frame(data2bis) %>%
          mutate(demandj = log(sjm)-log(s0m)) %>%
          filter(prodid == 2) %>%
          rename(demandj_2 = demandj,
                 pjm_2 = pjm,
                 x_2 = x) %>%
          select(demandj_2, pjm_2, x_2)

pjmbis_33 <- as.data.frame(data2bis) %>%
          mutate(demandj = log(sjm)-log(s0m)) %>%
          filter(prodid == 3) %>%
          rename(demandj_3 = demandj,
                 pjm_3 = pjm,
                 x_3 = x) %>%
          select(demandj_3, pjm_3, x_3)

pjmbis_44 <- as.data.frame(data2bis) %>%
          mutate(demandj = log(sjm)-log(s0m)) %>%
          filter(prodid == 4) %>%
          rename(demandj_4 = demandj,
                 pjm_4 = pjm,
                 x_4 = x) %>%
          select(demandj_4, pjm_4, x_4)

dta <- cbind(pjmbis_11, pjmbis_22, pjmbis_33, pjmbis_44)

 # IV estimations

m1_IV <- ivreg(demandj_1 ~ pjm_1 | x_1, data = dta)
m2_IV <- ivreg(demandj_2 ~ pjm_2 | x_2, data = dta)
m3_IV <- ivreg(demandj_3 ~ pjm_3 | x_3, data = dta)
m4_IV <- ivreg(demandj_4 ~ pjm_4 | x_4, data = dta)

# Storing betas from OLS estimations 

beta_1 <- summary(m1_IV)$coefficients[2]
beta_2 <- summary(m2_IV)$coefficients[2]
beta_3 <- summary(m3_IV)$coefficients[2]
beta_4 <- summary(m4_IV)$coefficients[2]

# Fitted demand from OLS estimations

fitted_1 <- mean(fitted(m1_IV))
fitted_2 <- mean(fitted(m2_IV))
fitted_3 <- mean(fitted(m3_IV))
fitted_4 <- mean(fitted(m4_IV))

# Computing elasticities

mean_1 <- mean(dta$pjm_1)
mean_2 <- mean(dta$pjm_2)
mean_3 <- mean(dta$pjm_3)
mean_4 <- mean(dta$pjm_4)

e_1 <- beta_1*mean_1/fitted_1
e_2 <- beta_2*mean_2/fitted_2
e_3 <- beta_3*mean_3/fitted_3
e_4 <- beta_4*mean_4/fitted_4

# Butilding the table

Table.1.x <- tibble(`Product ID` = c(1, 2, 3, 4),
       `OLS Estimates` = c(beta_1, beta_2, beta_3, beta_4),
       `Average price by product` = c(mean_1, mean_2, mean_3, mean_4),
       `Fitted demand` = c(fitted_1, fitted_2, fitted_3, fitted_4),
        Elasticities = c(e_1, e_2, e_3, e_4))

# Output 

Table.1.x %>% 
  kbl(booktabs = T) %>%
  kable_styling(latex_options = "striped", full_width = T) 
Product ID OLS Estimates Average price by product Fitted demand Elasticities
1 4.154134 4.281237 1.730964 10.274527
2 4.065378 4.263290 1.726696 10.037603
3 3.790334 4.257158 1.688815 9.554657
4 4.101576 4.276978 1.696768 10.338682

\(~\)

  1. Cross price elasticities instrumented by demand shifter :

\(~\)

# Define the transformation function delta

delta <- function(x) {
  x = x
}

# Building data by demand

pjmbis_1 <- as.data.frame(data2bis) %>%
          mutate(demandj = log(sjm)-log(s0m)) %>%
          filter(prodid == 1) %>%
          rename(demandj_1 = demandj) %>%
          select(pjm, demandj_1)

pjmbis_2 <- as.data.frame(data2bis) %>%
          mutate(demandj = log(sjm)-log(s0m)) %>%
          filter(prodid == 2) %>%
          rename(demandj_2 = demandj) %>%
          select(pjm, demandj_2)

pjmbis_3 <- as.data.frame(data2bis) %>%
          mutate(demandj = log(sjm)-log(s0m),
          pjm_3 = ifelse(prodid == 3, delta(pjm), pjm)) %>%
          filter(prodid == 3) %>%
          select(pjm_3, demandj, x)

pjmbis_4 <- as.data.frame(data2bis) %>%
          mutate(demandj = log(sjm)-log(s0m)) %>%
          filter(prodid == 4) %>%
          rename(demandj_4 = demandj) %>%
          select(pjm, demandj_4)

dta <- cbind(pjmbis_1, pjmbis_2, pjmbis_3, pjmbis_4)

# OLS estimations 

m1 <- ivreg(demandj_1 ~ pjm_3 | x, data = dta)
m2 <- ivreg(demandj_2 ~ pjm_3 | x, data = dta)
m3 <- ivreg(demandj_4 ~ pjm_3 | x, data = dta)

# Storing betas from OLS estimations 

beta_1 <- summary(m1)$coefficients[2]
beta_2 <- summary(m2)$coefficients[2]
beta_4 <- summary(m3)$coefficients[2]

# Fitted demand from OLS estimations

fitted_1 <- mean(fitted(m1))
fitted_2 <- mean(fitted(m2))
fitted_4 <- mean(fitted(m3))

# Computing elasticities

mean <- mean(dta$pjm_3)

e_1 <- beta_1*mean/fitted_1
e_2 <- beta_2*mean/fitted_2
e_4 <- beta_4*mean/fitted_4

# Output

Table.3 <- tibble(`IV Estimates` = c(beta_1, beta_2, beta_4),
       `Average price of product 3` = c(mean, mean, mean),
       `Fitted demand` = c(fitted_1, fitted_2, fitted_4),
       Elasticities = c(e_1, e_2, e_4))

# Output.2 

Table.3 %>% 
  kbl(booktabs = T) %>%
  kable_styling(latex_options = "striped", full_width = T) 
IV Estimates Average price of product 3 Fitted demand Elasticities
-0.0575285 4.257158 1.730964 -0.1414864
-0.1473556 4.257158 1.726696 -0.3633044
-0.0055597 4.257158 1.696768 -0.0139492
# xtable(Table.3, digits = 4)

\(~\)

5 - Which IV do you prefer? Why? Estimate own-price elasticities and average cross-price elasticities with respect to the price of product 3 again with the IV of your choice.

\(~\)