Skip to content

fit_regularized().summary() shows as None #3670

Open

Description

@iceman121

Hi,
I am running the following regression code:

Kara = sm.OLS(train.ix[:,0], train.ix[:, col_for_coeff]).fit_regularized(alpha=0.2, L1_wt=0.5)
print(Kara.summary())

The output is shown as

None

There was hardly any incidents on Stackoverflow, except one small mention in the comments section on this question. Any help?

System info:
OS: Windows 8.1
Python: 3.5.3
Statsmodels: 0.8.0

Activity

  1. Chroner2 commented on May 22, 2017

    @Chroner2

    Had same issue, check the Kara.params

  2. iceman121 commented on May 28, 2017

    @iceman121
    Author

    That's what I used as a workaround, but I figured people would like to know that this is happening, and that I am not the only one facing this issue.

  3. josef-pkt commented on May 28, 2017

    @josef-pkt
    Member

    related #3626 There is currently no cov_params and inference for elastic net penalization.

  4. josef-pkt commented on May 28, 2017

    @josef-pkt
    Member

    Thanks for the stackoverflow link.
    To the answer: using the standard cov_params, normalized_cov_params ignores the penalization (and I'm not sure it uses the scale estimate, but maybe it does that.)

  5. borgesf commented on Jun 29, 2017

    @borgesf

    Hi guys. Sorry, It was not very clear to me if there is a solution - and could not find anything about Kara.params. Any way to find the parameters of the regularized regression?

  6. iceman121 commented on Jun 29, 2017

    @iceman121
    Author

    'Kara' was just the name I gave to my object. Basically, if you do sm.OLS().fit_regularized(), the object has an attribute called params.

    You can call it in the following way:

    supercool_godawesome_model = sm.OLS(exog, endog).fit_regularized(alpha=0.2, L1_wt=0.5)
    regularized_regression_parameters = supercool_godawesome_model.params
    print(regularized_regression_parameters)
    

    Does that help?

  7. borgesf commented on Jun 30, 2017

    @borgesf

    Dang! I have not checked that the name of your object was Kara, sorry for that. Now I get it.

    I was just wondering if there was a way to get ohter goodness-of-fit parameters, because I wanted to test backward elimination, but I guess I will have to resort to conventional OLS without regularization to do so =/

    Thank you for the help!

  8. owlas commented on Jul 10, 2017

    @owlas

    Thanks @Chroner2 for the temporary fix, I'm also using .params for now.

    However, there is still a lot of missing information. Do we know why?

  9. kshedden commented on Jul 10, 2017

    @kshedden
    Contributor
  10. xguse commented on Jul 25, 2017

    @xguse

    I just wanted to put this here in case it wasn't one of the papers that @kshedden was refering to with

    ...there are relatively recent papers on this topic,...

    This paper lists the inventor of LASSO as an author, I believe.
    https://projecteuclid.org/euclid.aos/1400592161

  11. allenanie commented on Jun 24, 2018

    @allenanie

    Almost a fully year later :) I just encountered this problem again.

    I think at least people should fix the documentation first? Instead of saying it returns A RegularizedResults instance, should say it returns the model instead.

    http://www.statsmodels.org/dev/generated/statsmodels.regression.linear_model.OLS.fit_regularized.html

  12. 2 remaining items

  13. doodlecancode commented on Oct 8, 2018

    @doodlecancode

    I am trying to get the summary from the model as below but get a 'None' object when i print summary. Is there a way to get the summary?
    model = sm.WLS(y, xx, weights = weight)
    results_fr = model.fit_regularized(L1_wt = 0)
    prop.logger.debug("Weighted: {}".format(results.summary()))

    image

  14. kshedden commented on Oct 8, 2018

    @kshedden
    Contributor
  15. LudvicLaberge commented on Mar 3, 2019

    @LudvicLaberge

    Is the fit_regularized method stable for all families?

    @kshedden I used the code you provided above, got results with the Negative Binomial family, but when I tweaked it for Tweedie distribution I get no result.params of None:

    import numpy as np                                                                                                                           
    import statsmodels.api as sm                                                                                                                 
    import pandas as pd                                                                                                                          
                                                                                                                                                 
    n = 100                                                                                                                                      
    x1 = np.random.normal(size=n)                                                                                                                
    x2 = np.random.normal(size=n)                                                                                                                
                                                                                                                                                 
    y = np.exp(np.random.normal(size=n))     # adjusted the y to fit the tweedie domain                                                                                                    
                                                                                                                                                 
    df = pd.DataFrame({"y": y, "x1": x1, "x2": x2})                                                                                              
                                                                                                                                                 
    model = sm.GLM.from_formula("y ~ x1 + x2", family=sm.families.Tweedie(var_power=1.5), data=df)                                                   
    result = model.fit_regularized()                                                                         
                                                                                                                                                 
    print(result.params)
    

    Also, this message kept returning:

    C:\Users\Ludvic\Anaconda3\lib\site-packages\statsmodels\genmod\generalized_linear_model.py:753: RuntimeWarning: overflow encountered in power
      resid = np.power(self.endog - mu, 2) * self.iweights
    
  16. kshedden commented on Mar 3, 2019

    @kshedden
    Contributor

    I haven't looked at the Tweedie code before, but just scanning it I noted that loglike_obs was not implemented:

    https://github.com/statsmodels/statsmodels/blob/master/statsmodels/genmod/families/family.py#L1457

    The is needed for fit_regularized to work.

    It would have been easier to get to the bottom of this if we raised an exception there instead of returning NaN.

  17. LudvicLaberge commented on Mar 3, 2019

    @LudvicLaberge

    Thank you for the quick answer, just read the comment in the link

    This is not implemented because of the complexity of calculating infinite series of sums.

    Are there any plans of implementing this? Some packages in R have, like HDtweedie, but I'd prefer to stay in python.

  18. kshedden commented on Mar 3, 2019

    @kshedden
    Contributor

    The log-likelihood is equation 8 here:

    https://arxiv.org/pdf/1508.06378.pdf

    I don't know off-hand how to relate mu, phi, rho, and alpha to the parameters of the GLM. If we could establish that, it would not be too hard to implement.

  19. josef-pkt commented on Mar 4, 2019

    @josef-pkt
    Member

    @thequackdaddy has the code for tweedie loglike approximation, but I don't find it right now.
    It's not in #3079.

    So far, we haven't really needed loglike of tweedie, irls and newton only use derivative information and not the objective function, so we can estimate params and cov_params without having loglike. That seems to work pretty well. The nan for loglike is so that summary does not break, AFAIR.

  20. thequackdaddy commented on Mar 4, 2019

    @thequackdaddy
    Member

    As mentioned, the challenge is that Tweedie ancipiates an infinite series of sums. You basically have the guess which of the terms are computationally meaningful (within machine precision) and include those. The rest you get to ignore. Otherwise, the sum--being infinite--wouldn't end.

    I have a separate GitHub project that I personally use to estimate the Tweedier parameter... https://github.com/thequackdaddy/tweedie

    It follows a paper by Peter K Dunn pretty closely. I don't think I would suggest adding it to statsmodels. I used scipy's distribution infrastructure, but its messy ... scipy doesn't anticipate having a continuous distribution that allows exact 0's, as Tweedie does. Its good enough for me.

    I think a compromise might be to add the "Extended Quasi Logliklihood" for Tweedie as the loglike which is pretty trivial. My understanding is that it should be sufficient to estimate Beta parameters.

  21. LudvicLaberge commented on Mar 4, 2019

    @LudvicLaberge

    Yes the Extended Quasi Loglikelihood as loglike would do the trick.

    @kshedden here is a link to the formula, right under this line:

    The extended quasi-likelihood (EQL) is constructed according to the definition of McCullagh and Nelder (1989, Chapter 9) as

  22. karmadina commented on Apr 9, 2019

    @karmadina

    Hi, I had the same issue which I solved using refit=True in the fit_regularized function, this will run a fit on the first result and return an object that supports summary()
    Kara = sm.OLS(train.ix[:,0], train.ix[:, col_for_coeff]).fit_regularized(alpha=0.2, L1_wt=0.5, refit=True)
    print(Kara.summary())

  23. kshedden commented on Apr 9, 2019

    @kshedden
    Contributor

    We haven't settled on what post-estimation information is provided following a regularized fit. Therefore, a Summary table would basically only contain the parameter estimates, which you can also get from result.params. Once we have a way to get standard errors or other interesting post-estimation quantities, we can build a summary table.

  24. FranciscoReveriano commented on Jul 9, 2020

    @FranciscoReveriano

    It would be great if a "model.summary" package was created
    So we could make sure that it outputs the results automatically.
    I have also ran to this problem.

  25. ManuelZ commented on Aug 6, 2020

    @ManuelZ

    I got into this also.

  26. ianbstewart commented on Oct 3, 2020

    @ianbstewart

    Weird bug: it seems that the solution from @karmadina (set refit=True) works only if L1_wt is not equal to zero. Using the example code as preamble:

    import numpy as np
    import statsmodels.api as sm
    from statsmodels.genmod.families.links import logit
    import pandas as pd
    np.random.seed(123)
    n = 100
    x1 = np.random.normal(size=n)
    x2 = np.random.normal(size=n)
    y = ((-x2 + np.random.normal(scale=1, size=n)) > 0.).astype(int)
    df = pd.DataFrame({"y": y, "x1": x1, "x2": x2})
    model = sm.GLM.from_formula("y ~ x1 + x2", family=sm.families.family.Binomial(link=logit()), data=df)
    

    If we run fit_regularized with a higher L1_wt then it's OK:

    result = model.fit_regularized(alpha=1e-5,method='elastic_net',L1_wt=0.5,refit=True)
    print(results.summary())
    

    But if we run it with a weight of 0, then we get an error:

    result = model.fit_regularized(alpha=1e-5,method='elastic_net',L1_wt=0.,refit=True)
    print(results.summary())
    

    yields NotImplementedError.

    Is there something under the hood that checks L1_wt and prevents the summary from being generated?

  27. natwille1 commented on Feb 4, 2022

    @natwille1

    How come you're able to provide standard error estimates using statsmodels.discrete.discrete_model.Logit.fit_regularized? The implementation uses L1 regularization and still provides a summary method that results in an object containing standard errors, 95% confidence intervals, pvalues etc?

    I guess the other frustration is that the documentation for the statsmodels.genmod.generalized_linear_model.GLM.fit_regularized class just isn't correct - it states that returned object is of type GLMResults whereas actually it's an instance of statsmodels.base.elastic_net.RegularizedResultsWrapper.. at the very least, this could be updated?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions