如何使用自定义的自助法估计来更新linearmodels PanelResults对象?

huangapple go评论130阅读模式
英文:

How can I update a linearmodels PanelResults object with custom bootstrap estimates?

问题

我构建了自己的类来实现一个估算过程(称之为EstimationProcedure)。要运行该过程,用户调用fit方法。首先,它使用linearmodels包中PooledOLS类的fit方法拟合一个Pooled OLS模型。这将返回一个PanelResults对象,我将其存储在变量model中。其次,我的fit方法估算标准错误、t-统计量、p-值等(使用我编写的自定义自助法),其结果存储在局部变量中,例如std_errorststatspvalues等。现在,我的方法应该返回一个PanelResults对象,该对象结合了初始估算和我的估算结果(因为我想使用linearmodel的功能来比较多个回归并生成latex输出)。

为此,我需要创建一个新的PanelResults对象。然而,通过model的属性无法访问所需的信息。

从概念上讲,我需要做什么来实现这一点?或者有没有更聪明的方法来实现这一点?我认为这实际上是一个关于面向对象编程(OOP)的问题,而我没有经验。

以下代码说明了我的类的结构:

from linearmodels.panel import PooledOLS
from linearmodels.panel.results import PanelResults

class EstimationProcedure:
    def __init__(self, data):
        self.data = data
    
    def fit(self):
        # 估算Pooled OLS
        model = PooledOLS(self.data)

        # 使用自助法构建自己的结果
        # 这需要来自初始PooledOLS估算的结果
        std_errors, tstats, pvalues = self.bootstrap(self.data)
            
        # 要创建并返回一个新的PanelResults对象,我需要
        # 从初始Pooled OLS估算中传递一些结果,比如`res`
        # 和我的自己的结果到构造函数。然而,`PooledOLS`
        # 在不通过属性使其可访问的情况下,内部准备
        # 了`PanelResults`构造函数所需的估算结果。因此,我无法“重新创建”它。
        res = dict()
        return PanelResults(res)

# 数据存储在某个数据框中
df = pd.DataFrame()

# 使用我的估算过程
model = EstimationProcedure(df)
model.fit()
英文:

I built my own class to implement an estimation procedure (call it EstimationProcedure). To run the procedure, the user calls method fit. First, this fits a Pooled OLS model using the fit method of the PooledOLS class from the linearmodels package. This returns a PanelResults object which I store in variable model. Second, my fit method estimates, e.g., standard errors, t-statistics, p-values, etc. (using a custom bootstrapping method I wrote) whose results are stored in local variables, e.g., std_errors, tstats, pvalues, etc. My method shall now return a PanelResults object that combines information from the initial estimation and my own estimates (because I want to use linearmodel's capabilities to compare multiple regressions and produce latex output).

To this end, I need to create a new PanelResults object. However, the necessary information is not accessible through attributes of model.

Conceptually, what would I need to do to implement this? Or is there a smarter way to achieve this? I suppose that this is rather a question on OOP which I have no experience with.

The following code illustrates the structure of my class:

from linearmodels.panel import PooledOLS
from linearmodels.panel.results import PanelResults

class EstimationProcedure:
    def __init__(self, data):
        self.data = data
    
    def fit(self):
        # estimate Pooled OLS
        model = PooledOLS(self.data)

        # construct my own results using a bootstrap procedure
        # this requires the result from an initial PooledOLS estimation
        std_errors, tstats, pvalues = self.bootstrap(self.data)
            
        # to create and return a new PanelResults object, I need 
        # to pass a number of results, say `res`, from the initial
        # pooled OLS estimation along with my own results to the
        # constructor. However, `PooledOLS` prepares 
        # estimation results required by `PanelResults`'s
        # constructor internally without making them accessible
        # through attributes. Hence, I cannot "recreate" it.
        res = dict()
        return PanelResults(res)

# data is stored in some dataframe
df = pd.DataFrame()

# usage of my estimation procedure
model = EstimationProcedure(df)
model.fit()

答案1

得分: 1

你可以创建一个名为CustomPanelResultsPanelResults子类(第1行),并重写其构造函数以接受你的自定义结果(第2行)。然后,调用父类的构造函数,将来自PooledOLS估计的结果(第3行)以及你的自定义结果(第4行)一起传递,以创建一个新的PanelResults对象,其中包括两组结果。

然后,修改你的EstimationProcedure类的fit方法,以创建该子类的实例并传递必要的参数:

def fit(self):
    model = PooledOLS(self.data)
    std_errors, tstats, pvalues = self.bootstrap(self.data)
    res = {'std_errors': std_errors, 'tstats': tstats, 'pvalues': pvalues}
    return CustomPanelResults(model, res)
英文:

You can create a subclass of PanelResults named CustomPanelResults (line 1) and override its constructor to take in your custom results (line 2). Then call the constructor of the parent class with the results from the PooledOLS estimation (line 3) along with your custom results (line 4) to create a new PanelResults object that includes both sets of results.

class CustomPanelResults(PanelResults):              # line 1
    def __init__(self, model, custom_results):       # line 2
        super().__init__(model)                      # line 3
        self.custom_results = custom_results         # line 4

Then modify the fit method of your EstimationProcedure class to create an instance of this subclass and pass in the necessary arguments to its constructor:

def fit(self):
    model = PooledOLS(self.data)
    std_errors, tstats, pvalues = self.bootstrap(self.data)
    res = {'std_errors': std_errors, 'tstats': tstats, 'pvalues': pvalues}
    return CustomPanelResults(model, res)

答案2

得分: 0

  1. 创建PooledOLS的子类
class CustomPooledOLS(PooledOLS):
  1. 查找计算所需内容的方法(或方法)。我假设PooledOLS计算了你需要的一些内容,然后将其丢弃。
  2. 重写计算所需内容的方法(们),并更改它以保存所需内容在某个参数中。
def nameOfTheFunction(all, of, the, function, arguments):
    # - 在这里复制原始函数的所有代码
    # - 修改它,以便将所需的内容保存在对象中,例如
    this.tstats_cache = temp_tstats
    # 稍后,你可以在EsimationProcedure中使用这些内容,以提供给PanelResults

我有些难以理解你的全部需求,但希望这有所帮助。

英文:

Not very clean solution, but sometimes its impossible to do otherwise.

  1. Create child class of PooledOLS
class CustomPooledOLS(PooledOLS):
  1. Find where is method (or methods) that computes what you need. I assume PooledOLS calculates something you need and disregards it afterwards.
  2. Overwrite method(s) that compute(s) things you need and change it so it saves what you need in some parameter
def nameOfTheFunction(all, of, the, function, arguments):
    # - here copy all of the original function code
    # - modify it so that it saves in the object what is needed, e.g.
    this.tstats_cache = temp_tstats 
    # later you can use those in EsimationProcedure to feed to PanelResults

I had difficulty understanding all your needs, but hopefully this helps you

huangapple
  • 本文由 发表于 2023年2月6日 04:13:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定