我有一个数据库(我使用Stata 13),它有多个插补和复杂的样本设计(Strate和Pweight),所以我通常在分析之前使用以下命令:mi estimate, esampvaryok:svy:
我只是想知道在这种情况下有没有办法在Stata中使用polychoric
命令?或者,如果不可能,您是否知道允许我这样做的其他软件?
答案 0 :(得分:1)
将polychoric
应用于每个插补数据集,然后对结果进行平均。虽然polychoric
不是调查感知的,但只需要概率权重来估计相关性。这是用于计算两个相关性估计值的代码:1)来自polychoric
的各个相关性的平均值; 2)基于那些相关性的平均反双曲正切变换的估计。在示例中它们是相似的,但我通常更喜欢后者。请参阅:Nick Cox,Speaking Stata:与置信度相关,或者重新审视Fisher's。 The Stata Journal(2008)8,Number 3,pp.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:主要是Mata
/* 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