Announcement

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

  • Margins and margins effects using splines and fractional multinomial logit model

    Dear all,

    Using Stata 15, we are analyzing the effect of age (
    age is from 16 years to 80 years)
    , period (year of the Survey; time gab 3 years) and cohort (birth year in 5-year cohorts) [APC-models; e.g. Yang & Land, 2006] on the percentages of different beverages (e.g. beer, wine, spirits etc.) of alcohol overall consumption, e.g. 40% of all alcohol consumed was beer. We are using individual data, Overall 50.000 persons, 7 surveys. Therefore, we use a fractional multinomal logit model (fmlogit; Buis, 2008). Nevertheless, period and cohort are coded as factors while age is used as a restriced cubic spline using rc_splines for flexibility reasons.
    The main aim is calculating predictions (margins) for age, period and cohort and marginal effects for interpretation purposes (since the parameters in the model are difficult to interpret).
    The main problem is getting predictions (margins) or marginal effects for the age-splines.

    Short example:

    rc_spline age, nknots(5)
    fmlogit beer wine spirits, eta(i.period i.cohort _Sage_1-_Sage_4)
    *cohort-effects
    margins i.cohort, at( (asbalanced) _factor (mean) _continuous) force predict(outcome(beer))
    *period-effects
    margins i.period, at( (asbalanced) _factor (mean) _continuous) force predict(outcome(beer))
    *age effects

    margins, at( _Sage_1=(16(1)80) (asbalanced) _factor (mean) _continuous) force predict(outcome(beer))
    *marginal effects
    margins, dydx(*) predict(outcome(beer))


    Is this the "right way" to get "age effects" - margins for age? Because postrcspline is not applicable with fmlogit and only displays the results in a graph; ist here another way to include all spline variables to get margins?
    How to get
    margin effects for age because postrcspline is not applicable and dfmlogit is not applicable for Stata 15?; or is ist even possible to get soething like marginal effects for a continous variable or spline?


    Thanks in advance.



    Reference:
    Yang, Yang and Kenneth C. Land. 2006. ‘‘A Mixed Models Approach to the Age-Period-Cohort Analysis of Repeated Cross-Section Surveys: Trends in Verbal Test Scores.’’Sociological Methodology 36:75-97

  • #2
    Hi Nicki
    I came up with a command named "f_able" for cases similar to that, with the caveat that you will have to create the splines by hand:
    Let me show you an example:
    Code:
    webuse sysdsn1, clear
    ssc install f_able
    fgen age2=age^2
    mlogit insure age age2 male nonwhite i.site
    f_able, nlvar(age2)
    margins, dydx(age) nochain numerical
    ** this should produce the same as
    mlogit insure age c.age#c.age male nonwhite i.site
    margins, dydx(age)
    so, if you can create the splines with a single fgen line, you can use it combined with f_able.

    For example, with a single knot quadratic spline
    Code:
    webuse sysdsn1, clear
    ssc install f_able
    fgen age2=age^2
    fgen age3=max(age-40,0)^2
    mlogit insure age age2 age3 male nonwhite i.site
    f_able, nlvar(age2 age3)
    margins, dydx(age) nochain numerical
    HTH

    Comment


    • #3
      Dear Fernando,

      thank you very much for your command "f_able" which is very helpful.

      But I have some struggle implementing restricted cubic splines because the syntax is quite long (see following code for age4) and always leading to an error: "Type mismatch".
      I think this might be the case if the label inserted with fgen is "see notes" and maybe too long...

      Code:
      webuse sysdsn1, clear
      ssc install f_able
      *example of components of a restricted cubic spline
      fgen age4=max(age- 48,0)^3 - ((74-48)/(74-61))*max(age- 61,0)^3 + ((61-48)/(74-61))*max(age- 74,0)^3
      mlogit insure age age4 male nonwhite i.site
      f_able, nlvar(age4)
      Would you please check if this is the problem.
      ...first I thought the problem was in using fmlogit, but it worked with simple cubic splines.
      Last edited by Nicki Seitz; 23 Sep 2020, 07:25.

      Comment


      • #4
        Hi Nicki
        Thank you very much for the feed back. I realize I had to correct something on the code.
        this was the first time i needed to use f_able for such a "long" variable transformation.

        So, if you can go and edit the file "fgen.ado"
        and change its content to the following:
        Code:
        *! v1.01 FRA Change fgen. Note needed to be stored without Quotes
        program fgen
            syntax newvarname =/exp [if] [in] 
            local typelist double
            gen `typelist' `varlist'=`exp' 
            if strlen("`exp'")<75 label var `varlist' "`exp'"
            else  {
                label var `varlist' "See notes"
                note `varlist': `exp'
            }
        end
        I ll send this update to SSC soon, but you may want to do it before that happens.

        after you do that. you can type the following:

        Code:
        capture program drop fgen
        webuse sysdsn1, clear
        ssc install f_able
        *example of components of a restricted cubic spline
        fgen age4=max(age- 48,0)^3 - ((74-48)/(74-61))*max(age- 61,0)^3 + ((61-48)/(74-61))*max(age- 74,0)^3
        mlogit insure age age4 male nonwhite i.site
        f_able, nlvar(age4)
         margins, dydx(age) nochain numerical
        This should fix that problem. But let me know if you encounter other issues. ​​​​​​​Fernando

        Comment


        • #5
          Hi Fernando,

          thank you for your quick response.
          Now f_able can handle also longer formulas.
          It also works well with fmlogit!

          I think your f_able is very handy and your unpublished manuscript is wonderful written.
          Thank you for your Stata contribution.

          Nicki

          Comment

          Working...
          X