Stata 中“制表双向”频率计数表的 5×5 矩阵

发布于 2024-12-25 14:59:34 字数 834 浏览 2 评论 0原文

我想创建一个 5×5 网格的 tabulate Twoway 频率计数表,如下表所示。

在此处输入图像描述

使用嵌套的 foreach 循环生成每个子表很容易,但是长列表输出比 5×5 网格更难解释(并且具有冗余条目 - 它提供对称矩阵的两半)。

Stata可以制作这样的表格吗?需要明确的是,我稍后可以弄清楚 LaTeX,我只是对获得清晰简洁的控制台输出感兴趣。

谢谢!下面是一些使用 auto 数据执行基础操作的代码,但生成一个列表而不是矩阵。 xtile 来自 egenmore

sysuse auto, clear
global vars price mpg headroom trunk weight
foreach x of global vars {
    egen d_`x' = xtile(`x'), nquantiles(2)
}

* can make diagonal entries
tabulate d_price d_price

* can make off-diagonal entries
tabulate d_price d_mpg

* crude solution that generates list output with redundant entries
foreach x of global vars {
    foreach y of global vars {
        tabulate d_`x' d_`y'
    }
}

I would like to create a 5-by-5 grid of tabulate twoway frequency count tables, like the following table.

enter image description here

Generating each sub-table is easy with nested foreach loops, but the long list output is more difficult to interpret than a 5-by-5 grid (and has redundant entries -- it provides both halves of the symmetric matrix).

Is it possible to make a table like this in Stata? To be clear, I can figure out the LaTeX later, I am just interested in getting clear and concise console output.

Thanks! Here is some code that does the basics using the auto data, but generates a list instead of a matrix. xtile is from the egenmore package

sysuse auto, clear
global vars price mpg headroom trunk weight
foreach x of global vars {
    egen d_`x' = xtile(`x'), nquantiles(2)
}

* can make diagonal entries
tabulate d_price d_price

* can make off-diagonal entries
tabulate d_price d_mpg

* crude solution that generates list output with redundant entries
foreach x of global vars {
    foreach y of global vars {
        tabulate d_`x' d_`y'
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾其所爱 2025-01-01 14:59:34

我在循环中添加了一些矩阵运算。

tempname col all tabout
foreach x of global vars {
    foreach y of global vars {
        qui tabulate d_`x' d_`y', matcell(`tabout')
        mat colnames `tabout' = `x' `x'
        mat rownames `tabout' = `y' `y'
        mat `col' = (nullmat(`col') \ `tabout' )
    }
    mat `all'= (nullmat(`all') , `col')
    mat drop `col'
}
mat list `all'

它有点类似于我的名为 meantab 的程序,位于 http:// /code.google.com/p/kk-adofiles/

I've added some matrix operations into your loop.

tempname col all tabout
foreach x of global vars {
    foreach y of global vars {
        qui tabulate d_`x' d_`y', matcell(`tabout')
        mat colnames `tabout' = `x' `x'
        mat rownames `tabout' = `y' `y'
        mat `col' = (nullmat(`col') \ `tabout' )
    }
    mat `all'= (nullmat(`all') , `col')
    mat drop `col'
}
mat list `all'

It is a little similar to my program named meantab, at http://code.google.com/p/kk-adofiles/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文