Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to plot log-odds for spline

    Hi all, I would like to plot the results of a spline regression as log-odds or odds ratio. However, if additional variables are added to the model, the final plot is messy. Please consider the example below

    Code:
    sysuse auto, clear
    mkspline2 spl_price = price, cubic nknots(4)
    mkspline2 spl_weight = weight, cubic nknots(4)
    logit foreign spl_price*
    predict xb1, xb
    twoway line xb1 price, sort name(plot_ok, replace)
    logit foreign spl_price* spl_weight*
    predict xb2, xb
    twoway line xb2 price, sort name(plot_not_ok, replace)
    Any suggestion?

    Many thanks,
    Manuel

  • #2
    Code:
    sysuse auto, clear
    mkspline spl_price = price, cubic nknots(4)
    mkspline spl_weight = weight, cubic nknots(4)
    
    logit foreign spl_price* spl_weight*
    
    // find values at which to fix spl_weight1 - spl_weight3
    sum spl_weight1 if weight == 3200 // close to the median
    local at1 = r(mean)
    sum spl_weight2 if weight == 3200
    local at2 = r(mean)
    sum spl_weight3 if weight == 3200
    local at3 = r(mean)
    
    // predict the odds
    margins, at(spl_weight1=`at1' spl_weight2 = `at2' spl_weight3 = `at3') ///
        over(price) expression(exp(xb()))
    
    //plot the odds   
    marginsplot, noci yscale(log)    ///
        ylab(0.00001  "10{sup:-5}"   ///
             0.001    "10{sup:-3}"   ///
             0.1      "10{sup:-1}"   ///
             10       "10{sup:1}"    ///
             1000     "10{sup:3}"    ///
             100000   "10{sup:5}"    ///
             10000000 "10{sup:7}", angle(0))   ///
             plotopts(msymbol(oh)) ///
             ytitle("odds of being foreign (log scale)")   ///
             scheme(s1mono)
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	65.5 KB
ID:	1666050
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Hi Maarten,
      Thank you for your help. I assume if I have more than two spline variables in the model, the same procedure should be done for all. What if I also have a categorical control variable, like this:


      Code:
      sysuse auto, clear
      mkspline spl_price = price, cubic nknots(4)
      mkspline spl_weight = weight, cubic nknots(4)
      logit foreign spl_price* spl_weight* i.rep78
      Also, could you please provide the code to show odds ratios instead of log-odds?
      Last edited by Manuel Ferraro; 26 May 2022, 02:36.

      Comment


      • #4
        Basically, my question is whether and how it would be possible, with Stata, to obtain curves with odds ratios (or changes in log-odds) such as those below. Please note that there is no referent value but rather the odds ratios are plotted across all values of the spline exposure.

        Click image for larger version

Name:	Restricted-cubic-spline-plot-of-odds-ratios-for-failure-to-restore-renal-function-Serum.png
Views:	1
Size:	27.5 KB
ID:	1667603


        Source: https://www.researchgate.net/figure/...fig3_346527412

        Comment


        • #5
          Can you provide N example using only linear and quadratic polynomials?

          Comment

          Working...
          X