Plotting density of logit and probit

I am trying to plot densities for the logit and probit models with plot function but it's not working out.
The code is below.
if ggplot2 can be used to achieve same outcome then it would be of great help.
dataset available here MP NSS.dta - Google Drive

MP_NSS$scst= MP_NSS$socgrp
MP_NSS$scst= ifelse(MP_NSS$socgrp== c(1, 2), 1, 0)
MP_NSS$pds2= MP_NSS$pds_wheat_qty
  replace_na(list(pds2= 0))
MP_NSS$pds2= ifelse(MP_NSS$pds2>0,1, 0)
MP_NSS$total_land2= MP_NSS$total_land
   replace_na(list(total_land2= 0))
logit3= glm(pds2~ scst+  unitvalue_rice_wheat+ total_land2+ I(total_land2* total_land2), family = binomial(link = "logit"), data = MP_NSS)
plot(MP_NSS$unitvalue_rice_wheat, MP_NSS$pds2)
curve(exp(coef(logit3)[1]+ (coef(logit3)[2]*(MP_NSS$unitvalue_rice_wheat)))/(1+ exp(coef(logit3)[1]+ (coef(logit3)[2]*(MP_NSS$unitvalue_rice_wheat)) )))

Hi @tsrockon28,

If you check out this post, Plotting your logistic regression model, it takes you through both the base and ggplot2 versions of making a chart from the model. The R Markdown document is also available:

but didn't help.
not getting typica llogistic curve.

If you can make this into a self-contained reprex, so others can see the code and output, that would be great. It's hard to tell what the problem is from description alone, and it's a tall-ask of others to download an external data file, etc.


1 Like


For curve() it wants an expression that uses the variable x. Replace MP_NSS$unitvalue_rice_wheat with x. Use add = TRUE to draw on top of the previous plot.

did that but getting weird curve

see this

So, if you take a look at the URL here, you can see it begins with localhost, which means it's only on your computer (it's not a link to something other people can see on the web).

I definitely recommend taking the 10-15 minutes at some point to watch Jenny Bryan’s quick, helpful overview of the package and how to use it (Jenny starts ~10:40):

Others on these boards, including those new to R (looking at you, @christinelly :wave:), can definitely attest to the worthiness (and relative ease) of getting reprex-ing down!

1 Like

Thanks Mara for all the help.
Margins package with cplot function achieves all this beautifully.

1 Like