econocharts icon indicating copy to clipboard operation
econocharts copied to clipboard

Budget constraint graph

Open jvdiblasi opened this issue 5 years ago • 1 comments

Hello, your initiative is excellent.

It would be possible to add that a basic graph could be added that joins the budget constraint with the indifference curve.

Thanks.

jvdiblasi avatar Nov 16 '20 13:11 jvdiblasi

I also thought about this. Right now the function I developed (not published yet) allows creating budget constraint as in the image below, but not the corresponding indifference curves. The problem is that I can't add the indifference curve from a line in an automatic manner, but add a tanget to a indifference curve, so I'm not sure if this functionality should be added within the indifference function.

budget(rect1, rect2, x = c(3, 5), labels = c("A", "B"))

imagen

What I've tried so far is the following:

# Data
curve2 <- data.frame(Hmisc::bezier(c(1, 3, 9),
                                   c(9, 3, 1)))
x <- curve2$x
y <- curve2$y
plot(x, y)
spl <- smooth.spline(y ~ x)
lines(spl, col = 2)

newx <- 4
pred0 <- predict(spl, x = newx, deriv = 0)
pred1 <- predict(spl, x = newx, deriv = 1)

yint <- pred0$y - (pred1$y * newx)
xint <- -yint / pred1$y

ggplot(curve2, aes(x = x, y = y)) +
  geom_line(data = data.frame(x = x, y = yint + pred1$y * x), color = "red", lwd = 2) +
  geom_line(lwd = 2) + 
  geom_point(data = data.frame(x = pred0$x, y = pred0$y), size = 3, color = "green")

imagen

The previous code can be used to add the tangent of a curve. What do you think? If you have any other suggestions or ideas please let me know.

R-CoderDotCom avatar Nov 16 '20 19:11 R-CoderDotCom