Plotting Indifference Curves with R Contour Function

[This article was first published on Econometric Sense, 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.

The following post at Constructing Difference Curves – Part 3 from economics.about.com provides a discussion on indifference curves (but actually I think they are isoquants) and how to construct them. I think I have a grasp on how to do this in R if you define the utility function as z =  √(x*y) . For the x and y data I used modified data from the article mentioned above, and then plotted some simulated data.

The data I used for x and y (which I modified-see documentation in the R-code) is listed below:

x: 1,2,3,4,5,6,7,8
y: 10,10,10,15,15,30,60,90

The R code below reads in the data and plots level sets or indifference curves for both the data above and the simulated data. This very basic, for full documentation ( and options for x,y limits, levels etc.) of the contour function in R see here.

Edit: for the second part of the program, if I switch the values of x and y I get indifference curves that are convex to the origin as they should be- the correction can be also represented much more compactly as:

contour(
xyT<-t(as.matrix(cbind(x<-seq(0,20,by=2),y<-seq(0,100,by=10)))),
z<-as.matrix(sqrt(x*y))
)


See program below:

# *------------------------------------------------------------------
# | PROGRAM NAME: INDIFFERENCE_CURVES_R
# | DATE: 3/11/11
# | CREATED BY:  MATT BOGARD
# | PROJECT FILE:  P:\R  Code References            
# *----------------------------------------------------------------
# | PURPOSE: MORE TO PLOT LEVEL SETS USING THE CONTOUR FUNCTION THAN              
# |          TO ACTUALLY PLOT INDIFFERENCE CURVES 
# *------------------------------------------------------------------
# | COMMENTS:               
# |
# |  1: references: http://stat.ethz.ch/R-manual/R-devel/library/graphics/html/contour.html 
# |  2: 
# |  3: 
# |*------------------------------------------------------------------
# | DATA USED: http://economics.about.com/od/indifferencecurves/a/constructing3.htm              
# |
# |
# |*------------------------------------------------------------------
# | CONTENTS:               
# |
# |  PART 1: explicit data 
# |  PART 2: simulated data  (sort of) - not really indifference curves
# |  PART 3: 
# *-----------------------------------------------------------------
# | UPDATES:               
# |
# |
# *------------------------------------------------------------------
 
# *------------------------------------------------------------------
# | PART 1:   explicit data            
# *-----------------------------------------------------------------
 
 
 
# raw data 
 
x <- c(1,2,3,4,5,6,7,8)
y <- c(10,10,10,15,15,30,60,90)
 
# note- for the contour function to work below, x and y 
# These must be in ascending order-based on the contour documentation
#  which is the opposite of how the data was presented at
# about.com 
 
# put x and y in a matrix
 
xy <- as.matrix(cbind(x,y))
 
# transpose xy
 
xyT <- t(xy) 
 
# define function z as a matrix
 
z <- as.matrix(sqrt(x*y))
 
# plot the countour plot / specified level sets
 
contour(xyT,z) # all levels
 
contour(xyT,z, levels =c(10,20,50)) # specified levels for z
 
# *------------------------------------------------------------------
# | PART 2:   simulated data  (sort of) - not really indifference curves            
# *-----------------------------------------------------------------
 
 
rm(list=ls()) # get rid of any existing data 
 
ls() # display active data -should be null 
 
# define x and y
 
x <- seq(0,100,by=10)
y <- seq(0,20,by=2)
 
# put x and y in a matrix
 
xy <- as.matrix(cbind(x,y))
 
# transpose xy
 
xyT <- t(xy) 
 
# define function z as a matrix
 
z <- as.matrix(sqrt(x*y))
 
contour(xyT,z)

To leave a comment for the author, please follow the link and comment on their blog: Econometric Sense.

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)