Search code examples
statasurvey

Use pweight with confidence intervals and store in a matrix


I am calculating a lot of means on survey-data, where I have to use proportional weights.

Unfortunately, weights are not allowed with the command ci. I could use the mean command but I need to have the results stored, so I subsequently can put them into a matrix.

My loop-code looks like this:

local i = 1
foreach var of varlist hinst1 - hinst34 {
  ci varname if `var'==1  
  mat matname[`i',1]=r(mean)
  mat matname[`i',1]=r(lb)  
  mat matname[`i',1]=r(ub)  
  local ++i

}

It is possible to use aweights but I need pweights.


Solution

  • Using Stata's nhanes2f toy dataset as an example:

    webuse nhanes2f, clear
    svyset psuid [pweight=finalwgt], strata(stratid)
    

    The svy prefix can be used as follows:

    matrix A = J(3,3,.)
    
    local i = 1
    
    foreach var of varlist sex race region {
      svy: mean age if `var'==1 
      matrix res = r(table)
      matrix A[1,`i'] = res[1, 1]
      matrix A[2,`i'] = res[5, 1]
      matrix A[3,`i'] = res[6, 1]  
      local ++i
    }
    
    matrix list A
    
    A[3,3]
               c1         c2         c3
    r1  41.898222  42.508557  43.133186
    r2  41.206209  41.852839  42.034576
    r3  42.590235  43.164274  44.231796