0

I am estimating a model in Stata 16 over several subsamples. I want a chart comparing two coefficients of interest over the different subsamples, with axis labels showing which subsample it comes from.

Is there a way to combine both of these on the same panel, with the mileage estimates in one colour and the trunk space in another?

The closest I can get using coefplot is a tiled plot with a set of coefficients of one variable in one panel, and the coefficients for the other variable in another panel (see toy example below). Any idea how to get both on the same panel?

webuse auto

forval v=2/5 {
    reg price trunk mpg if rep78==`v'
    est store reg_`v'
}

coefplot reg_2 || reg_3 || reg_4 || reg_5, keep(trunk mpg) bycoefs vertical 
Nick Cox
  • 35,529
  • 6
  • 31
  • 47

1 Answers1

0

There's likely a more elegant way to do this with coefplot, but until someone posts that solution: you can use matrices to brute force coefplot into behaving the way you'd like. Specifically, define as many matrices as you have unique covariates, with each matrix's dimension being #specs x 3. Each row will contain the covariate's estimated coefficient, lower CI, and upper CI for a particular model specification.

This works because coefplot assigns the same color to all quantities associated with plot (as defined by coefplot's help file). plot is usually a stored model from estimates store, but by using the matrix trick, we've shifted plot to be equivalent to a specific covariate, giving us the same color for a covariate across all the model specifications. coefplot then looks to the matrix's rows to find its "categorical" information for the labeled axis. In this case, our matrix's rows correspond to a stored model, giving us the specification for our axis labels.

// (With macros for the specification names + # of coefficient
//  matrices, for generalizability)
clear *
webuse auto

// Declare model's covariates
local covariates trunk mpg

// Estimate the various model specifs
local specNm = ""   // holder for gph axis labels
forval v=2/5 {
    // Estimate the model
    reg price `covariates' if rep78==`v'
    
    // Store specification's name, for gph axis labels
    local specNm = "`specNm' reg_`v'"
    
    // For each covariate, pull its coefficient + CIs for this model, then
    // append that row vector to a new matrix containing that covariate's
    // b + CIs across all specifications
    matrix temp = r(table) 
    foreach x of local covariates{
        matrix `x' = nullmat(`x') \ (temp["b","`x'"], temp["ll","`x'"], temp["ul","`x'"])
    }
}

// Store the list of 'new' covariate matrices, along w/the
// column within this matrix containing the coefficients
global coefGphList = ""
foreach x of local covariates{
    matrix rownames `x' = `specNm'
    global coefGphList = "$coefGphList matrix(`x'[,1])"
}

// Plot
coefplot $coefGphList, ci((2 3)) vertical
SMzgr356
  • 83
  • 6