This is an example on how you can use R to solve some the exercises in your textbook. You can look at some videos on how to download and install R here: http://dist.stat.tamu.edu/pub/rvideos/
Lets start with exercise A.1 (p.653)
p = seq(from = 0, to = 50, by = 1) # Create p
q = -3 + 1.5 * p # Supply
data.frame(q = q, p = p) # Store p & q in a data frame, and print it out
## q p
## 1 -3.0 0
## 2 -1.5 1
## 3 0.0 2
## 4 1.5 3
## 5 3.0 4
## 6 4.5 5
## 7 6.0 6
## 8 7.5 7
## 9 9.0 8
## 10 10.5 9
## 11 12.0 10
## 12 13.5 11
## 13 15.0 12
## 14 16.5 13
## 15 18.0 14
## 16 19.5 15
## 17 21.0 16
## 18 22.5 17
## 19 24.0 18
## 20 25.5 19
## 21 27.0 20
## 22 28.5 21
## 23 30.0 22
## 24 31.5 23
## 25 33.0 24
## 26 34.5 25
## 27 36.0 26
## 28 37.5 27
## 29 39.0 28
## 30 40.5 29
## 31 42.0 30
## 32 43.5 31
## 33 45.0 32
## 34 46.5 33
## 35 48.0 34
## 36 49.5 35
## 37 51.0 36
## 38 52.5 37
## 39 54.0 38
## 40 55.5 39
## 41 57.0 40
## 42 58.5 41
## 43 60.0 42
## 44 61.5 43
## 45 63.0 44
## 46 64.5 45
## 47 66.0 46
## 48 67.5 47
## 49 69.0 48
## 50 70.5 49
## 51 72.0 50
Lets look at a plot of these two variables.
The first one is the mathematica perspective where q is on the y-axis:
plot(q ~ p) # Mathematical perspective
The second one is the economic perspective where p is on the y-axis:
plot(p ~ q) # Economic perspective
We could make the plot a bit nicer, adding lables and a solid line.
plot(p ~ q, xlab = "Quantity", ylab = "Price", type = "l")
Another way of doing this, is using the curve() function, but it must contain “x”
curve(-3 + 1.5 * x, 0, 50)
Find the slope by taking the derivative of the function. We call the derivative slope.
slope = D(expression(-3 + 1.5 * x), "x") # b)
slope
## [1] 1.5
Elasticity = slope * p/(-3 + 1.5 * p) # Elasticity=dy/dx*x/y
Elasticity
## [1] 0.000 -1.000 Inf 3.000 2.000 1.667 1.500 1.400 1.333 1.286
## [11] 1.250 1.222 1.200 1.182 1.167 1.154 1.143 1.133 1.125 1.118
## [21] 1.111 1.105 1.100 1.095 1.091 1.087 1.083 1.080 1.077 1.074
## [31] 1.071 1.069 1.067 1.065 1.062 1.061 1.059 1.057 1.056 1.054
## [41] 1.053 1.051 1.050 1.049 1.048 1.047 1.045 1.044 1.043 1.043
## [51] 1.042
data.frame(p = p, E = Elasticity)
## p E
## 1 0 0.000
## 2 1 -1.000
## 3 2 Inf
## 4 3 3.000
## 5 4 2.000
## 6 5 1.667
## 7 6 1.500
## 8 7 1.400
## 9 8 1.333
## 10 9 1.286
## 11 10 1.250
## 12 11 1.222
## 13 12 1.200
## 14 13 1.182
## 15 14 1.167
## 16 15 1.154
## 17 16 1.143
## 18 17 1.133
## 19 18 1.125
## 20 19 1.118
## 21 20 1.111
## 22 21 1.105
## 23 22 1.100
## 24 23 1.095
## 25 24 1.091
## 26 25 1.087
## 27 26 1.083
## 28 27 1.080
## 29 28 1.077
## 30 29 1.074
## 31 30 1.071
## 32 31 1.069
## 33 32 1.067
## 34 33 1.065
## 35 34 1.062
## 36 35 1.061
## 37 36 1.059
## 38 37 1.057
## 39 38 1.056
## 40 39 1.054
## 41 40 1.053
## 42 41 1.051
## 43 42 1.050
## 44 43 1.049
## 45 44 1.048
## 46 45 1.047
## 47 46 1.045
## 48 47 1.044
## 49 48 1.043
## 50 49 1.043
## 51 50 1.042
A more advanced plot of the relationship. As we can see, the elasticity (\( \frac{\partial y}{\partial x}\frac{x}{y} \)) along a linear function is a non-linear relationship. Note that at p=2, q=0, and the elasticity is equal to +\( \infty \). A supply elasticity is positive, hence we plot the elasticity from p=3.
library(grDevices)
plot(Elasticity ~ p, type = "l", xlim = c(3, 50), ylim = c(0, 3))
title("Elasticity as a function of price")
abline(h = 1, col = 4, lty = 3)
text(16, 2, expression(frac(partialdiff(y), partialdiff(x)) == 1.5))
text(20, 1.5, expression(Elasticity == frac(partialdiff(y), partialdiff(x)) %*%
frac(x, y)))
a)
curve(-2 + 6 * (1/x), 1, 10)
D(expression(-2 + 6 * (1/x)), "x") # The derivative
## -(6 * (1/x^2))
curve(-(6 * (1/x^2)), 1, 10) # Plot of the derivative
b)
library(Ryacas)
## Loading required package: XML
yacas("Limit(x,0)(-(6 * (1/x^2)))") # The derivative is largest as x goes to zero -> -Infinity
## [1] "Starting Yacas!"
## expression(-Inf)
yacas("Limit(x,Infinity)(-(6 * (1/x^2)))") # x -> Infinity, derivative -> 0
## expression(0)
c)
fn = yacas(expression(-(6 * (1/x^2)))) # Define the derivative as a function (fn)
Eval(fn, list(x = 5)) # Evaluate the function at x=5
## [1] -0.24
a) Try using wolfram alpha for this one: http://www.wolframalpha.com/input/?i=x%5E1%2F2+*+x%5E1%2F6
a)
curve(0.5 + 0.2 * log(x), 1, 50)
dx = D(expression(0.5 + 0.2 * log(x)), "x")
dx
## 0.2 * (1/x)
library(mosaic)
## Loading required package: lattice
## Loading required package: grid
## Loading required package: survival
## Loading required package: splines
## Loading required package: Hmisc
## Hmisc library by Frank E Harrell Jr
##
## Type library(help='Hmisc'), ?Overview, or ?Hmisc.Overview') to see overall
## documentation.
##
## NOTE:Hmisc no longer redefines [.factor to drop unused levels when
## subsetting. To get the old behavior of Hmisc type dropUnusedLevels().
## Attaching package: 'Hmisc'
## The following object(s) are masked from 'package:survival':
##
## untangle.specials
## The following object(s) are masked from 'package:base':
##
## format.pval, round.POSIXt, trunc.POSIXt, units
## Attaching package: 'mosaic'
## The following object(s) are masked from 'package:Hmisc':
##
## do
## The following object(s) are masked from 'package:stats':
##
## binom.test, D, median, prop.test, sd, var
## The following object(s) are masked from 'package:base':
##
## max, mean, min, print, sample
g <- makeFun(0.5 + 0.2 * log(x) ~ x)
g(x = 49)
## [1] 1.278
f <- makeFun(0.2 * (1/x) ~ x)
f
## function (x)
## 0.2 * (1/x)
f(x = 49) # The slope at t=49
## [1] 0.004082
f(x = 49) * 49/g(x = 49) # The elasticity at t=49
## [1] 0.1564