Announcement

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

  • Linear and cubic splines, margins and plot - something is wrong!

    Dear all,

    I want to compare different models with linear regression, linear splines, and cubic splines, but I think I have misunderstood something. I don't get the code to work.

    I get these messages:
    [BMI] not found
    invalid syntax
    BMI ambiguous abbreviation
    estimation result BMIlinearsp not found

    **Fitting the models and sacing results.**

    *Linear regression.
    regress SBP BMI age i.sex i.smoker
    local betahat1= `:di %5.3f _b[BMI]'
    quietly eststo linear: margins, at(BMI=(17(0.5)51)) post

    * A model with linear splines of BMI with cut-offs at 25, 30 and 35.
    mkspline BMI1 25 BMI2 30 BMI3 35 BMI4= BMI, margin
    regress SBP age i.sex i.smoker BMI1-BMI4
    local betahat2= `:di %5.3f _b[BMI1]'
    quietly eststo BMIlinearsp: margins, at(BMI=(17(0.5)51)) post

    *A model with cubic splines with three knots.
    mkspline BMI_sp= BMI, cubic knots(25 30 35) displayknots
    regress SBP age i.sex i.smoker BMI_sp*
    predict rcsfit
    regress SBP BMI_sp* age i.sex i.smoker
    local betahat3= `:di %5.3f _b[BMI]'
    quietly eststo BMIcubicsplines: margins, at(BMI=(17(0.5)51)) post


    ** Determining the effect on SBP on BMI 27->28.**

    *Linear regression.
    quietly regress SBP BMI age i.sex i.smoker
    margins, at(BMI=(27 28)) post
    lincom _b[2._at]-_b[1._at]

    *Linear splines - Not sure about this code!!
    quietly regress SBP BMI1-BMI4 age i.sex i.smoker
    margins, at(BMI1=27 BMI2=2 BMI3=0 BMI4=0 BMI1=28 BMI2=3 BMI3=0 BMI4=0) post
    lincom ((_b[3._at]+_b[4._at])/2)-((_b[1._at]+_b[2._at])/2)

    *Cubic splines - Not sure about this code either!!
    quietly regress SBP age i.sex i.smoker BMI_sp*
    margins, at(BMI_sp1=(27 28)) post
    lincom _b[2._at]-_b[1._at]

    *Then the plot, but I don't get the splines to be plotted, there is something with my code. Do you know how to solve it?
    coefplot linear BMIlinearsp BMIcubicsplines, at title(Effect of BMI on Systolic blood preassure) ytitle(Systolic blood preassure) xtitle(BMI) leg(order(1 "Linear model (B{sub:1}=`betahat1')"2 "Linear splines (B{sub:2}=`betahat2')"3 "Cubic splines (B{sub:3}=`betahat3')") pos(6)) recast(line) noci

    Thank you so much in advance!

    Anna

  • #2
    Someone who knows about splines who can help me?

    Comment


    • #3
      Hi Anna,
      This is really a problem about how Stata understands and interprets variables created with mkspline.
      Namely, it wont understand them.
      what you can do is use -f_able- from SSC.
      Here is the PPT explaining https://www.stata.com/meeting/us20/s...ios-Avila1.pdf
      and the Paper https://journals.sagepub.com/doi/pdf...6867X211000005
      Also look into the examples in the help file
      Fernando

      Comment

      Working...
      X