Announcement

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

  • How to define a constraint on all parameters of all levels of a factor variable in a restricted regression

    I want to run a constrained regression where there is factor variable (with a lot of levels). I want to add the constraint that the sum of the parameters of all levels of this factor variable is 1. How should I do this? Ideally, I want to do something like

    constraint 1 sum(i.group)=1
    cnsreg y x1 x2 x3 x4 i.group, constraints(1)

    But this syntax obviously does not work. I searched for a while but did not find the right code to use. I am wondering if you are so kind to share with me some sample code that does this task. Thanks!
    Last edited by Jiahao Chen; 09 Aug 2020, 13:48.

  • #2
    So, if the variable group has, say, 5 levels, 1, 2, 3, 4, 5 you would write it as:

    Code:
    constraint def 1 1.group + 2.group + 3.group + 4.group + 5.group = 1
    cnsreg y x1 x2 x3 x4 i.group, constraints(1)
    Added: If group has too many levels for this to be practical:

    Code:
    levelsof group, local(c1)
    local c1: subinstr local c1 " " ".group+", all
    local c1 `c1'.group = 1
    constraint def 1 `c1'
    cnsreg y x1 x2 x3 x4 i.group, constraints(1)
    Last edited by Clyde Schechter; 09 Aug 2020, 13:59.

    Comment


    • #3
      Thanks!

      Comment


      • #4
        Thank you, Clyde Schechter.

        I have a similar problem. I want to add the constraint that all the parameters of all levels of a factor variable are negative. Hence, something like:

        Code:
        constraint 1 i.group < 0
        cnsreg y x1 x2 x3 x4 i.group, constraints(1)
        I have many levels, so your first solution is, unfortunately, not practical. I have been trying to adapt your second solution to my problem without success. I would be very grateful if you could give me a hand.
        Last edited by Davide Pietrobon; 04 Sep 2020, 03:36.

        Comment


        • #5
          You cannot, in Stata, constrain the coefficient of a regression to be negative. The only constraints that are supported are constraints that can be written as linear equations; no inequalities are supported.

          What you can do, though it will be very tedious to set up, is use the -nl- command for the regression, and designate the coefficients in that model to be the negative exponentials of parameters you estimate. That would guarantee that the coefficients themselves are always negative.

          Comment


          • #6
            Thank you, Clyde Schechter.

            Comment

            Working...
            X