Dear statalisters,

I'm trying to replicate the results of the oaxaca9 command manually, but I have problems calculating the standard deviations since in no case do they give me the same as the direct command. All this prior to the classic Heckman corrections. I think my mistake is that the way he calculates the variances oaxaca9 is not the same as the one I do, but if that's the problem I do not know how to calculate the variances oaxaca.

My command of oaxaca9 is:
Code:
oaxaca9 lwage_h $controles2 if lwage_h>0,  by(female) svy

On the other hand, the way in which I estimate the variance of salary differences and the coefficients of Oaxaca is
Code:
mat K = (X1' * B1_var * X1) + (B1' * var1 * B1) + trace(var1 * B1_var)   + (X2' * B2_var * X2) + (B2' * var2 * B2) + trace(var2 * B2_var)
matrix oax[2,1] = sqrt(K[1,1])
matrix oax[3,1] = oax[1,1]/oax[2,1]

matrix L =  (X1-X2)' * B1_var * (X1-X2)+ B1' * (var1+var2) * B1 
matrix oax[2,2] = sqrt(L[1,1])
matrix oax[3,2] = oax[1,2]/oax[2,2]

matrix M = X2' * (B1_var+B2_var)*X2  +  (B1-B2)'*var2*(B1-B2) 
matrix oax[2,3] = sqrt(M[1,1])
matrix oax[3,3] = oax[1,3]/oax[2,3]

matlist oax

where, B and B_var were obtained from:
Code:
global malelist "0 1"

foreach sexo in $malelist {
svy: reg lwage_h $controles2 if female==`sexo' & lwage_h>0 
                if `sexo'==0 {
                gen used = e(sample)==1
                matrix B1 = e(b) 
                matrix B1_var = e(V)  //matriz de varianzas y covarianzas
                predict yhat1 if used==1 & female==0, xb
                }
                if `sexo'==1 {
                replace used=1 if e(sample)==1
                matrix B2 = e(b) 
                matrix B2_var = e(V) //matriz de varianzas y covarianzas
                predict yhat2 if used==1 & female==1, xb
                }
}
So, X and X_var:

Code:
global controles3 "educ exper exper2 rural publico used" // used at the end for the vector of 1 of the variables matrix
local nvar = 6
matrix X=J(`nvar',4,0) 

qui forval i=0/1 {
local a=1
foreach v in $controles3 {
local j=`i'+1
svy: mean `v' if female==`i'
        mat x_`a'_`j'=e(b)
        mat X[`a',`j']= x_`a'_`j'[1,1]
        
        mat v_`a'_`j'=e(V)
        mat X[`a',`j'+2]= v_`a'_`j'[1,1]
        
        local ++a
        }
        }
        mat X = X'
        matrix rownames X = hombre mujer hombre_var mujer_var 
        matrix colnames X = $controles3
//


matrix X1 = J(`nvar',1,0)
matrix X2 = J(`nvar',1,0)
matrix X1_v = J(`nvar',1,0)
matrix X2_v = J(`nvar',1,0)
qui forval i=1/6 {  
mat X1[`i',1] = X[1,`i']
mat X2[`i',1] = X[2,`i']
mat X1_v[`i',1] = X[3,`i']
mat X2_v[`i',1] = X[4,`i']
}


Attached results of the command from oaxaca command and my matrix with the results of my command.

thank you very much in advance for your help dear community