Announcement

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

  • Out of sample prediction formula using stpm2

    Dear All,

    I would be grateful for help in producing the formula required to make out of sample predictions following from fitting a flexible parametric model using stpm2.

    I have constructed a flexible parametric survival model using stpm2 on the scale (odds), with a df of 4 and a dftvc 1. I've obtained the coefficients of my covariates,_rcs [1 to 4], the rcs_tvcs and the constant term as shown below.
    Coefficient Std. err. z P>z [95% conf. interval]
    xb
    fix_1 0.223 0.131 1.7 0.088 -0.034 0.48
    fix_2 0.672 0.133 5.05 0 0.411 0.933
    fix_3 0.567 0.134 4.23 0 0.305 0.83
    fix_4 0 (omitted)
    BMI3_centered 0.009 0.004 2.55 0.011 0.002 0.017
    age_centered 0.009 0.002 4.12 0 0.005 0.013
    Sex 0.177 0.044 4.06 0 0.092 0.263
    rfp_1 -0.356 0.079 -4.48 0 -0.512 -0.2
    rfp_2 0.162 0.147 1.1 0.27 -0.126 0.45
    Revised
    2 0.17 0.075 2.28 0.023 0.024 0.317
    3 or greater 0.311 0.086 3.61 0 0.142 0.48
    Size
    >=26 & size_head<30 -0.46 0.136 -3.39 0.001 -0.726 -0.194
    >=30 & size_head<36 -1.038 0.14 -7.43 0 -1.312 -0.764
    36 -1.357 0.147 -9.2 0 -1.646 -1.068
    >36 -1.428 0.287 -4.97 0 -1.991 -0.865
    Posterior -0.519 0.048 -10.85 0 -0.613 -0.425
    sickness 0.038 0.004 9.77 0 0.03 0.045
    N_5yr 0.356 0.098 3.63 0 0.164 0.549
    M_5yr 0.512 0.061 8.38 0 0.392 0.632
    F_5yr 0.309 0.075 4.1 0 0.161 0.457
    A_5yr 0.605 0.116 5.21 0 0.378 0.833
    F_5yr 1.074 0.208 5.15 0 0.665 1.482
    Frasurg -0.596 0.245 -2.44 0.015 -1.076 -0.117
    Nfrasurg 0.755 0.257 2.94 0.003 0.252 1.258
    _rcs1 2.602 0.135 19.34 0 2.338 2.866
    _rcs2 0.148 0.021 6.91 0 0.106 0.191
    _rcs3 -0.035 0.022 -1.61 0.108 -0.077 0.008
    _rcs4 -0.082 0.009 -9.6 0 -0.099 -0.066
    _rcs_Sex1 -0.109 0.014 -7.9 0 -0.136 -0.082
    _rcs_rfp_2 0.203 0.068 2.99 0.003 0.07 0.336
    _rcs_fix4 0.001 0.045 0.02 0.985 -0.087 0.089
    _cons 3.132 0.557 5.62 0 2.04 4.223
    I would now like to predict survival probabilities at various time points, but particularly at 2 years (=2) on an external platform for new values of the covariates to be inputted. I have obtained the positions of the knots which are: -5.900581836700439 -2.956143140792847 -1.968756437301636 -.3671925663948059 .6924625039100647. I am having trouble writing out the equation, and would be grateful for any help available!

    Many thanks in advance,
    Ben

  • #2
    Ben,

    It will be easier to calculate the splines externally if you use the noorthog option.

    Below is some code that extracts the knots and then regenerates the spline variables at 2 years and obtains some predictions.
    You will need to do something similar for your example

    Code:
    clear
    use https://www.pclambert.net/data/rott3
    
    stpm2 hormon age, df(4) tvc(hormon) dftvc(1) scale(odds) noorthog
    
    // generate predictions at 2 years
    gen t2 = 2
    gen lnt2 = ln(t2)
    
    // store knots
    local knots `e(ln_bhknots))'
    local Nknots  = wordcount("`knots'")
    local minknot = word("`knots'",1)
    local maxknot = word("`knots'",`Nknots')
    
    
    // generate new spline variables
    gen newrcs1 = lnt2
    forvalues j = 2/`=`Nknots'-1' {
      local knot`j' = word("`knots'",`j')
      local lambda = (`maxknot' - `knot`j'')/(`maxknot'-`minknot')
      gen newrcs`j' = (lnt2>`knot`j'')* (lnt2-`knot`j'')^3 -             ///
                      (lnt2>`minknot')*`lambda'*(lnt2-`minknot')^3 -     ///
                      (lnt2>`maxknot')*(1-`lambda')*(lnt2-`maxknot')^3 
    }
    
    
    gen xb = _b[hormon]*hormon              +    ///
             _b[age]*age                    +    ///
             _b[_rcs1]*newrcs1              +    ///
             _b[_rcs2]*newrcs2              +    ///
             _b[_rcs3]*newrcs3              +    ///
             _b[_rcs4]*newrcs4              +    ///
             _b[_rcs_hormon1]*lnt2*hormon   +    ///
             _b[_cons]
             
    // transform to survival
    gen S1 = 1/(1 + exp(xb))
    
    // compare with predict option
    predict S2, surv timevar(t2)
    compare S1 S2

    Comment


    • #3
      Dear Prof Lambert,

      Thank you so much - that was really helpful!

      Best wishes,
      Ben

      Comment

      Working...
      X