Announcement

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

  • How to fit multiple AFT models and store their AIC and BIC in a tabular form for comparison

    I would like to save the AIC and BIC values in tabular form for fitted Accelerated Failure Time (AFT) models. Currently, I need to run each model separately, and run the "estat ic" command repetatively. Can we automate the comparison process using a loop.

    HTML Code:
    // Example of Exponential AFT model
    streg age ib(1).sex ib(0).ph_ecog ph_karno pat_karno meal_cal wt_loss, distribution(exponential) time allbaselevels cformat(%9.3f) pformat(%5.3f) sformat(%8.3f)
    
    estat ic
    
    
    // Example of Loglogistic AFT model
    streg age ib(1).sex ib(0).ph_ecog ph_karno pat_karno meal_cal wt_loss, distribution(loglogistic) time allbaselevels cformat(%9.3f) pformat(%5.3f) sformat(%8.3f)
    
    estat ic
    I know how to fit an AFT model and interpret it, but I would like to automate the fitting using a for loop where it would iterate through the distribution names [exponential, loglogistic etc], fit the model like above and estimate the AIC and BIC then save it in tabular form. Once the computation done for all model, then it prints the table with “model name”, "AIC" and "BIC" columns and corresponding values.

    I'm new to stata and still learning the coding part. Any help regarding this will be appreciated.
    Last edited by Rahul Raoniar; 26 Oct 2021, 13:02.

  • #2
    I come up with one solution that works, it only prints the results.

    HTML Code:
    foreach model in "exponential" "loglogistic"  "weibull" "lognormal" "ggamma"  {
        quietly streg age ib(1).sex ib(0).ph_ecog  ph_karno pat_karno meal_cal wt_loss, distribution(`model') time
        quietly estat ic
        quietly matrix list r(S)
        matrix S = r(S)
        scalar aic = S[1,5]
        scalar bic = S[1,6]
        di " `model'    " " AIC: " aic " BIC: " bic
    }

    Here is the output
    HTML Code:
     exponential     AIC: 558.65921 BIC: 589.44403
     loglogistic     AIC: 547.22439 BIC: 581.42974
     weibull     AIC: 535.54847 BIC: 569.75382
     lognormal     AIC: 565.19123 BIC: 599.39658
     ggamma     AIC: 537.34394 BIC: 574.96983
    How can I convert it into a beautiful tabulated format?
    Last edited by Rahul Raoniar; 26 Oct 2021, 14:01.

    Comment


    • #3
      Code:
      frame create ics str32 model float(aic bic)
      foreach model in exponential "oglogistic  weibull lognormal ggamma  {
          quietly streg age ib(1).sex ib(0).ph_ecog  ph_karno pat_karno meal_cal wt_loss, distribution(`model') time
          quietly estat ic
          matrix S = r(S)
          frame post ics ("`model'") (S[1,5]) (S[1, 6])
      }
      
      frame change ics
      fromat aic bic  %3.2f
      list, nobs clean
      At the end of this code, in addition having to a table-like listing in the Results window, frame ics contains a data set with the model names, and their AICs and BICs, which you can save for later use, or perform further analysis on there at this time.

      Comment


      • #4
        Thank you, Clyde Schechter for the wonderful solution. I'm familiar with the R and Python version of data frame, but today I learned that Stata also offers frames, and we can handle multiple dataframes in a single session.

        Comment

        Working...
        X