如何在伴侣的变量X上使Y回归?

时间:2019-09-26 09:06:12

标签: regression stata

我调查了家庭内部/伴侣之间的某些影响。我有几个变量的paneldata(人年)和一个合作伙伴ID。我想根据一个人的伴侣变量来回归一个人的结果。我不知道如何在Stata中执行此规范。

* Example generated by -dataex-. To install: ssc install dataex
clear
input float(year id pid y x)
1 1 3  9  2
2 1 3 10  4
3 1 . 11  6
1 2 4 20  2
2 2 4 21  6
3 2 3 22  7
1 3 1 25  5
2 3 1 30 10
3 3 2 35 15
1 4 2 20  4
2 4 2 30  6
3 4 . 40  8
end

* pooled regression
reg y x

* fixed effects regression
xtset year id
xtreg y x, fe

我可以进行合并和固定效果回归。但是,即使对于合并的/简单的回归,我如何才能将某人的结果回归到他人的自变量上?

实际上,对于第1个人,我需要在5/10/11回归9/10/11。等等。

  • 人员2:15年4月6日回归20/21/22
  • 人3:在2/4/7回归25/30/35
  • 人4:在2/6 /上回归20/30/40。

想法:如果regress函数中没有选项,我想我可以为我拥有的每个自变量创建新变量并将其命名为x_partner。在此示例中,x_partner应该包含5,10,。,4,6,15,2,4,7,2,6,。但我仍然不知道如何实现这一目标。

bysort id (year): egen x_partner = x[pid] // rough idea

1 个答案:

答案 0 :(得分:1)

粗略的想法行不通。 egen需要指定其自己的功能之一,仅此一项就使语法非法。

但是这里的本质是查找伙伴的值,并放入与每个标识符对齐的新变量。

感谢您使用dataex

来自SSC的

rangestat是社区提供的命令,允许单行解决方案。考虑

* Example generated by -dataex-. To install: ssc install dataex
clear
input float(year id pid y x)
1 1 3  9  2
2 1 3 10  4
3 1 . 11  6
1 2 4 20  2
2 2 4 21  6
3 2 3 22  7
1 3 1 25  5
2 3 1 30 10
3 3 2 35 15
1 4 2 20  4
2 4 2 30  6
3 4 . 40  8
end

ssc install rangestat 

rangestat wanted_y=y wanted_x=x if !missing(id, pid), interval(id pid pid) by(year) 

list, sepby(id) 

     +-------------------------------------------------+
     | year   id   pid    y    x   wanted_y   wanted_x |
     |-------------------------------------------------|
  1. |    1    1     3    9    2         25          5 |
  2. |    2    1     3   10    4         30         10 |
  3. |    3    1     .   11    6          .          . |
     |-------------------------------------------------|
  4. |    1    2     4   20    2         20          4 |
  5. |    2    2     4   21    6         30          6 |
  6. |    3    2     3   22    7         35         15 |
     |-------------------------------------------------|
  7. |    1    3     1   25    5          9          2 |
  8. |    2    3     1   30   10         10          4 |
  9. |    3    3     2   35   15         22          7 |
     |-------------------------------------------------|
 10. |    1    4     2   20    4         20          2 |
 11. |    2    4     2   30    6         21          6 |
 12. |    3    4     .   40    8          .          . |
     +-------------------------------------------------+