【问题标题】:Linking Peers of Peers: Replacing multiple listings of 2 link peers链接对等点:替换 2 个链接对等点的多个列表
【发布时间】:2016-06-28 01:12:15
【问题描述】:

我的数据提供了受访者指定朋友的标识符。我正在尝试链接 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 

谁能让我指出正确的方向?

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

如果我解释得不好,那么我很抱歉,请让我知道它在哪里令人困惑。

【问题讨论】:

    标签: variables stata


    【解决方案1】:

    正如 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
    

    【讨论】:

    • Robert 这比我的方法更有效率,感谢@Danielle。但是,我需要真正的对等点,即由 2 个链接分隔的对等点。对于节点集 {i,j,k},其中 i 链接到 j,j 链接到 k,但 i 和 k 未链接,我需要 k 作为 i 的朋友 2 而不是 j 和 k。因此,对于friend2,必须将同时出现在friend1 下的那些人设置为丢失或丢弃。我现在正在做这件事。如果我弄清楚了,那么我会注意到变化。感谢您的帮助!
    • 如果我理解正确的话,那就更简单了。我已经相应地调整了代码。最后,你找到朋友的朋友,然后简单地保留那些与一级朋友不匹配的人。
    • 是的,这是正确的,您的方法效果很好。我会投票赞成你的答案,但显然我还不允许这样做。
    • 我从来没有在这个论坛上问过问题,所以我不知道你能得到什么,但我相信你可以选择“接受”我的回答。根据帮助:“接受由问题原作者接受的答案旁边的彩色复选标记表示。”
    【解决方案2】:

    如果您将所有这些变量连续存储(从您使用的代码和命名看来是这样),也许您可​​以将其重塑为一个长数据集,然后使用重复命令之一来标记或消除重复项。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多