ScPoEcon / ScPoApps

Interactive apps for our econometrics course
Other
3 stars 4 forks source link

illustrate marginal effects of probit/logit models #4

Closed floswald closed 4 years ago

floswald commented 4 years ago

background

this app relates to this chapter in our book https://scpoecon.github.io/ScPoEconometrics/binary.html

this should illustrate why there is no single marginal effect in a probit model. that is while in the linear model

dy / dx_i = \beta_i

in the nonlinear probit/logit case we have

d Pr(y_i = 1 | x) / dx_i = g(x\beta) \beta_i 

where g is the pdf of the chosen cdf G (standard normal for probit, logistic for logit)

that is: the marginal effect depends not only on \beta_i but at which x you choose to evaluate that expression. In this app we assume for simplicity that \beta = 1.

Output of app

something like this plot:

library(ggplot2)
ggplot(data.frame(x = c(-5,5)), aes(x=x)) + 
  stat_function(fun = pnorm, aes(colour = "Probit")) + 
  stat_function(fun = plogis, aes(colour = "Logit")) + 
  theme_bw() + 
  scale_colour_manual(name = "Function G",values = c("red", "blue")) +
  scale_y_continuous(name = "Pr(y = 1 | x)")

Screenshot 2020-09-08 at 09 12 31

(but for one case at a time)

where you add a straight tangent line illustrating the slope at a certain value of x.

app input

floswald commented 4 years ago

not sure ggplot is the best way to achieve this btw. maybe with base R you are better off eg.

curve( expr = pnorm, from,to)
# compute start and end points, given x and above formula (x0,x1 etc)
segment(x0 = x0,x1=x1,y0=y0,y1=y1)
floswald commented 4 years ago

looks perfect to me. let's talk about submitting this via PR next time we talk. thanks!

dxy99999 commented 4 years ago

No problem! I am learning more git skills and keep working on the rest APPs!

floswald commented 4 years ago

@dxy99999 you know what would be really cool here to add: in the box on the left where you have the slider, just at the bottom, a text output that gives the current value of the derivative. in your code, you would just display the value of tangent. can you add that please?

dxy99999 commented 4 years ago

Sure, no problem!