2013-11-27 22 views
0

我有一个数据库(我使用Stata 13),它具有复杂样本设计(Strate和Pweight)的多个插补,所以我通常在使用以下命令之前我的分析:mi estimate, esampvaryok:svy:使用多重插值和复杂样本设计的多边形关联(Stata)

我只是想知道有没有什么方法可以在Stata中使用polychoric命令?或者,如果不可能,你是否知道其他允许我这样做的软件?

+0

这是很好的做法,是在哪里的命令来自若而不是来自Stata的基础安装。 'polychoric'是一个用户编写的命令,可以运行'findit polychoric'。 –

回答

1

polychoric应用于每个插补数据集,然后对结果取平均值。虽然polychoric不是调查意识,但只需要概率权重来估计相关性。以下是计算两个相关性估计值的代码:1)来自polychoric的各个相关性的平均值; 2)基于这些相关的平均逆双曲正切变换的估计。在这个例子中他们是相似的,但我通常更喜欢后者。请参阅:尼克考克斯,斯塔塔演讲:与信心的相关性,或费舍尔z再访。 Stata期刊(2008)8,第3期,第413-439页。 http://www.stata-journal.com/article.html?article=pr0041

更新:输出的相关性,以Stata的矩阵和添加的行&列名

/* Create MI data set */ 
set seed 43228226 
sysuse auto, clear 
recode rep78 1/2=3 
replace foreign = . in 3/5 
mi set flong 
mi register impute rep78 foreign 
mi impute chained /// 
(ologit) rep78 /// 
(logit) foreign = turn weight mpg /// 
[pw = turn], add(5) double 

/* Set up polychoric */ 

/* local macro with variables */ 
local pvars rep78 foreign mpg weight 

local nvars : word count("`pvars'") 
mata: nv = strtoreal(st_local("nvars")) 

qui mi des 
mata: nreps = st_numscalar("r(M)") 

/* loop through MI data sets to get sums*/ 
mata: sum_r = J(nv,nv,0) 
mata: sum_atr = J(nv,nv,0) 

forvalues i = 1/`=r(M)'{ 
qui polychoric `pvars' [pw = turn] if _mi_m==`i' 
mata: r = st_matrix("r(R)") 
mata: sum_r = sum_r + r 
mata: sum_atr = sum_atr +atanh(r) 
} 
/* Now average and get estimates */ 
mata: 
st_matrix("rho1",sum_r/nreps) 
/* For version based on atanh: 
1) Get average of atanh transforms. 
2) Diagonal elements are missing; substitute 0s. 
3) Back transform to correlation scale. 
4) Put 1s on diagonal. 
5) Make the matrix symmetric. */ 
st_matrix("rho2", /// 
makesymmetric(tanh(lowertriangle(sum_atr/nreps,0))) +I(nv)) 
end 


/* rho1 : average correlations 
    rho2 : back transform of average atanh(r) */ 
forvalues i = 1/2 { 
mat colnames rho`i' = `pvars' 
mat rownames rho`i' = `pvars' 
mat list rho`i' 
} 

更新2:晴马塔

/* Set up variables & pweight */ 
local pcvars rep78 foreign mpg weight 
local pwtvar turn 

mata: 
stata("qui mi query") 
M= st_numscalar("r(M)") 

vnames = tokens(st_local("pcvars")) 
nv = cols(vnames) 

/*Initialize sums for average numerators */ 
sum_r = J(nv,nv,0) 
sum_atr = J(nv,nv,0) 

/* Run -polychoric- on each imputed data set */ 
for (j = 1; j<=M; j++) { 
    st_numscalar("j",j) 
    stata("qui polychoric `pcvars' [pw = `pwtvar'] if _mi_m==j") 
    r = st_matrix("r(R)") 
    sum_r = sum_r + r 
    sum_atr = sum_atr + atanh(r) 
} 
/* Create Stata correlation matrices from average over imputations*/ 
st_matrix("rho1",sum_r/M) 
st_matrix("rho2", /// 
    makesymmetric(tanh(lowertriangle(sum_atr/M,0))) +I(nv)) 

/* Label rows & columns */ 
c = (J(nv,1,""),vnames') 
st_matrixrowstripe("rho1",c) 
st_matrixcolstripe("rho1",c) 

st_matrixrowstripe("rho2",c) 
st_matrixcolstripe("rho2",c) 
end 

mat list rho1 
mat list rho2 
+0

感谢您使用这种语法! – user2388178

+0

非常欢迎。如果你喜欢这个答案,请接受它。我添加了分析代码的替代版本。你指定'polychoric'的变量和顶部的概率权重。这个版本可能比原来的版本快一点,因为它实际上都在Mata中,除了'mi query'(替代mi des)和'polychoric'之外。 –