重塑“链”中的数据格式(stata .DTA 文件)

发布于 2024-10-10 06:35:44 字数 302 浏览 2 评论 0原文

我有“链”格式的数据,其中有接受治疗“锁”的受试者和从每个“锁”招募的受试者或“链接”。因此,我的数据形状既宽又长 - 我如何编写 Stata .DTA 程序来重塑运行模型?我的数据是这样开始的

idlock idlink1 idlink2 ...

1 10 11 ...

2 20 21 ...

21 30 31 ...

一个链环以后可以成为一把锁,但它仍然是原始锁链的一部分。因此,21 是从 1 开始的链条中的一个环节。 每个新锁最多有 5 个链接(idlink1-idlink5)

I've got data in "chain" format where there are subjects that get a treatment "locks" and subjects, or "links", that are recruited from each "lock". Therefore, my data are shaped both widely and long - how can I write a Stata .DTA program to reshape for running models? My data start like this

idlock idlink1 idlink2 ...

1 10 11 ...

2 20 21 ...

21 30 31 ...

and a link can be come a lock later on, but it is still a part of the chain of the original lock. So, 21 is a link in the chain that starts with 1.
There are up to 5 links for each new lock (idlink1-idlink5)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不如归去 2024-10-17 06:35:44

需要更多关于您想要对数据执行的操作的详细信息,但我要做的第一件事是创建一些变量来总结每个锁的链接数量(或描述链)。然后,您可以将数据视为长面板数据,将初始锁定作为 panelid,将 timevar 作为链中链接或节点的数量。我假设您在数据集中还有一些想要建模的变量(我已将它们生成为随机 DV 和一些 IV),然后您可以使用 -xt- 命令套件对您想要建模的任何内容进行建模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

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

More details on what you want to do with the data are needed, but the first thing I would do is create some vars that summarize the number of links per lock (or describe the chains). Then you can treat the data as long panel data with the initial lock as the panelid and the timevar as the number of links or nodes in the chain. I assume you have some more variables in the dataset that you want to model (I've generated them as a random DV and some IVs), then you can model whatever it is you want to model using the suite of -xt- commands in Stata (some examples are provided below):

    *******************************! 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

^note: watch for wrapping issues in the code above!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文