Announcement

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

  • Piecewise regression with interaction with categorical var - some groups are non-linear after spline

    I have used mkspline to make the variables for a piecewise regression
    Code:
    mkspline preH 3 postH = time
    generate jump = 1
    replace  jump = 0 if time < 3
    Then fit a piecewise regression with an interaction by a categorical variable with 3 levels:

    Code:
    mixed hrelsat c.preH#ibn.H_Group i.jump#ibn.H_Group c.postH#ibn.H_Group || CSID: preH jump postH
    This works just fine (see attached), but when I plot the raw means by group (see attached) I see that two of the groups appear to have a quadratic trend after the spline while only one is truly linear. Is there a way to incorporate these quadratic trends after the spline for these two groups to at least test if the quadratic is actually significant?


    h_means_bytraj.pngh_multigroup_piecewise.png

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double CSID byte time double(hrelsat wrelsat H_Group W_Group H5_Exposure W5_Exposure) byte(preH postH) float jump
    10056 0                50                46 3 3 1  2 1 0 0
    10056 1                50                46 3 3 1  2 2 0 0
    10056 2                46                50 3 3 1  2 3 0 1
    10056 3                50                43 3 3 1  2 3 2 1
    10056 4                48                27 3 3 1  2 3 3 1
    10056 5                50                41 3 3 1  2 3 4 1
    10060 0                47                46 2 2 4  . 1 0 0
    10060 1                35                15 2 2 4  . 2 0 0
    10060 2                37                46 2 2 4  . 3 0 1
    10060 3                 .                 . 2 2 4  . 3 2 1
    10060 4                 .                 . 2 2 4  . 3 3 1
    10060 5                 .                 . 2 2 4  . 3 4 1
    10073 0                48                44 3 3 5  5 1 0 0
    10073 1                50                52 3 3 5  5 2 0 0
    10073 2                49                52 3 3 5  5 3 0 1
    10073 3                47                52 3 3 5  5 3 2 1
    10073 4                51                52 3 3 5  5 3 3 1
    10073 5                48                52 3 3 5  5 3 4 1
    10080 0                51                52 3 3 7  5 1 0 0
    10080 1                51                49 3 3 7  5 2 0 0
    10080 2                48                50 3 3 7  5 3 0 1
    10080 3                46                46 3 3 7  5 3 2 1
    10080 4                45                48 3 3 7  5 3 3 1
    10080 5                46                50 3 3 7  5 3 4 1
    10081 0                17                20 1 1 .  . 1 0 0
    10081 1                 .                 . 1 1 .  . 2 0 0
    10081 2                 .                 . 1 1 .  . 3 0 1
    10081 3                50                41 1 1 .  . 3 2 1
    10081 4                 .                 . 1 1 .  . 3 3 1
    10081 5                 .                 . 1 1 .  . 3 4 1
    10089 0                45                30 3 2 3  6 1 0 0
    10089 1                46                32 3 2 3  6 2 0 0

  • #2
    You can always make your splines manually and include non-linear terms.

    Comment


    • #3
      I will also just note in passing that in the example data shown in #1, the values of jump, preH and postH are not the results from
      Code:
      mkspline preH 3 postH = time
      generate jump = 1
      replace jump = 0 if time < 3
      So something is wrong with the data in the first place. I'd fix that, before worrying about adding more details to the model. Also, data errors are like cockroaches: if you see one there is often a large infestation. So before you do anything else with the data, you should review the data management so far and fix any other errors that may be lurking there. If you trace data errors back to the original source, contact them and ask them to fix them.

      Get the data right first, then worry about modeling details.

      Comment


      • #4
        Thanks very much for pointing that out Clyde. I think I've discovered the root of the problem. Does this seem correct?

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input double CSID byte time double(hrelsat wrelsat H_jointtraj_Group W_jointtraj_Group H5_Exposure W5_Exposure) byte(preH postH) float jump
        10056 0                50                46 3 3 1  2 0 0 0
        10056 1                50                46 3 3 1  2 1 0 0
        10056 2                46                50 3 3 1  2 2 0 0
        10056 3                50                43 3 3 1  2 3 0 1
        10056 4                48                27 3 3 1  2 3 1 1
        10056 5                50                41 3 3 1  2 3 2 1
        10060 0                47                46 2 2 4  . 0 0 0
        10060 1                35                15 2 2 4  . 1 0 0
        10060 2                37                46 2 2 4  . 2 0 0
        10060 3                 .                 . 2 2 4  . 3 0 1
        10060 4                 .                 . 2 2 4  . 3 1 1
        10060 5                 .                 . 2 2 4  . 3 2 1
        10073 0                48                44 3 3 5  5 0 0 0
        10073 1                50                52 3 3 5  5 1 0 0
        10073 2                49                52 3 3 5  5 2 0 0
        10073 3                47                52 3 3 5  5 3 0 1
        10073 4                51                52 3 3 5  5 3 1 1
        10073 5                48                52 3 3 5  5 3 2 1
        10080 0                51                52 3 3 7  5 0 0 0
        10080 1                51                49 3 3 7  5 1 0 0
        10080 2                48                50 3 3 7  5 2 0 0
        10080 3                46                46 3 3 7  5 3 0 1
        10080 4                45                48 3 3 7  5 3 1 1
        10080 5                46                50 3 3 7  5 3 2 1
        10081 0                17                20 1 1 .  . 0 0 0
        10081 1                 .                 . 1 1 .  . 1 0 0
        10081 2                 .                 . 1 1 .  . 2 0 0
        10081 3                50                41 1 1 .  . 3 0 1
        10081 4                 .                 . 1 1 .  . 3 1 1
        10081 5                 .                 . 1 1 .  . 3 2 1
        10089 0                45                30 3 2 3  6 0 0 0
        10089 1                46                32 3 2 3  6 1 0 0
        10089 2                46                34 3 2 3  6 2 0 0
        10089 3                43                49 3 2 3  6 3 0 1
        10089 4                47 3.333333333333333 3 2 3  6 3 1 1
        10089 5                42                16 3 2 3  6 3 2 1
        10098 0                48                28 3 1 .  . 0 0 0
        10098 1                 .                 . 3 1 .  . 1 0 0
        10098 2                 .                 . 3 1 .  . 2 0 0
        10098 3 46.66666666666667                28 3 1 .  . 3 0 1
        10098 4                 .                 . 3 1 .  . 3 1 1
        10098 5                 .                 . 3 1 .  . 3 2 1
        10102 0                46                50 2 3 5  4 0 0 0
        10102 1                27                47 2 3 5  4 1 0 0
        10102 2                42                51 2 3 5  4 2 0 0
        10102 3                51                51 2 3 5  4 3 0 1
        10102 4                47                51 2 3 5  4 3 1 1
        10102 5                45                48 2 3 5  4 3 2 1
        10108 0                50                29 3 1 2  4 0 0 0
        10108 1                49                23 3 1 2  4 1 0 0
        10108 2                47                27 3 1 2  4 2 0 0
        10108 3                52                52 3 1 2  4 3 0 1
        10108 4                44                28 3 1 2  4 3 1 1
        10108 5                48                35 3 1 2  4 3 2 1
        10125 0                43                50 2 3 6  6 0 0 0
        10125 1                34                49 2 3 6  6 1 0 0
        10125 2                32                51 2 3 6  6 2 0 0
        10125 3                 .                23 2 3 6  6 3 0 1
        10125 4 41.11111111111111                50 2 3 6  6 3 1 1
        10125 5                50                50 2 3 6  6 3 2 1
        10126 0 41.11111111111111 37.77777777777778 3 3 .  . 0 0 0
        10126 1                 .                 . 3 3 .  . 1 0 0
        10126 2                 .                 . 3 3 .  . 2 0 0
        10126 3                 .                49 3 3 .  . 3 0 1
        10126 4                 .                 . 3 3 .  . 3 1 1
        10126 5                 .                 . 3 3 .  . 3 2 1
        10144 0                51                49 2 3 5 10 0 0 0
        10144 1                40                48 2 3 5 10 1 0 0
        10144 2                31                39 2 3 5 10 2 0 0
        10144 3                50                49 2 3 5 10 3 0 1
        10144 4                49                52 2 3 5 10 3 1 1
        10144 5                52                51 2 3 5 10 3 2 1
        10162 0                50                44 3 3 2  0 0 0 0
        10162 1                51                41 3 3 2  0 1 0 0
        10162 2                37                38 3 3 2  0 2 0 0
        10162 3                46                22 3 3 2  0 3 0 1
        10162 4                36                19 3 3 2  0 3 1 1
        10162 5                19                20 3 3 2  0 3 2 1
        10166 0                47                49 3 3 3  7 0 0 0
        10166 1                46                50 3 3 3  7 1 0 0
        10166 2                 .                 . 3 3 3  7 2 0 0
        10166 3 52.22222222222222                49 3 3 3  7 3 0 1
        10166 4                 .                 . 3 3 3  7 3 1 1
        10166 5                52                 . 3 3 3  7 3 2 1
        10212 0                45                50 3 3 .  1 0 0 0
        10212 1                47                48 3 3 .  1 1 0 0
        10212 2                42                28 3 3 .  1 2 0 0
        10212 3 37.77777777777778                46 3 3 .  1 3 0 1
        10212 4                 .                46 3 3 .  1 3 1 1
        10212 5                 .                50 3 3 .  1 3 2 1
        10240 0                46                43 3 3 .  . 0 0 0
        10240 1                49                44 3 3 .  . 1 0 0
        10240 2                48                47 3 3 .  . 2 0 0
        10240 3                50                16 3 3 .  . 3 0 1
        10240 4                 .                 . 3 3 .  . 3 1 1
        10240 5                 .                 . 3 3 .  . 3 2 1
        10241 0                35                43 2 3 6  1 0 0 0
        10241 1                37                40 2 3 6  1 1 0 0
        10241 2                37                40 2 3 6  1 2 0 0
        10241 3                34                 4 2 3 6  1 3 0 1
        end

        Comment


        • #5
          Yes, that data looks better, consistent with your description of it.

          Comment


          • #6
            Thank you Clyde! Any thoughts about the quadratic term?

            Would this do it?
            Code:
             mixed hrelsat c.preH#ibn.H_Group i.jump#ibn.H_Group c.postH##c.postH#ibn.H_Group || CSID: preH jump postH
            Phil, can you be more specific?

            Comment


            • #7
              Re #6, yes, I believe that code would do what you ask.

              Comment


              • #8

                Hannah,

                How did you plot your model? Can you share the code? I'm trying to plot a similar model but can't seem to get it right.

                Thanks!

                Comment

                Working...
                X