Search code examples
latexstata

Append three regression tables into separate panels


Consider the following toy data:

input strL Country Population Median_Age Sex_Ratio GDP Trade year
"United States of America" 3999 55 1.01 5000 13.1 2012
"United States of America" 6789 43 1.03 7689 7.6 2013
"United States of America" 9654 39 1.00 7689 4.04 2014
"Afghanistan" 544 24 0.76 457 -0.73 2012
"Afghanistan" 720 19 0.90 465 -0.76 2013
"Afghanistan" 941 17 0.92 498 -0.81 2014
"China" 7546 44 1.01 2000 10.2 2012
"China" 10000 40 0.96 3400 14.3 2013
"China" 12000 38 0.90 5900  16.1 2014
"Canada" 7546 44 1.01 2000 1.2 2012
"Canada" 10000 40 0.96 3400 3.1 2013
"Canada" 12000 38 0.90 5900  8.5 2014
end

I run different regressions (using three different independent variables):

*reg1

local var "GDP Trade"
foreach ii of local var{
qui reg `ii' Population i.year    
est table, b p
outreg2 Population using table, drop(i.year*) bdec(3) sdec(3) nocons tex(nopretty) append
}

*reg2

local var "GDP Trade"
foreach ii of local var{
qui reg `ii' Median_Age i.year    
est table, b p
outreg2 Population using table2, drop(i.year*) bdec(3) sdec(3) nocons tex(nopretty) append
}

*reg3

local var "GDP Trade"
foreach ii of local var{
qui reg `ii' Sex_Ratio i.year    
est table, b p
outreg2 Population using table3, drop(i.year*) bdec(3) sdec(3) nocons tex(nopretty) append
}

I use the append option to append different dependent variables that are to be regressed on the same set of independent variables. Hence, I obtain three different tables.

I wish to "merge" these tables when I compile in LaTeX, so that they appear as a single table, with three different panels, one below the other.

Table1
Table2
Table3

I can use the tex(frag) option of the community-contributed command outreg2, but that will not give me the desired outcome.


Solution

  • Here is a simple way of doing this, using the community-contributed command esttab:

    clear
    
    input strL Country Population Median_Age Sex_Ratio GDP Trade year
    "United States of America" 3999 55 1.01 5000 13.1 2012
    "United States of America" 6789 43 1.03 7689 7.6 2013
    "United States of America" 9654 39 1.00 7689 4.04 2014
    "Afghanistan" 544 24 0.76 457 -0.73 2012
    "Afghanistan" 720 19 0.90 465 -0.76 2013
    "Afghanistan" 941 17 0.92 498 -0.81 2014
    "China" 7546 44 1.01 2000 10.2 2012
    "China" 10000 40 0.96 3400 14.3 2013
    "China" 12000 38 0.90 5900  16.1 2014
    "Canada" 7546 44 1.01 2000 1.2 2012
    "Canada" 10000 40 0.96 3400 3.1 2013
    "Canada" 12000 38 0.90 5900  8.5 2014
    end
    
    local var "GDP Trade"
    foreach ii of local var{
        regress `ii' Population i.year 
        matrix I = e(b)
        matrix A = nullmat(A) \ I[1,1]
        local namesA `namesA' Population_`ii'
    }
    
    matrix rownames A = `namesA'
    
    local var "GDP Trade"
    foreach ii of local var{
        regress `ii' Median_Age i.year 
        matrix I = e(b)
        matrix B = nullmat(B) \ I[1,1]
        local namesB `namesB' Median_Age_`ii'
    }
    
    matrix rownames B = `namesB'
    
    local var "GDP Trade"
    foreach ii of local var{
        regress `ii' Sex_Ratio i.year 
        matrix I = e(b)
        matrix C = nullmat(C) \ I[1,1]
        local namesC `namesC' Sex_Ratio_`ii'
    }
    
    matrix rownames C = `namesC'
    
    matrix D = A \ B \ C
    

    Results:

    esttab matrix(D), refcat(Population_GDP "Panel 1" ///
                             Median_Age_GDP "Panel 2" ///
                             Sex_Ratio_GDP "Panel 3", nolabel) ///
                             gaps noobs nomtitles ///
                             varwidth(20) ///
                             title(Table 1. Results)
    
    Table 1. Results
    ---------------------------------
                                   c1
    ---------------------------------
    Panel 1                          
    
    Population_GDP           .3741343
    
    Population_Trade         .0009904
    
    Panel 2                          
    
    Median_Age_GDP           202.1038
    
    Median_Age_Trade          .429315
    
    Panel 3                          
    
    Sex_Ratio_GDP            18165.85
    
    Sex_Ratio_Trade            27.965
    ---------------------------------
    

    Using the tex option:

    \begin{table}[htbp]\centering
    \caption{Table 1. Results}
    \begin{tabular}{l*{1}{c}}
    \hline\hline
                        &          c1\\
    \hline
    Panel 1             &            \\
    [1em]
    Population\_GDP      &    .3741343\\
    [1em]
    Population\_Trade    &    .0009904\\
    [1em]
    Panel 2             &            \\
    [1em]
    Median\_Age\_GDP      &    202.1038\\
    [1em]
    Median\_Age\_Trade    &     .429315\\
    [1em]
    Panel 3             &            \\
    [1em]
    Sex\_Ratio\_GDP       &    18165.85\\
    [1em]
    Sex\_Ratio\_Trade     &      27.965\\
    \hline\hline
    \end{tabular}
    \end{table}
    

    EDIT:

    This preserves the original format:

    local var "GDP Trade"
    foreach ii of local var{
        regress `ii' Population i.year 
        matrix I = e(b)
        matrix A = (nullmat(A) , I[1,1])
        local namesA `namesA' `ii'
    }
    
    matrix rownames A = Population
    matrix colnames A = `namesA'
    
    local var "GDP Trade"
    foreach ii of local var{
        regress `ii' Median_Age i.year 
        matrix I = e(b)
        matrix B = nullmat(B) , I[1,1]
        local namesB `namesB' `ii'
    }
    
    matrix rownames B = "Median Age"
    matrix colnames B = `namesB'
    
    local var "GDP Trade"
    foreach ii of local var{
        regress `ii' Sex_Ratio i.year 
        matrix I = e(b)
        matrix C = nullmat(C) , I[1,1]
        local namesC `namesC' `ii'
    }
    
    matrix rownames C = "Sex Ratio"
    matrix colnames C = `namesC'
    
    matrix D = A \ B \ C
    

    Table 1. Results
    --------------------------------------
                          GDP        Trade
    --------------------------------------
    Population       .3741343     .0009904
    Median Age       202.1038      .429315
    Sex Ratio        18165.85       27.965
    --------------------------------------