0

我正在尝试修改@Nick Cox 在我之前的问题中发布的代码中发布的代码,但我遇到了一些问题。

我已经设置了我varlist和我的组变量。我还更改了col选项以适合我的varname. 我想添加每组的观察次数r(N 1)/r(N 2)并在结果列表中添加一些“标题”。

我正在尝试研究该display命令,但我无法找到解决方案。

我的代码如下:

foreach v of var BVCAlogMAR AvgSSI L1DensityWholeImage { 
    quietly ranksum `v', by(G6PDcarente) porder 
    scalar pval = 2*normprob(-abs(r(z)))
    di "`v'{col 34}" %05.3f pval " " %6.4e pval  "   " %05.3f r(porder) ///
    "   " %05.3f r(N 1)  "   " %05.3f r(N 2)
} 

我无法将 和 的值放入结果列表r(N 1)r(N 2)。此外,我不知道如何显示具有以下标题的列标题:

P 值,PValue2,Porder-观察。第 1 组 - 观察。第 2 组

你能帮我么?

4

1 回答 1

1

You are incorrectly referring to r(N_1) and r(N_2) as r(N 1) and r(N 2) respectively.

In the toy example provided in your previous post, i have corrected this mistake and i inserted six additional lines in bold, which achieve what you want:

sysuse auto, clear 

local i = 0

foreach v of var mpg price weight length displacement { 
    local ++i
    quietly ranksum `v', by(foreign) porder 
    scalar pval = 2*normprob(-abs(r(z)))
    
    if `i' == 1 {
      display %20s "P-Value", %5s "PValue2", %5s "Porder1", %5s "group 1", %5s "group 2"
      display ""
    }

    display "`v'{col 14}" %05.3f pval " " %6.4e pval  "   " %05.3f r(porder)  ///
    "   " %05.3f r(N_1)  "   " %05.3f r(N_2)
} 

The first display command acts as a header, but you will have to play with the values to get the desired spacing. The second merely adds a blank line.

The counter macro i serves to display the header and blank lines only in the first step of the for loop instead of them repeating for each variable.

The results are illustrated below:

             P-Value PValue2 Porder1 group 1 group 2

mpg          0.002  1.9e-03   0.271   52.000   22.000
price        0.298  3.0e-01   0.423   52.000   22.000
weight       0.000  3.8e-07   0.875   52.000   22.000
length       0.000  9.4e-07   0.862   52.000   22.000
displacement 0.000  1.1e-08   0.921   52.000   22.000

For more information about output formatting using the display command, type help format in Stata's command prompt.

于 2018-05-14T22:13:26.090 回答