Deriving the Demand Curve from the Consumer’s Objective Problem in R.

[This article was first published on R – Jacob Smith Economics, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Note: This blog builds off of what was discussed previously here

Continuing with our adventures with the NlcOptim package for R we start with trying to use it for deriving useful objects like the demand curve. Since we are economists we’re not going to simply define a downward sloping demand curve, rather we want to derive it from our consumer’s objective problem.

The way we will do this practically is by embedding our consumer’s problem in the context of a for loop. In the code below we should note that we solve for our demands for each change in price of good 1 (denoted in the code by x[1]) at each price p in our vector of prices.

We repeat this and then store information on prices and quantity demanded in a data frame which is subsequently plotted. Details on this procedure is given in the code below.

#############################
#Deriving Demand Curves in R#
#############################
library('NlcOptim')

################################################
#Initializing by defining sequence of prices,  #
#Null vector for storage of demands and initial#
#"guess" for our solver.                       #
################################################

price<-seq(1,15,by=0.1)
qdemand<-NULL
x0<-c(1,1)

##############
#Preferences#
#############
preferences<-function(x){
  return(-(x[1]+1)^0.5*x[2]^0.5)
}

###################
#Budget Constraint#
###################
budgetconstraint<-function(x){
  f=NULL
  f= rbind(f,p*x[1]+1*x[2]-10)
  return(list(ceq = NULL, c = f))
}

###########################
#Define for loop and Solve#
##########################

for(p in price){
  umax<-solnl(x0,preferences,budgetconstraint)
  d<- umax$par[1]
  qdemand<-rbind(qdemand,d)}

############
#Plot data#
##########
df<-data.frame(price,qdemand)
plot(qdemand,price,type='l',col='blue', xlab='Quantity',ylab='Price',
     main="The Demand Curve", lwd=4.0)
Behold our Micro-Founded Demand Curve.

The ability to do this is very exciting as it gives us tools for truly simulating demand under several conditions. The only story we need to make this more interesting is by adding a supply curve to this graph which will be given its own discussion in a separate post.

If you know any ways to make this code more efficient, be sure to let me know in the comments on this blog!

To leave a comment for the author, please follow the link and comment on their blog: R – Jacob Smith Economics.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)