matrix sm = (0,1,2,3,3 \ /// 1,0,1,2,2 \ /// 2,1,0,1,1 \ /// 3,2,1,0,0 \ /// 3,2,1,0,0) use http://teaching.sociology.ul.ie/oslo/bhps18dyads // Assume in pairs of rows, male first then female gen refpid = pid replace refpid = pid[_n-1] if mod(_n,2)==0 gen sex = 2 - mod(_n,2) gen jbmiss = regexm(jb, "\.") sort refpid sex by refpid: egen prob = max(jbmiss) drop if prob drop prob jbmiss // Verify that your data is properly composed of pairs by refpid: assert sex[1]==1 by refpid: assert sex[2]==2 by refpid: assert _N==2 // Sort into blocks such that spouse pair is (i,i+_N/2) sort sex refpid oma eun*, subsmat(sm) indel(2) pwd(dyads) len(18) standard(none) mata: d = st_matrix("dyads") d1 = d[1..rows(d)/2,rows(d)/2+1..rows(d)] d2 = d[rows(d)/2+1..rows(d),1..rows(d)/2] dd1 = diagonal(d1) dd2 = diagonal(d2) dm1 = mean(d1)' dm2 = mean(d2)' dse1 = J(rows(d)/2, 1, .) dse2 = J(rows(d)/2, 1, .) for (i=1; i<= rows(d)/2; i++) { dse1[i] = sqrt(variance(d1[,i])) :/ sqrt(rows(dse1)-1) dse2[i] = sqrt(variance(d2[,i])) :/ sqrt(rows(dse2)-1) } // How many opposite-sex sequences are further than the partner sequence? d1g = J(rows(dd1),1,.) d2g = J(rows(dd2),1,.) for (i=1; i<=rows(dd1); i++) { d1g[i] = rowsum(d1[i,.] :> dd1[i]) d2g[i] = rowsum(d2[i,.] :> dd2[i]) } dd = dd1 \ dd2 dm = dm1 \ dm2 dse = dse1 \ dse2 dg = d1g \ d2g dd = dd, dm, dse, dg newvars = st_addvar("double", ("dist", "dmean", "dse", "dg")) st_store(., newvars, dd) end gen zstat = (dist-dmean)/dse count if abs(zstat)>2 /* The z-stat is the (distance - mean distance)/se for an individual to his/her partner versus mean distance to each case of the opposite category. That tests whether the distance is different from the mean, but possibly with dubious statistical logic. dg is the number of cases where distance to others is greater than the distance to partner. */