数据扩展,以便创建两个协变量的所有可能组合 [英] Data expansion so to create all possible combinations of two covariates

查看:61
本文介绍了数据扩展,以便创建两个协变量的所有可能组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据集如下所示:

clear all

input id year x
      1  1992 1
      1  1995 5
      1  1996 7
      2  1992 2
      2  1993 4
end

我需要根据特定于 id 的观察数扩展数据集,并创建以下数据结构,然后将其用于进一步的计算(基本上是针对每个 id - year 值的组合,我必须重复最初的 year 值集):

I need to expand the dataset according to the id-specific number of observations and create the following data structure then used for further computations (basically, for each id-year combination of values I have to repeat the initial set of year values):

     id  year  y2   x 
      1  1992  1992 1
      1  1992  1995 1
      1  1992  1996 1
      1  1995  1992 5
      1  1995  1995 5
      1  1995  1996 5
      1  1996  1992 7
      1  1996  1995 7
      1  1996  1996 7
      2  1992  1992 2
      2  1992  1993 2
      2  1993  1992 4
      2  1993  1993 4

扩展很容易通过以下方式获得:

The expansion is easily obtained with:

bysort id: gen N = _N
expand n_obs, gen(expanded)

但是,也许这个问题很简单,但是我找不到合适的策略来获取 y2 .感谢您的任何建议.

However, perhaps the problem is trivial, but I cannot find a suitable strategy to obtain y2. Thanks for any suggestions.

推荐答案

这里有一些技巧; joinby 是关键.由于您随后编辑了您的问题以包含变量x,因此我编辑了此答复,以仅保留ID和y2在与原始文件连接的文件中.

Here is some technique; joinby is the key. Since you subsequently edited your question to include the variable x, I edited this reply to retain only id and y2 in the file being joined to the original.

// setup test data
clear all
input id year x
      1  1992 1
      1  1995 5
      1  1996 7
      2  1992 2
      2  1993 4
end
tempfile t1
save `t1'
clear

// do the job
use `t1'
rename year y2
keep id y2
joinby id using `t1'
order id year y2
sort id year y2
list, sepby(id)

哪个给了我们

     +----------------------+
     | id   year     y2   x |
     |----------------------|
  1. |  1   1992   1992   1 |
  2. |  1   1992   1995   1 |
  3. |  1   1992   1996   1 |
  4. |  1   1995   1992   5 |
  5. |  1   1995   1995   5 |
  6. |  1   1995   1996   5 |
  7. |  1   1996   1992   7 |
  8. |  1   1996   1995   7 |
  9. |  1   1996   1996   7 |
     |----------------------|
 10. |  2   1992   1992   2 |
 11. |  2   1992   1993   2 |
 12. |  2   1993   1992   4 |
 13. |  2   1993   1993   4 |
     +----------------------+

这篇关于数据扩展,以便创建两个协变量的所有可能组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆