首页 > 解决方案 > 如何在合作伙伴的变量 X 上回归一个人的 Y?

问题描述

我调查了家庭内部/伴侣之间的某些影响。我有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/ 2011 年 9 月 10 日回归。等等。

想法:如果函数中没有选项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

标签: regressionstata

解决方案


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

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

感谢您使用dataex.

rangestat来自社区贡献的命令 SSC 允许使用单线解决方案。考虑

* 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          .          . |
     +-------------------------------------------------+

推荐阅读