链接对等方:替换2个链路对等方的多个列表

时间:2016-06-28 01:12:15

标签: variables stata

我有数据为受访者指定的朋友提供标识符。我试图将2个链接的同伴(不是受访者的朋友,而是受访者的朋友的朋友)联系起来,这样我就可以使用2个链接的同伴特征作为同伴结果和特征影响的工具。被访者。

我已成功链接了2个链路对等体的标识符,但我需要将这2个链路对等体的重复条目替换为缺失,只留下每个2链路对等体标识符一次。也许这比我做到的要容易得多 - 我不是最聪明也不是最聪明的 - 但到目前为止我都失败了。下面我提供了一个标识符的玩具数据集,以及我尝试过的工作步骤以及我被卡住的地方。

我提供了我一直在使用的整个设置,但它是我被卡住的代码中的第4点。这个玩具示例仅列出了2位男性和2位女性朋友,但真实数据中列出了最多5位男性和5位女性朋友。如果有人有关于如何使整个方法更加通用和直接的提示,我将不胜感激。

在数据中,aid是人员标识符; mf1aid是男性朋友1,依此类推。我需要将不是aid的朋友的朋友的朋友链接到aid。有些早期步骤需要vlookup计划。

这些部分可行,但可能效率低下。

clear all
clear mata
set more off





input aid  mf1aid mf2aid ff1aid ff2aid  
      101   102     103     106    .    
      102   101     104     106    .    
      103   101     104     107   108   
      104   105     102     108   109   
      105   104     101     106   110   
      106   101     102     107   108   
      107   103      .      106   110   
      108   103     104     109   108   
      109   104     101     108   110   
      110   105     104     109   107   

end 

/* The above data is setup so that some aid's name friends who reciprocate and some
name friends who do not reciprocate the link (as in the real data).  */




/* Need to link aid's of those 2 links away from each aid 

 It must do the following:
    1. Link the friend `aid`s of each person's friends
    2. Delete the friend of friends `aid`s that are also the person's 1 link friends
    3. Delete extra counts of 2 link `aid`s that may occur if a person's friends
        are all linked to a k `aid` that is not linked to i.        
    4. Delete self from friend of friend links. */




// Duplicating friend links because `vlookup.ado` would not link friends of friends id
forvalues i = 1(1)2 {

    gen mf`i'aid2 = mf`i'aid
    gen ff`i'aid2 = ff`i'aid

}   


/* 
Strategy: 1. use vlookup to attach the friend ids of each aid's friends,
          so for mf1aid it attaches that friend's mf1aid2, mf2aid2, ff1aid2, and ff2aid2
          2. delete self links in 2 link set
          3. delete own friends who are in 2 link set
          4. drop multiple listings in 2 link set to just 1 listing         

Mf_mf = "male friend male friend" so the male friends of i's male friends
Mf_ff = "male friend female friend"
Ff_mf = "female friend male friend"
Ff_ff = "female friend female friend"
*/  


// 1. Using vlookup.ado to link friends friend aids 
forvalues i = 1(1)2 {

    vlookup mf1aid, gen(Mf1_mf`i'aid) key(aid) value(mf`i'aid2)
    vlookup mf1aid, gen(Mf1_ff`i'aid) key(aid) value(ff`i'aid2)

    vlookup mf2aid, gen(Mf2_mf`i'aid) key(aid) value(mf`i'aid2)
    vlookup mf2aid, gen(Mf2_ff`i'aid) key(aid) value(ff`i'aid2)

    vlookup ff1aid, gen(Ff1_mf`i'aid) key(aid) value(mf`i'aid2)
    vlookup ff1aid, gen(Ff1_ff`i'aid) key(aid) value(ff`i'aid2)

    vlookup ff2aid, gen(Ff2_mf`i'aid) key(aid) value(mf`i'aid2)
    vlookup ff2aid, gen(Ff2_ff`i'aid) key(aid) value(ff`i'aid2)
}

drop mf1aid2-ff2aid2


// 2. Now Delete self links in friend of friend links
forvalues i = 1(1)2 {

    replace Mf1_mf`i'aid = . if Mf1_mf`i'aid == aid
    replace Mf2_mf`i'aid = . if Mf2_mf`i'aid == aid

    replace Ff1_mf`i'aid = . if Ff1_mf`i'aid == aid
    replace Ff2_mf`i'aid = . if Ff2_mf`i'aid == aid

}


// 3. Delete friends of friends who are also friends of i
forvalues i = 1(1)2 {

    replace Mf1_mf`i'aid = . if Mf1_mf`i'aid == mf1aid | Mf1_mf`i'aid == mf2aid     
    replace Mf1_ff`i'aid = . if Mf1_ff`i'aid == ff1aid | Mf1_ff`i'aid == ff2aid

    replace Mf2_mf`i'aid = . if Mf2_mf`i'aid == mf1aid | Mf2_mf`i'aid == mf2aid     
    replace Mf2_ff`i'aid = . if Mf2_ff`i'aid == ff1aid | Mf2_ff`i'aid == ff2aid

    replace Ff1_mf`i'aid = . if Ff1_mf`i'aid == mf1aid | Ff1_mf`i'aid == mf2aid     
    replace Ff1_ff`i'aid = . if Ff1_ff`i'aid == ff1aid | Ff1_ff`i'aid == ff2aid

    replace Ff2_mf`i'aid = . if Ff2_mf`i'aid == mf1aid | Ff2_mf`i'aid == mf2aid     
    replace Ff2_ff`i'aid = . if Ff2_ff`i'aid == ff1aid | Ff2_ff`i'aid == ff2aid

}

这是我坚持的步骤。因为每个人的一些朋友分享其他朋友,我现在留下2个链接同伴,每个受访者出现不止一次。

// 4. Replace multiple listings of 2 link peers



global mfofs "Mf1_mf1aid Mf1_mf2aid Mf2_mf1aid Mf2_mf2aid Ff1_mf1aid Ff2_mf2aid"
global ffofs "Mf1_ff1aid Mf1_ff2aid Mf2_ff1aid Mf2_ff2aid Ff1_ff1aid Ff2_ff2aid"




putmata aid Z=(Mf1_mf1aid Mf1_mf2aid Mf2_mf1aid Mf2_mf2aid Ff1_mf1aid Ff2_mf2aid Mf1_ff1aid Mf1_ff2aid Mf2_ff1aid Mf2_ff2aid Ff1_ff1aid Ff2_ff2aid) 

mata: 

/*
fofa = Z
for (i=1; i<=rows(Z); i++) {
    row = fofa[i,]'
    nvals[i] = length(uniqrows(select(row, (row :< .))))
}
*/


// The below is all sorts of wrong
fof = J(rows(Z), cols(Z), .)
 for (i=1; i<=rows(Z); i++) {
    for (j=1; j<=cols(Z); j++) {
        for (k=1; k<=cols(Z); k++) {
            if (Z[i,j] - Z[i,j+k] !=0) Z[i,j] = Z[i,j] 
        }
    }
}



end 

有人能让我指出正确的方向吗?

我看到egen的{​​{1}}函数名为egenmore。但是,它只返回不重复的值。我需要保留值的第一个表达式,然后设置为缺少重复。

如果我解释得不好,那么我道歉并请让我知道它在哪里混乱。

2 个答案:

答案 0 :(得分:2)

正如Danielle建议的那样,处理长篇数据通常要简单得多。

所需要的是朋友的朋友列表。列表中的人不能成为一级朋友。在示例数据中,对于aid == 101102中唯一不是101的朋友或101的第一级朋友是104

在下面的代码中,我使用rangejoin(来自SSC)将每个观察者的朋友与他们的朋友配对。

* Example generated by -dataex-. To install: ssc install dataex
clear
input float(aid mf1aid mf2aid ff1aid ff2aid)
101 102 103 106   .
102 101 104 106   .
103 101 104 107 108
104 105 102 108 109
105 104 101 106 110
106 101 102 107 108
107 103   . 106 110
108 103 104 109 108
109 104 101 108 110
110 105 104 109 107
end

* convert to long form
rename aid id
reshape long @aid, i(id) j(mfn) string
drop if mi(aid)
drop mfn
isid id aid, sort

* save each id's list of friends
save "friends.dta", replace

* first level of friends
rename aid friend1

* pair each observation with using obs where id is the same as friend1 in current obs
rangejoin id friend1 friend1 using "friends.dta"
rename aid friend2
drop *_U friend1

* remove self and duplicates
drop if id == friend2
bysort id friend2: keep if _n == 1

* remove those are are first level friends
rename friend2 aid
merge 1:1 id aid using "friends.dta", keep(master) nogen

答案 1 :(得分:0)

如果要将所有这些变量存储在一行中(从您使用的代码和命名看起来如此),也许您可​​以将其重新整形为长数据集,然后使用其中一个重复命令来标记或消除重复项。

注意:这应该是一个评论而不是一个答案,但我没有这样做的声誉......