Stata制表用零

时间:2013-10-29 06:37:43

标签: tabs stata

我遇到了Stata和命令的问题:

svy: tab x

如果没有给定x的观察结果。

我的问题是,当没有对类别的观察时,Stata只会丢弃相应的行。

我的任务是运行多个表格并保存密钥结果并将其导出到csv文件。有时存储的向量有n个元素,有时因为零只有n-1个元素所以我不知道如何将它们组合成一个更大的矩阵(或者至少将它们导出到一个文件中,行之间有规则的间距和如果没有观察,则值为0)。 我也试过了

estpost svy, subpop(x0): tab x, count se format(%10.4g)

但我仍有同样的问题。

1 个答案:

答案 0 :(得分:1)

更新3 此解决方案基于estpost svy: tab,因为该命令返回比svy: tab本身更多的可用结果向量。与之前的版本一样,此解决方案将所有这些结果放入Stata数据集中。它会在求助于循环之前检查数据是否包含缺少的类别,并稍微收紧循环限制。根据尼克的建议,缺失值将替代所有与标准错误相关的统计信息。注意

 estpost svy: tab rep78

默认情况下,将估算的单元格比例放入e(b),将标准错误放入e(se),而

 estpost svy: tab rep78, count

将估计的计数及其SE放入这些矩阵中。但是,其他摘要仍可在e(cell)e(count)中使用。

 sysuse auto, clear
 drop if rep78==2 |rep78==5

 svyset _n [pw = turn]
 estpost svy: tab rep78,  se

 /* Number categories from 1 to max */
 local maxcat = 5
 mata:
 /* count rows, add one for totals row
   assign the category for that row as .a */
 r = (st_matrix("e(Row)"), .a)'
 b = st_matrix("e(b)")'
 serr = st_matrix("e(se)")'
 lb = st_matrix("e(lb)")'
 ub = st_matrix("e(ub)")'
 def = st_matrix("e(deff)")'
 dft = st_matrix("e(deft)")'
 ct = st_matrix("e(count)")'
 pr = st_matrix("e(cell)")'
 obs = st_matrix("e(obs)")'
 d1 =(r , b, serr, lb, ub, def, dft, obs, pr, ct)

 /*  Where there are no totals, use a standard missing value */
 d1[rows(d1),3::7] = J(1,5, .)

 /* Check if there are no missing rows.
 If so, output the original returned matrices */
 if (`e(r)' ==`maxcat') d = d1
 /* Else create a zero matrix and populate it
 with statistics for the non-missing categories*/
 else {   
     d2= J(`maxcat',10,0)
     d2[.,1] =(1::`maxcat')
     for (j = 1; j<=`e(r)'; j++) {
        for (k = 1; k<=r[j,1]; k++) {
           if (r[j,1]== k) {
                   d2[k,2] = b[j,1]
                   d2[k,3] = serr[j,1]
                   d2[k,4] = lb[j,1]
                   d2[k,5] = ub[j,1]
                   d2[k,6] = def[j,1]
                   d2[k,7] = dft[j,1]
                   d2[k,8] = obs[j,1]
                   d2[k,9] = pr[j,1]
                   d2[k,10] = ct[j,1]
            }
        }
    }
 /* If rows are missing set SE-realated stats to missing*/
   for (k = 1; k<=`maxcat'; k++) {
      if (d2[k,2] == 0)  d2[k,3..7] =J(1,5,.)
    }
 /* Now add the totals row */
 d = d2 \ d1[rows(d1),.]
 }
 end
 clear
 getmata (rep78 b se lb ub deff deft nobs prop count ) = d
 format  b se lb ub deff deft prop  %5.2f
 format nobs count %10.0gc
 label define rtot  .a "Totals"
 label values rep78 rtot
 list
 save results, replace

原始答案以下是一种创建包含零类别的矩阵new的方法。逻辑:设置一个零矩阵来保存所有类别的结果;然后使用非缺失类别中的值替换零。宏maxcat包含表格变量的最大类别数。该代码假定tabled变量中的类别是从1到maxcat的整数。 mata块提取标准错误的向量,标量e(r)保存实际表中的行数。

 sysuse auto, clear
 svyset _n
 drop if rep78== 2 | rep78==5
 svy: tab rep78, count se

 local  maxcat = 5  //max no. of categories
 matrix  oldr = e(Row)'   // category values
 matrix ct = e(Obs)  // table counts

 // serr is a vector of std. errors
 mata: st_matrix("serr", sqrt(diagonal(st_matrix("e(V)"))))

 // matrix new  will hold the expanded results
 matrix new = J(`maxcat', 3, 0)

 forvalues j = 1/`=e(r)' {
 forvalues k = 1/`maxcat'{
 matrix new[`k',1] = `k'
 if oldr[`j',1]== `k'  {
 matrix new[`k',2] = ct[`j',1]
 matrix new[`k',3] = serr[`j',1]
 }
 }
 }
 matrix list new

更新2 :这是一个完成Mata大部分工作的版本,然后将估算值保存到Stata数据集中。我稍微更改了矩阵的名称。

 sysuse auto, clear
 svyset _n
 drop if rep78== 2 | rep78==5
 svy: tab rep78, count se
 local maxcat =5

 mata:
 r = st_matrix("e(Row)")'
 ct = st_matrix("e(Obs)")
 serr= sqrt(diagonal(st_matrix("e(V)")))
 d = J(`maxcat',3,0)
 for (j = 1; j<=`e(r)'; j++) {
     for (k = 1; k<=`maxcat'; k++) {
         d[k,1] = k
         if (r[j,1]== k) {
            d[k,2] =   ct[j,1]
            d[k,3] = serr[j,1]
         }
    }
  }
 end
 clear
 getmata (rep78 count se) = d
 replace se = . if count==0
 format se %8.2f
 list
 save results, replace
相关问题