【问题标题】:How do I get two coefficients from a set of regressions plotted on the same chart?如何从绘制在同一图表上的一组回归中获得两个系数?
【发布时间】:2023-01-05 03:33:43
【问题描述】:

我正在通过几个子样本估计 Stata 16 中的模型。我想要一个图表来比较不同子样本的两个感兴趣系数,轴标签显示它来自哪个子样本。

有没有办法将这两者结合在同一个面板上,一种颜色的里程估计和另一种颜色的后备箱空间?

我可以使用 coefplot 得到的最接近的是一个平铺图,其中一个面板中的一个变量的一组系数,以及另一个面板中另一个变量的系数(参见下面的玩具示例)。知道如何将两者放在同一个面板上吗?

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 

【问题讨论】:

    标签: stata coefplot


    【解决方案1】:

    coefplot 可能有更优雅的方法来执行此操作,但在有人发布该解决方案之前:您可以使用矩阵强制 coefplot 以您想要的方式行事。具体来说,定义与唯一协变量一样多的矩阵,每个矩阵的维度是#眼镜x 3. 每行将包含特定模型规格的协变量估计系数、较低 CI 和较高 CI。

    这是有效的,因为 coefplot 将相同的颜色分配给与关联的所有数量plot(由coefplot 的帮助文件定义)。plot通常是来自estimates store 的存储模型,但通过使用矩阵技巧,我们已经转移plot等效于特定的协变量,为所有模型规格的协变量提供相同的颜色。 coefplot 然后查看矩阵的行以找到标记轴的“分类”信息。在这种情况下,我们矩阵的行对应于一个存储的模型,为我们提供轴标签的规范。

    // (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
    

    【讨论】:

      猜你喜欢
      • 2021-07-18
      • 2016-07-01
      • 1970-01-01
      • 2020-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多