0%

1
2
3
4
5

cd "E:\Data Storages\Dataset for Learning\"
set scheme s1mono
use eg_simdata.dta, replace
panelview Y D , type(outcome) i(id) t(time) mycolor(Reds) discrete ytitle("haha my y title here") title("Discrete Outcome")
1
2
3

use eg_turnout.dta, replace
panelview turnout policy_edr, type(treat) i(abb) t(year) mycolor(PuBu) bytiming prepost(off) ylabel("") title("How's bytiming working?")

  • 面板数据整理
  • 巧用前一期或下一期的取值为当期负值
  • 注意[]一定要与by id: 连用
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

set obs 50
gen a = _n
gen year = 1991 if inrange(a,1,10)
replace year = 1993 if inrange(a,11,20)
replace year = 1995 if inrange(a,21,30)
replace year = 1997 if inrange(a,31,40)
replace year = 2000 if inrange(a,41,50)
gen c = uniform()
bys year:gen id = _n

drop a

xtset id year

*生成属性(来自下一轮)
by id: gen next = c[_n+1]

*生成属性(来自上一轮)
by id: gen back = c[_n-1]

*生成属性(来自第一轮)
by id: gen first = c[1]

  • 方差成分模型
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

cd "D:\Data Storages\Dataset for Learning"
use nlsy.dta,clear
gen id= _n
reshape long anti self pov, i(id)
bys id:gen times = _n
set matsize 11000

xtset id
xtreg anti,mle

------------------------------------------------------------------------------
        anti |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       _cons |   1.636833   .0557054    29.38   0.000     1.527652    1.746014
-------------+----------------------------------------------------------------
    /sigma_u |   1.208859   .0441422                      1.125365    1.298547
    /sigma_e |    1.01226   .0209977                      .9719306    1.054263
         rho |   .5878254   .0214793                      .5453008    .6293423
------------------------------------------------------------------------------
LR test of sigma_u=0: chibar2(01) = 578.26             Prob >= chibar2 = 0.000

dis e(sigma_u)^2
1.4613398

dis e(sigma_e)^2
1.0246701

mixed anti || id:, mle
eststo m1

------------------------------------------------------------------------------
        anti |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       _cons |   1.636833   .0557054    29.38   0.000     1.527652    1.746014
------------------------------------------------------------------------------


------------------------------------------------------------------------------
  Random-effects Parameters  |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
id: Identity                 |
                  var(_cons) |   1.461341   .1067235      1.266447    1.686226
-----------------------------+------------------------------------------------
               var(Residual) |    1.02467   .0425105      .9446484     1.11147
------------------------------------------------------------------------------
LR test vs. linear model: chibar2(01) = 578.26        Prob >= chibar2 = 0.0000

ICC(intraclass correlation coefficient)= var(_cons)/[var(_cons)+var(Residual)] = 1.461341 / (1.461341+1.02467) = .58782564
组内相关系数为58.78%,表明有58.78%的变异是由研究对象差异(组间斜率差异)造成

estat icc

Intraclass correlation

------------------------------------------------------------------------------
                       Level |        ICC   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
                          id |   .5878256   .0214794      .5451833    .6291892
------------------------------------------------------------------------------


mean anti

Mean estimation                   Number of obs   =      1,743

--------------------------------------------------------------
             |       Mean   Std. Err.     [95% Conf. Interval]
-------------+------------------------------------------------
        anti |   1.636833    .037777       1.56274    1.710926
--------------------------------------------------------------

reg anti
eststo m2

      Source |       SS           df       MS      Number of obs   =     1,743
-------------+----------------------------------   F(0, 1742)      =      0.00
       Model |           0         0           .   Prob > F        =         .
    Residual |  4333.11532     1,742  2.48743704   R-squared       =    0.0000
-------------+----------------------------------   Adj R-squared   =    0.0000
       Total |  4333.11532     1,742  2.48743704   Root MSE        =    1.5772


------------------------------------------------------------------------------
        anti |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       _cons |   1.636833    .037777    43.33   0.000      1.56274    1.710926
------------------------------------------------------------------------------

mixed anti
eststo m3

Mixed-effects ML regression                     Number of obs     =      1,743


                                                Wald chi2(0)      =          .
Log likelihood = -3266.8666                     Prob > chi2       =          .


------------------------------------------------------------------------------
        anti |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       _cons |   1.636833   .0377662    43.34   0.000     1.562813    1.710853
------------------------------------------------------------------------------


------------------------------------------------------------------------------
  Random-effects Parameters  |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
               var(Residual) |    2.48601   .0842111      2.326319    2.656663
------------------------------------------------------------------------------

lrtest m1 m3, stats

Likelihood-ratio test                                 LR chi2(1)  =    578.26
(Assumption: m3 nested in m1)                         Prob > chi2 =    0.0000

-----------------------------------------------------------------------------
       Model |          N   ll(null)  ll(model)      df        AIC        BIC
-------------+---------------------------------------------------------------
          m3 |      1,743          .  -3266.867       2   6537.733    6548.66
          m1 |      1,743          .  -2977.735       3    5961.47   5977.861
-----------------------------------------------------------------------------
Note: BIC uses N = number of observations. See [R] BIC note.

  • 劳伦斯·马希,戴维·科米尔,《样条回归模型》
  • 样条回归可以刻画两条回归线在连接点处平滑的斜率变化,并且避免回归线中间出现断裂。
  • 样条回归相比多项式回归的优势
  • 多项式回归可以用时间、时间的平方项、时间的立方项等作为自变量,然而这样很快会遇到完全多重共线性问题。
  • 多项式回归不够灵活,不足以捕捉到斜率的突然变化,尤其当变化周期不规律的时候。
  • 样条回归相比虚拟变量回归/阶跃式回归(piecewise regression)的优势
  • 样条回归是限制性的虚拟变量回归,样条变化处是连续而非断裂的。
  • 分离回归(sperate regression)

1
2
3
4

use "D:\Data Storages\Dataset for Learning\depression_clean.dta", clear
reshape long dep, i(sid) j(time)
mixed dep time || sid:time, var cov(unstr)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21

------------------------------------------------------------------------------
         dep |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        time |    -1.6025   .4262612    -3.76   0.000    -2.437957   -.7670434
       _cons |   14.18924   .8147121    17.42   0.000     12.59243    15.78605
------------------------------------------------------------------------------


------------------------------------------------------------------------------
  Random-effects Parameters  |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
sid: Unstructured            |
                   var(time) |   3.201386   2.047798      .9138158    11.21547
                  var(_cons) |   21.93819   6.613945       12.1501    39.61154
             cov(time,_cons) |  -1.153612   2.751286     -6.546034     4.23881
-----------------------------+------------------------------------------------
               var(Residual) |    10.3135    2.15051      6.853596    15.52006
------------------------------------------------------------------------------
LR test vs. linear model: chi2(3) = 54.85                 Prob > chi2 = 0.0000