Wednesday, 8 February 2023

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

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()


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

No comments:

Post a Comment