Endogenous Prices Demand Model in R

# SIMULATE A Demand model with ENDOGENOUS prices # Daniel Toro Gonzalez 2014 # Universidad Tecnológica de Bolívar ######################################### # NECESSARY PACKAGES TO RUN THIS PROGRAM# ######################################### #install.packages("AER") #In case is not installed remove the first # sign infront of the line #install.packages("systemfit") #In case is not installed remove the first # sign infront of the line ########################################## # The data generating model, unobservable by the researcher is: # DD = B0+B1*P+B2*I+B3*X+e1 with e1~N(0,1) ############## # MODEL SETUP# ############## set.seed(20) #To fix the Random Number Generator n <- 1000 #Sample size (Change at will) # Parameter Values (Change at will) B0 <- 100 #Intercept of Demand B1 <- -2 #Price coefficient B2 <- +5 #Income coefficient B3 <- +3 #Quality coefficient parameters <- c(B0, B1, B2, B3) # Save the original population parameters for printing purposes # Errors e1 <- rnorm(n, mean=0, sd=1) #Error e1~N(0,k) k=1, this term is included in the demand equation e2 <- rnorm(n, mean=0, sd=1) #Error e2~N(0,k) k=1, this term will be included in the endogenous price equation # Quality (Exogenous variable, non observable) X <- rnorm(n, mean=10, sd=1) #Quality X~N(10,k) k=1 # Price of Labor (Exogenous variable, observable) W <- rnorm(n, mean=18, sd=4) #Minimum wage by hour W~N(18,k) k=1 # Endogenous Prices depend on labor cost (W), Quality (X) and a random term e2~N(0,k) k=1 # The values of the parameters (10, 0.5, 2) can be modified at will P <- 10+0.5*W+2*X+e2 #Average Exogenous Income US$5000 I <- rnorm(n, mean=5000, sd=500) # Generating quantity values for the demand function # using the non observable population parameters (B0, B1, B2, B3) # and the simulated values of the variables (P, I, X, e1) Q <- B0+B1*P+B2*I+B3*X+e1 # Demand Courve dataeq <- data.frame(Q, P, I, X) # Data Frame with all the variables. #The database of prices (P), Income (I), Quality (X) and quantities (Q) is attached to the R search path. attach(dataeq) summary(dataeq) # Describes de dataframe cor(P, X) # Prices and Quality are highly correlated ############################## # ESTIMATIONS # ############################## # Reg0 is the estimation of the demand model # as if quality is an observable variable # this model replicates closely the real data generating process reg0 <- lm(Q~P+I+X) r0coef <- coef(reg0) resid_reg0 <- resid(reg0) # Reg1 is the estimation omiting Quality # which is unobservable but highly correlated with the prices reg1 <- lm(Q~P+I) r1coef <- coef(reg1) ############################## # RESULTS # ############################## cat("***************************************************************************") cat("NAMES OF THE VARIABLES..", "INTERC", "........", "PRICE", ".......", "INCOME", "....", "QUALITY") cat("***************************************************************************") cat("Original Parameters.....", parameters[1], "........", parameters[2], "...........", parameters[3], ".........", parameters[4], "......") cat("Complete Model..........", r0coef[1], "...", r0coef[2], "....", r0coef[3], "..", r0coef[4]) cat("Model without quality...", r1coef[1], "...", r1coef[2], "...", r1coef[3], "...", "N/A", "...") ############################## # CONCLUSIONS # ############################## # The incomplete model lm(Q~P+I) where quality (X) is unobservable and captured # in the error term u1 = B3*X+e1, generate biased parameter estimation for B1 # The parameter increases in real terms capturing the positive effect of quality on consumtion. #BIAS bias <- (1-((r0coef[2]-r1coef[2])/r0coef[2]))*100 cat("Biased parameter represents...", bias, "% of the original coefficient.") ############################## # CONTROL FUNCTION # ############################## # Assume W is observable so we can estimate P = D0+D1W+u2 #P <- 10+0.5*W+2*X+e2 # ESTRUCTURAL PRICE EQUATION # regP <- lm(P~W) CF <- resid(regP) # Control Function Variable (Error terms of the Estructural Price Equation cor(P, X) # Prices and Quality are highly correlated cor(P, CF) # Prices and Control Residuals are highly correlated # Hence the error term of the price equation captures the unobservable quality. # DEMAND EQUATION WITH CONTROL # # We estimate the demand model using CF instead of X, hence: Q = B0+B1*P+B2*I+B3*CF reg2 <- lm(Q~P+I+CF) r2coef <- coef(reg2) summary(reg2) # Since the coefficient B3 is statistically significant, this is an implicit Hausman test for endogeneity. ############################## # RESULTS # ############################## cat("***************************************************************************") cat("NAMES OF THE VARIABLES..", "INTERC", "........", "PRICE", ".......", "INCOME", "....", "QUALITY") cat("***************************************************************************") cat("Original Parameters.....", parameters[1], "........", parameters[2], "...........", parameters[3], "..........", parameters[4], "......") cat("Complete Model..........", r0coef[1], "...", r0coef[2], "....", r0coef[3], "...", r0coef[4]) cat("Model without quality...", r1coef[1], "...", r1coef[2], "...", r1coef[3], "...", "N/A", "....") cat("Model with CF...........", r2coef[1], "...", r2coef[2], "....", r2coef[3], "...", r2coef[4]) ############################## # CONCLUSIONS # ############################## # The CF strategy allows the researcher to control for the unobservable factors and to correctly identify the price parameter. ############################## # INSTRUMENTAL # # VARIABLES # ############################## library(AER) library(systemfit) # 2SLS estimation reg3 <- ivreg(Q ~ P + I | P + W ) r3coef <- coef(reg3) cor(P, W) # Wages are correlated with Prices cor(resid_reg0, W) # Instruments are uncorrelated with errors cor(P, CF) # Wages are correlated with errors cor(resid_reg0, CF) # Instruments are uncorrelated with errors ############################## # RESULTS # ############################## cat("***************************************************************************") cat("NAMES OF THE VARIABLES..", "INTERC", "........", "PRICE", ".......", "INCOME", "....", "QUALITY") cat("***************************************************************************") cat("Original Parameters.....", parameters[1], "........", parameters[2], "...........", parameters[3], "..........", parameters[4], "......") cat("Complete Model..........", r0coef[1], "...", r0coef[2], "....", r0coef[3], "...", r0coef[4]) cat("Model without quality...", r1coef[1], "...", r1coef[2], "...", r1coef[3], "...", r1coef[4], "....") cat("Model with CF...........", r2coef[1], "...", r2coef[2], "....", r2coef[3], "...", r2coef[4]) cat("IV Model...............", r3coef[1], "...", r3coef[2], "....", r3coef[3], "...", r3coef[4], "....")

Created by Pretty R at inside-R.org

RESULTS

***************************************************************************

NAMES OF THE VARIABLES.. INTERC ........ PRICE ....... INCOME .... QUALITY

***************************************************************************

Parameters........................... 100 .............. -2 .............. 5 ............... 3 ......

Complete Model.............. 100.3154 .... -2.004483 .... 4.999976 ... 2.996596

Model without quality...... 102.9739 .... -1.322884 ... 5.000133 .... NA ....

Model with CF.................. 131.7149 ... -2.036874 .... 4.999955 ... 1.24093

IV Model.......................... -248.958 ... -1.184101 ..... 5.069486 ... NA ....

#The CF strategy allows the researcher to control for the unobservable

#factors and to correctly identify the price parameter.