简体   繁体   English

statsmodels:如何将方法添加到MLE结果类中

[英]statsmodels: how to add a method to a class of MLE result

(This is not a problem of simply adding a method to a given class) (这不是简单地向给定类添加方法的问题)

What I Want to Achieve 我想要实现的目标

Using Maximum Likelihood Estimation (Generic models) of statsmodels , I implemented an MLE estimator, and want to add a user-made method, which uses exog and params , to a class of fitted result (not an instance), eg, using classmetod() . 使用statsmodels Maximum Likelihood Estimation (Generic models) ,我实现了一个MLE估计器,并且想要将一个用户制造的方法(使用exogparams )添加到一类拟合结果(不是实例),例如,使用classmetod() But an error occurs because those variables are not available. 但是因为那些变量不可用而发生错误。 How can I achieve my goal? 我怎样才能实现目标?

Let me explain what I have done so far, using an example from here . 让我用这里的例子来解释我到目前为止所做的事情。

(I had a look at this for adding a method to an existing class.) (我看了一下这个用于向现有类添加方法。)

Example

import numpy as np
from scipy import stats
import statsmodels.api as sm
from statsmodels.base.model import GenericLikelihoodModel,GenericLikelihoodModelResults

data = sm.datasets.spector.load_pandas()
endog = data.endog
exog = sm.add_constant(data.exog)

class MyProbit(GenericLikelihoodModel):
    def loglike(self, params):
        exog = self.exog
        endog = self.endog
        q = 2 * endog - 1
        return stats.norm.logcdf(q*np.dot(exog, params)).sum()

# my attemp starts ---------------
def my_method(self):
    return print(self.exog, self.params, self.model)

GenericLikelihoodModelResults.my_method = classmethod(my_method)
# my attemp ends ----------------

res = MyProbit(endog, exog).fit()

res.my_method()

This generates the following error. 这会生成以下错误。

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-29-a2d4f516bca7> in <module>
     23 res = MyProbit(endog, exog).fit()
     24 
---> 25 res.my_method()

<ipython-input-29-a2d4f516bca7> in my_method(self)
     17 # my attemp start ---------------
     18 def my_method(self):
---> 19     return print(self.exog, self.params, self.model)
     20 GenericLikelihoodModelResults.my_method = classmethod(my_method)
     21 # my attemp ends ----------------

AttributeError: type object 'GenericLikelihoodModelResults' has no attribute 'exog'

This suggests that exog (similarly, endog and params ) are not available in GenericLikelihoodModelResults . 这表明exog (类似地, endogparams )在GenericLikelihoodModelResults中不可用。 Indeed, adding the following code shows none of exog , etc. 实际上,添加以下代码不会显示exog等。

def my_check(self):
    return dir(self)

GenericLikelihoodModelResults.my_check = classmethod(my_check)

This is despite the fact that they are available at an instance, as one can check using 尽管它们可以在一个实例中使用,但可以检查使用

res.exog
res.endog
res.params

I appreciate any constructive suggestions/comments. 我感谢任何建设性的建议/意见。

The exception message 异常消息

AttributeError: type object 'GenericLikelihoodModelResults' has no attribute 'exog'

refers to the results class returned by fit . 指的是fit返回的结果类。
exog , endog and similar are attributes of the model class. exogendog和类似的是模型类的属性。 But params is a results attribute because it is an outcome of the fit method params是一个结果属性,因为它是fit方法的结果

So either access the model attribute of in the results class self.model.exog if self is a results instance, or use the method for the model class, ie when self is a MyProbit instance. 因此,如果self是结果实例,则访问结果类self.model.exog的model属性,或者使用模型类的方法,即selfMyProbit实例时。

I'm not sure about the use of a classmethod in this case. 在这种情况下,我不确定是否使用了类方法。 The attributes are only available for the specific instance of the class. 这些属性仅适用于该类的特定实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM