当前位置:   article > 正文

Stata面板设置与面板数据多元线性回归与泊松回归命令_repeated time values within panel

repeated time values within panel

1. 设置面板

[XT] xtset – Declare data to be panel data

xtset panelvar timevar [, tsoptions]
  • 1
# example
xtset business_id year
  • 1
  • 2

常用面板数据命令 (具体用法用help查一下)

xtset: Declare a dataset to be panel data
xtdescribe: Describe pattern of xt data
xtsum: Summarize xt data
xttab: Tabulate xt data
xtdata: Faster specification searches with xt data
xtline: Line plots with xt data
xtreg: Fixed-, between- and random-effects, and population-averaged linear models
xtregar: Fixed- and random-effects linear models with an AR(1) disturbance
xtmixed: Multilevel mixed-effects linear regression
xtgls: Panel-data models using GLS
xtpcse: OLS or Prais-Winsten models with panel-corrected standard errors
xthtaylor; Hausman-Taylor estimator for error-components models
xtfrontier: Stochastic frontier models for panel data
xtrc: Random coefficients models
xtivreg: Instrumental variables and two-stage least squares for panel-data models
xtunitroot: Panel-data unit-root tests
xtabond: Arellano-Bond linear dynamic panel-data estimator xtdpdsys Arellano-Bond/Blundell-Bond estimation
xtdpd: Linear dynamic panel-data estimation
xttobit: Random-effects tobit models
xtintreg: Random-effects interval-data regression models
xtlogit: Fixed-effects, random-effects, & population-averaged logit models
xtprobit: Random-effects and population-averaged probit models
xtcloglog: Random-effects and population-averaged cloglog models
xtpoisson: Fixed-effects, random-effects, & population-averaged Poisson models
xtnbreg: Fixed-effects, random-effects, & population-averaged negative binomial models
xtmelogit: Multilevel mixed-effects logistic regression
xtmepoisson: Multilevel mixed-effects Poisson regression
xtgee: Population-averaged panel-data models using GEE
  • 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

参考来源:Stata:面板数据计量方法汇总

面板设置常见问题

1.1 pannel variable为string类型

把 string 转换为数字类型

# 自己改一下pannel variable
encode business_id, gen(business_id_1)
drop business_id
rename business_id_1 business_id
  • 1
  • 2
  • 3
  • 4

1.2 时间变量为string类型

  • 如果是只有年份的数据,可以直接把字符串转换为数值类型
destring year, replace force
  • 1
  • 如果时间变量还包含日期,则需要多一步转换

我的日期数据是这样的,包括了时分秒
在这里插入图片描述

# 把时间转换成双精度数值类型
gen double time = clock(date, "YMDhms")
  • 1
  • 2

转换后就会成为一串数值
在这里插入图片描述

stata常用时间命令,根据自己时间数据的格式以及需求选择合适的来用

Clock(s1,s2[,Y]) 
clock(s1,s2[,Y]) 
date(s1,s2[,Y]) 
daily(s1,s2[,Y])
  • 1
  • 2
  • 3
  • 4

具体还可以参考:
STATA日期函数汇总
【STATA】时间变量处理,字符串转为日期时间

1.3 报错:repeated time values within panel

面板数据下同一vari不允许出现同样的时间,因此我们需要把重复的数据去除

# 报告重复数据
duplicates report business_id time
# 列出重复数据
duplicates list business_id time
# 去除重复数据
duplicates drop business_id time, force
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2. 面板数据多元线性回归

[XT] xtreg – Fixed-, between-, and random-effects and population-averaged linear models

GLS random-effects (RE) model
    xtreg depvar [indepvars] [if] [in] [, re RE_options]

Between-effects (BE) model
    xtreg depvar [indepvars] [if] [in] , be [BE_options]
    
Fixed-effects (FE) model
    xtreg depvar [indepvars] [if] [in] [weight] , fe [FE_options]
    
ML random-effects (MLE) model
    xtreg depvar [indepvars] [if] [in] [weight] , mle [MLE_options]
    
Population-averaged (PA) model
    xtreg depvar [indepvars] [if] [in] [weight] , pa [PA_options]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
# 一维聚类调整异方差-序列相关稳健型标准误 (HAC)
xtreg like stars text_len sentiment polarity i.year, fe vce(cluster business_id)
  • 1
  • 2

在这里插入图片描述

# 二维聚类 SE
webuse "nlswork.dta", clear
vce2way regress ln_wage age grade, cluster(idcode year)
  • 1
  • 2
  • 3

参考来源:Stata 连享会: 聚类调整标准误笔记


3. 面板数据泊松回归

[XT] xtpoisson – Fixed-effects, random-effects, and population-averaged Poisson models

Random-effects (RE) model
    xtpoisson depvar [indepvars] [if] [in] [weight] [, re RE_options]

Conditional fixed-effects (FE) model
    xtpoisson depvar [indepvars] [if] [in] [weight] , fe [FE_options]

Population-averaged (PA) model
    xtpoisson depvar [indepvars] [if] [in] [weight] , pa [PA_options]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • example
# 一维聚类调整异方差-序列相关稳健型标准误 (HAC)
xtpoisson like stars text_len sentiment polarity i.year, vce(robust) i(business_id) fe
  • 1
  • 2

在这里插入图片描述

在这里回归模型会去掉 “one obs per group” 或 “all zero outcomes”的数据, 当DV不同IV相同时去掉的数据量也不一样。为了保证多个DV的模型都使用相同的数据进行回归分析,我们可以把每个DV下xtpoisson回归所使用的数据都标上记号

# xtpoisson useful stars text_len sentiment polarity i.year, vce(robust) i(business_id) fe
gen sample_useful=e(sample)
# xtpoisson funny stars text_len sentiment polarity i.year, vce(robust) i(business_id) fe
gen sample_funny=e(sample)
# xtpoisson cool stars text_len sentiment polarity i.year, vce(robust) i(business_id) fe
gen sample_cool=e(sample)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

然后再进行回归分析

xtpoisson useful stars text_len sentiment polarity i.year if sample_useful==1 & sample_funny==1 & sample_cool==1, vce(robust) i(business_id) fe

xtpoisson funny stars text_len sentiment polarity i.year if sample_useful==1 & sample_funny==1 & sample_cool==1, vce(robust) i(business_id) fe

xtpoisson cool stars text_len sentiment polarity i.year if sample_useful==1 & sample_funny==1 & sample_cool==1, vce(robust) i(business_id) fe
  • 1
  • 2
  • 3
  • 4
  • 5
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号