我有“链”格式的数据,其中有一些受试者从每个“锁定”中招募的受试者和“锁”或“链接”。因此,我的数据既广泛又长 - 我该如何编写stata .dta程序以重塑运行模型?我的数据像这样开始

IDLOCK IDLINK1 IDLINK2 ...

1 10 11 ...

2 20 21 ...

21 30 31 ...

稍后可以锁定链接,但它仍然是原始锁链的一部分。因此,21是链中的一个链接,以1。每个新锁(IDLink1-idlink5)最多有5个链接

有帮助吗?

解决方案

需要有关您要使用数据的内容的更多详细信息,但是我要做的第一件事是创建一些vars,以汇总每锁的链接数量(或描述链条)。然后,您可以将数据作为长面板数据将初始锁定作为图表和TimeVar视为链中的链接或节点的数量。我假设您要建模的数据集中还有更多变量(我已经将它们生成了随机DV和一些IV),那么您可以使用以下内容建模。 Stata(下面提供了一些示例):

    *******************************! BEGIN EXAMPLE
    //this first part will input the dataset into stata//
    clear
    inp id  link0 link1 link2 link3 link4
    1     1     2     3     4     5
    1000  97  98  99   . .
    3    . . . . . 
    4    . . . . . 
    5     6     7     8     9     10
    6    . . . . . 
    7    . . . . . 
    8     11  12  13  14  15
    9   . . . . . 
    10   . . . . . 
    11   . . . . . 
    12   . . . . . 
    13   . . . . . 
    14   . . . . .      
    15   . . . . .       
    99  100  . . . . .     
    100    101 . . . .     
    101   . . . . .  
    end


    //grab local macro with variables of interest//
    unab cou: link*
    di "`cou'"


    //1. DETERMINE THE INITIAL LOCK//
    tempvar pn
    g `pn' = .
    forval z=0/4{
               forval x=1/`=_N' {
               replace `pn'= id[_n-`x'] if id==link`z'[_n-`x']
          }
    }

    gen ilock=.
    lab var ilock "Initial Lock #"
    replace ilock=1 if mi(`pn')
    order ilock
    l ilock


    //2. Links assoc. with each ilock //

    **count those with no links established** 
    count if mi(link0)


    //ilocks//
    levelsof id if ilock==1, local(ilocks)
    foreach n in `ilocks' {
        //initial step//
        preserve
        keep if id==`n'
        global s`n' "`=link0' `=link1' `=link2' `=link3' `=link4'"
        di "${s`n'}"
        global s`n':subinstr global s`n' "." "", all
        di "${s`n'}"    
        restore
        }
    macro li    

    //branches off each ilock//
    foreach n in `ilocks' {
        //branches// 
            di in red "Branch `b' for macro s`n'"
            di as err "${s`n'}"
            forval b = 1/10 {
        qui token `"${s`n'}"'
        while "`1'" != ""  {
            *di in y "`1'"
            preserve
            keep if id==`1' 
            if _N==1 {
                global s`n'  ${s`n'}  `=link0' `=link1' `=link2' `=link3' `=link4' 
                di "${s`n'}"
                global s`n':subinstr global s`n' "." "", all
                di in yellow "${s`n'}"  
                global s`n':list uniq global(s`n')
            }
            restore
            mac shift
        }
        }
        }

    //g ilock_number = ilock number if ilocks==branches//
    g ilock_number = .
    foreach n in `ilocks' {
        replace ilock_number = id if id==`n'
        di in y "${s`n'}"
        global s`n':list uniq global(s`n')
        qui token `"${s`n'}"'
        while "`1'" != ""  {
            di in y "`1'"
            replace ilock_number = `n' if id==`1'
            mac shift   
        }
    }
    order ilock_number
    sort ilock_number id
    count if mi(ilock)



    **Decriptives:Count # OF linknodes**
    sort ilock id
    bys ilock_number:  count if mi(ilock)
    sort id ilock
    bys ilock_number, rc0: g linknodes = _n 
    order id link* linknodes ilock_n 
    l id link* ilock linknodes ilock_n, ta clean div
      **descriptives**
    ta ilock
    ta ilock linknodes

**here are all the chains in your data**
levelsof ilock_number, loc(al)
foreach v in `al' {
macro list  s`v'
}



    // Running models //
    **what kind of model do you want to run?**
    **assume using ids to identify panels-->

        **create fake dv/iv's for models**
    drawnorm iv1-iv5
    g dv = abs(int(rbinomial(10, .5)))

    xtset ilock_number linknodes 
    xtreg dv iv*, re

    **or model some link/lock info like the #links**
    bys ilock_number: g ttl_nodes = _N
    xtpoisson ttl_nodes iv* dv , re
    *******************************! END EXAMPLE

^注意:请注意上面代码中的包装问题!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top