Announcement

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

  • Estimating the marginal effect of one variable at different values for different groups

    Dear all,

    I am new to the statalist forum and would like to ask one question about one specific use of the margins command. Please feel free to tell me if my post does not meet the community standards so that I can improve next time.

    I am running a regression with interaction terms between two factor variables (year and group) and one continuous variable (house), and interested in the marginal effects of house in different years for different groups.

    Below is a sample of my dataset.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(nw_pov_no_vehi house bnd goest ffabus ffaequ liqcer year HH_type_elderdom)
    0 1595358.6         0   2823785  10635724         0   1196519 2016 2
    0  957215.2         0         0         0 148900.14  69132.21 2016 2
    1         0         0         0         0         0 1223.1083 2016 1
    1         0         0         0         0         0  3509.789 2016 1
    1         0         0         0         0         0   3084.36 2016 1
    0  340343.2         0         0         0  67005.06 214841.63 2016 1
    0    150000         0         0         0         0     37000 2019 2
    1         0         0         0         0         0      3100 2019 1
    1         0         0         0         0         0       481 2019 1
    0   1000000     25000         0         0         0    448000 2019 2
    1         0         0         0         0         0         0 1949 1
    0  176034.6         0         0         0         0   29.3391 1950 1
    1         0         0         0         0         0    977.97 1950 1
    1         0  997.5294         0         0         0   2933.91 1950 1
    0  76973.28         0         0         0         0 2535.5906 1951 1
    0         0 3758.1074         0         0  68184.13 18111.361 1951 1
    0  48363.34 2251.0938         0         0         0  1758.667 1953 2
    1         0         0         0         0         0 2638.0005 1953 1
    0  65950.01  747.4335 15089.362         0         0  7175.361 1953 2
    0  175866.7  747.4335  79140.02  87260.57  55574.51  5276.001 1953 1
    0         0         0 123106.69         0         0 3209.5674 1953 1
    1         0         0         0         0         0         0 1953 1
    1         0         0         0         0         0         0 1954 1
    0 126958.48         0  33271.88  8395.871         0  3502.303 1954 1
    0 35135.645  2512.199         0         0         0  878.3911 1955 1
    1         0         0         0         0         0         0 1955 1
    0  45676.34         0         0         0         0   3952.76 1955 1
    0         0  3478.429         0         0         0   29645.7 1955 1
    0 129810.83         0         0         0  50134.63         0 1956 2
    0 100411.45         0         0 17675.734         0 2091.9053 1957 1
    0 267763.88 223248.14         0         0         0  627.5716 1957 2
    0    102629         0         0         0         0   610.887 1958 1
    0         0         0         0         0 9591.3125 1018.1449 1958 2
    1         0         0         0         0         0  5649.209 1959 1
    0 121054.48         0         0         0         0  40.35149 1959 1
    0 129124.78         0         0         0         0         0 1959 1
    0 143104.61  22658.23 100113.65         0         0  32039.53 1960 1
    0  198756.4         0         0         0         0 1383.3445 1960 1
    0         0  381.6123         0         0         0  12378.55 1960 1
    0 155577.34         0         0         0         0 13224.074 1962 1
    0  67156.05         0         0         0  178141.1  2238.535 1965 2
    0  59694.27         0         0         0         0  910.3375 1965 1
    0  52232.48         0         0         0         0 134.31209 1965 1
    0         0         0  6764.124         0         0         0 1965 1
    1         0         0         0         0         0         0 1967 2
    0  67641.24  5580.402         0         0         0  67641.24 1968 1
    0  51333.45         0  64166.82 30492.057         0 1283.3363 1969 1
    0  363567.5         0         0 11876.108         0 12240.105 1970 1
    0 130772.22         0         0         0         0 11.624197 1971 1
    0  61027.04         0         0         0         0         0 1971 2
    0         0         0         0         0  26010.72  5812.099 1971 2
    0 107523.83         0  458095.1 129449.59         0 581.20984 1971 1
    0 174362.95         0         0         0 130053.62 30222.914 1971 1
    0  97052.84         0  23292.68         0         0 14245.033 1977 1
    1         0 1198.4889         0         0         0 1925.0045 1977 1
    0 182459.34         0         0         0         0  3029.729 1977 1
    1         0         0         0         0         0         0 1983 1
    0  110159.3 151.77504         0 36516.582         0  5904.538 1983 1
    0 227662.55         0         0 14687.906         0  489.5969 1983 1
    0  99447.66         0         0  59668.59         0  238.6744 1989 1
    0   1988953  501216.2 2227627.5  49723.83 1292819.6  895028.9 1989 2
    1         0   596.686         0         0         0 198.89532 1989 1
    0 129281.95         0         0         0         0  1193.372 1989 1
    0 119337.19         0         0         0         0  75978.01 1989 2
    0 141891.83  3338.631         0         0         0  3505.563 1995 1
    0    534181  38279580 103497.57  31303008  29213024  25086994 1995 2
    0 200317.88         0         0         0  500.7947  3004.768 1995 1
    0  584260.5         0 12519.867         0         0 2170.1104 1995 1
    0 143561.14         0         0         0         0  834.6578 1995 1
    0 597662.25         0  174580.3         0         0 19125.191 1998 1
    0  471838.6 157.27954         0         0  10223.17  58193.43 1998 1
    0         0         0         0         0  235919.3  62911.81 1998 2
    1         0         0         0         0         0  4718.386 1998 1
    0 324952.25         0         0         0         0  26357.24 2001 1
    0  722116.1         0         0         0  12998.09 18052.902 2001 2
    0   7221161  18601712 2166348.3         0  17330786  11594296 2001 1
    0 252740.64         0         0         0         0  48815.05 2001 2
    0  31773.11         0         0         0         0 2253.0024 2001 1
    0  61005.74 203.35246         0         0         0  27.11366 2004 1
    1  66428.47         0         0         0         0  271.1366 2004 1
    1         0         0         0         0         0 2033.5244 2004 2
    0  94897.81  94.89781         0         0         0  677.8415 2004 1
    0 246882.14         0         0         0         0         0 2007 1
    0  617205.4         0 1919508.6 100115648         0 247869.67 2007 2
    1 12344.107         0         0         0         0 12.344107 2007 1
    0  308602.7         0         0         0         0         0 2007 1
    1         0         0         0         0         0  6172.054 2007 1
    0 2098498.3         0 3394629.5         0         0 248486.88 2007 1
    0 1178248.6         0         0         0 1178248.6  5419.944 2010 1
    0  176737.3         0         0 34758.336         0  2827.797 2010 1
    0 242719.23         0         0         0         0 17496.992 2010 1
    1 212084.77         0         0         0         0 1413.8984 2010 1
    0 235649.73 11782.486 1537614.5         0         0  174380.8 2010 2
    0 131780.42 219.63403         0 1098.1702         0  1877.871 2013 1
    0  378868.7         0         0         0         0 3305.4924 2013 2
    0 274542.56         0         0         0  52712.17  85657.27 2013 2
    0  422795.5         0         0  5710.485         0  48319.49 2013 1
    0  384359.6         0         0         0         0  878536.1 2013 2
    0  82362.77         0         0         0         0  58203.02 2013 1
    0 1186023.9  12310488   8236277  42542016  37337788   2766291 2013 2
    end
    label values HH_type_elderdom HH_category
    label def HH_category 1 "children HH", modify
    label def HH_category 2 "elderly HH", modify
    What I hope to do is to estimate marginal effects for different groups in different years at different values of the house variable. For example, hypothetically I may have a regression like below:

    Code:
    regress nw_pov_no_vehi goest bnd ffabus house i.year#c.house i.year#i.HH_type_elderdom#c.house
    The I wish to generate estimates of the marginal effects. Say, I would like to do:
    Code:
    margins, over(year HH_type_elderdom) at(house = (TBD))
    I tried the following code:
    Code:
    margins, over(year HH_type_elderdom) at( (mean) house )
    But it estimates the marginal effects when house is equal to the mean in different groups in different years. In each year, the estimation is drawn at house equal to the group mean for each group. This does not satisfy my need, because I am trying to estimate the marginal effects when house is equal to the mean of one specific group. In other words, the value taken by the variable house should be different for different years, but the same for both groups. For instance, I may want house = (7900) for both groups in the year of 1990, and house = (9675) for both groups in the year of 2015, etc.


    Do you know how I may achieve this goal? Thank you in advance for your help!

    Best,
    Yangfan

  • #2
    First, there is a problem with your regression model. It includes interactions of year with HH_type and house, and there is a "main effect" for house, but not for year and not for elderdom. So it is an improper model: with an interaction term, you need to also include all of the "main" effects, as well as any lower-order interactions (of which there are none in your case.) Next, you just have to force -margins- to do this by looping over the years:

    Code:
    regress nw_pov_no_vehi goest bnd ffabus house i.year##i.HH_type_elderdom##c.house
    
    
    levelsof year, local(years)
    foreach y of local years {
        summ house if year == `y', meanonly
        local year_mean = r(mean)
        display _newline(2) as result "Results for house = mean in `y'"
        margins, over(year HH_type_elderdom) at(house = (`year_mean'))
    }
    By the way, do review -help margins- with particular attention to the difference between -margins year HH_type_elderdom- and -margins, over(year HH_type_elderdom)-. Both are legitimate, but they are different things, and -over()- is not often used. So just confirm that -over()- is actually giving you what you want before proceeding with it.

    Run with the example data, you will find that many of the results are not estimable. This is due to the fact that some combinations of year and HH_type_elderdom do not exist in the example data. Perhaps that isn't a problem in the full data.

    Thank you for using -dataex- on your very first post here. Indeed, in that respect and others, I think that your post is a model of completeness and clarity in presenting the information necessary to understand and solve your problem.

    Comment


    • #3
      Thank you Clyde! Your reply is helpful.

      However, there is one thing I still wish to ask about. I might have formulated the question ambiguously. The code given above estimates the marginal effects at the mean for each year, but the estimates are drawn at the same mean for all years. For example, in the iteration of year == 1970, the mean of house in 1970 is used for all years.

      What I hope to do is to have house= (year mean) for each year, and that changes with year. In other words, I hope to have house=(1950 mean) when year==1950, house=(1975 mean) when year==1975, etc.

      Do you know how I may do that? I noticed that in each iteration above, my desired marginal effects were actually estimated. Is it possible to simply slice one from each iteration and assemble them if later I would like to generate a plot with year on the xdimension?

      Comment


      • #4
        Sorry, I misunderstood your original request. To get what you want in #3:
        Code:
        levelsof year, local(years)
        foreach y of local years {
            summ house if year == `y', meanonly
            local year_mean = r(mean)
            display _newline(2) as result "Results for house = mean in `y'"
            margins, over(HH_type_elderdom) at(year = (`y') house = (`year_mean'))
        }
        Is it possible to simply slice one from each iteration and assemble them if later I would like to generate a plot with year on the xdimension?
        Yes. Assuming you are using version 17 or later, you can do this as follows:

        Code:
        regress nw_pov_no_vehi goest bnd ffabus house i.year##i.HH_type_elderdom##c.house
        estimates store regression
        
        frame create results int (year hh_type) float(mean_house predictive_margin)
        levelsof year, local(years)
        foreach y of local years {
            summ house if year == `y', meanonly
            local year_mean = r(mean)
            display _newline(2) as result "Results for house = mean in `y'"
            margins, over(HH_type_elderdom) at(year = (`y') house = (`year_mean')) post
            forvalues hh = 1/2 {
                frame post results (`y') (`hh') (`year_mean') (_b[`hh'.HH_type_elderdom])
            }
            estimates restore regression
        }
        
        frame change results
        label values hh_type HH_category
        label def HH_category 1 "children HH", modify
        label def HH_category 2 "elderly HH", modify
        
        graph twoway line predictive_margin year, by(hh_type) sort

        Comment


        • #5
          Thank you a lot for your help Clyde!

          Comment

          Working...
          X