Announcement

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

  • Compare multiple slopes in linear regression models

    I would like to compare the slope of the linear regression in each age group.
    First, I would like to perform trend analysis.
    Second, I'd like to compare the slope of each group in the entire group followed by comparing specific pair.

    The data are like following.

    twoway line death year if agegroup==4, lcolor("50 136 189*3") ///
    || line death year if agegroup==5, lcolor("50 136 189*1.5") ///
    || line death year if agegroup==6, lcolor("50 136 189") ///
    || line death year if agegroup==7, lcolor("50 136 189*.6") ///
    || line death year if agegroup==8, lcolor("50 136 189*.30") ///
    || line death year if agegroup==9, lcolor("50 136 189*.15")


    Click image for larger version

Name:	2022-01-17 0.33.29.png
Views:	1
Size:	23.1 KB
ID:	1645374


    Do I have to manually pick up the coefficient data after performing the following command?

    foreach i in 4 5 6 7 8 9 {
    regress death year if agegroup== `i' ,noheader
    }


    Any good command to perform trend analysis in a smarter way?

    Also, how can I compare coefficients only with their mean, standard error and 95%CI ?


  • #2
    Code:
    regress death c.year##i(4/9).agegroup
    margins agegroup, dydx(year) pwcompare
    While showing a graph of the data is of some value in that it gives a sense of the values of the data, for the purpose of developing code, that is not so important. Much more helpful is an example data set, so that we see the actual organization of the data and metadata--these are usually more important for developing code. The helpful way to do that is with the -dataex- command. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Thank you, Clyde.
      I uploaded simpler example data with 4 age groups (named 6, 7, 8, 9).

      Code:
      twoway line death year if agegroup==6 ///
      || line death year if agegroup==7 ///
      || line death year if agegroup==8 ///
      || line death year if agegroup==9 ///
      , legend(lab(1 "agegroup 6") lab(2 "agegroup 7") lab(3 "agegroup 8") lab(4 "agegroup 9"))

      regress death c.year##i(6/9).agegroup
      margins agegroup, dydx(year) pwcompare


      Using Clyde's code, I can compare coefficient. Thank you.

      Can I perform trend analysis for these coefficient?
      (I want to perform trend analysis for the slopes of linear regression (x axis=year, y axis=death) by age groups)
      Attached Files

      Comment


      • #4
        Well, you can do another regression analysis using agegroup as a continuous variable instead and look at the coefficient for the year#agegroup interaction. While this approach can be questionable in some settings, your agegroup variable is pretty well suited to it because the agegroups actually correspond to specific numerical ranges of age. So
        Code:
        regress c.year##c.agegroup if inrange(agegroup, 6, 9)
        Note: I appreciate the intent of attaching a sample.dta file. But like some others here, I do not risk downloading files from people I do not know. If you need more specific advice, please re-read the information I gave in #1 about using the -dataex- command to show example data.

        Comment


        • #5
          Thank you for the reply and also I understand showing data with dataex.

          Just follow up for #3,
          Can I say that the code should be following (may need to add outcome variable?)

          regress death c.year##c.agegroup if inrange(agegroup, 6, 9)

          Comment


          • #6
            Yes. Sorry, I forgot to type in the outcome variable--getting tired late in the day, I guess.

            Comment


            • #7
              Thank you so much!

              Comment

              Working...
              X