1. 面板回归(Python)

import statsmodels.api as sm # 回归
import statsmodels.formula.api as smf # 回归
from linearmodels.panel import PanelOLS # 面板回归

# 定义变量/因变量/自变量
dataset = stock_return_FF3_month
var_group = 'Stkcd'
var_date = 'date'
independent_variable_list = ['RiskPremium1_lag1','SMB1_lag1','HML1_lag1']
dependent_variable_factor = 'Mretwd'

# 面板回归之前,需要先设置“个股在外时间在内”的层次化索引 
dataset.set_index([var_group,var_date],inplace = True)

# 指定自变量和因变量
independent_variable = sm.add_constant(dataset[independent_variable_list])
dependent_variable = dataset[dependent_variable_factor]


# 设置个体固定效应和时间固定效应,并将回归结果打印出来(混合回归)
grunfeld_fet_pooled = PanelOLS(dependent_variable, independent_variable, entity_effects=False, time_effects=False)  
grunfeld_fet_pooled = grunfeld_fet_pooled.fit()  
print(grunfeld_fet_pooled)

# 设置个体固定效应和时间固定效应,并将回归结果打印出来(单向固定效应)
grunfeld_fet_oneway = PanelOLS(dependent_variable, independent_variable, entity_effects=True, time_effects=False)  
grunfeld_fet_oneway = grunfeld_fet_oneway.fit()  
print(grunfeld_fet_oneway)

# # 设置个体固定效应和时间固定效应,并将回归结果打印出来(双向固定效应)
# grunfeld_fet_twoway = PanelOLS(dependent_variable, independent_variable, entity_effects=True, time_effects=True)  
# grunfeld_fet_twoway = grunfeld_fet_twoway.fit()  
# print(grunfeld_fet_twoway)

你可能感兴趣的:(1. 面板回归(Python))