You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.)
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?
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 =/
Regularization is a work in progress, not just in terms of our
implementation, but also in terms of methods that are available. For
example, I am not aware of a generally accepted way to get standard errors
for parameter estimates from a regularized estimate (there are relatively
recent papers on this topic, but the implementations are complex and there
is no consensus on the best approach). I plan to look into this, but as of
now we don't have anything beyond the parameter estimates.
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.
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()))
We may not have implemented summary methods for all the fit_regularized
models yet. You should still be able to see the parameter estimates using
results_fr.params. There isn't much more than that to put into a summary
table (no standard errors).
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
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.
@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.
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.
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.
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())
Reacted by André F. Rendeiro, Patricia Bongiovanni Catandi, Vega, Taylor Joren, Matheus Calvelli, Nathan Timothy Handoko, Harry Lee and CowanCS1
Reacted by Manuel and lashmore
Reacted by JyotirmayS, Patricia Bongiovanni Catandi, Vlad Fomenko, Jesse Engelberg, yuguangshen and Nathan Timothy Handoko
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.
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.
Reacted by ZhihengChe, Kenneth Leung, Harry Lee and Prz00124
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?
Reacted by Patricia Bongiovanni Catandi, Vlad Fomenko, Daniil Zverev, Diane M. Napolitano, imzzhou, Max Ghenis, Ash, Seyed Ali Osia, Charles Dufour, Rohini Jayanthi and 11 more
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?
Reacted by hx-rohan, Kenneth Leung, Diane M. Napolitano, Henzhwang and John
Activity
Chroner2 commentedon May 22, 2017
Had same issue, check the
Kara.paramsiceman121 commentedon May 28, 2017
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.
josef-pkt commentedon May 28, 2017
related #3626 There is currently no cov_params and inference for elastic net penalization.
josef-pkt commentedon May 28, 2017
Thanks for the stackoverflow link.
To the answer: using the standard cov_params,
normalized_cov_paramsignores the penalization (and I'm not sure it uses the scale estimate, but maybe it does that.)borgesf commentedon Jun 29, 2017
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?
iceman121 commentedon Jun 29, 2017
'Kara' was just the name I gave to my object. Basically, if you do
sm.OLS().fit_regularized(), the object has an attribute calledparams.You can call it in the following way:
Does that help?
borgesf commentedon Jun 30, 2017
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!
owlas commentedon Jul 10, 2017
Thanks @Chroner2 for the temporary fix, I'm also using
.paramsfor now.However, there is still a lot of missing information. Do we know why?
kshedden commentedon Jul 10, 2017
xguse commentedon Jul 25, 2017
I just wanted to put this here in case it wasn't one of the papers that @kshedden was refering to with
This paper lists the inventor of LASSO as an author, I believe.
https://projecteuclid.org/euclid.aos/1400592161
allenanie commentedon Jun 24, 2018
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
2 remaining items
doodlecancode commentedon Oct 8, 2018
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()))
kshedden commentedon Oct 8, 2018
LudvicLaberge commentedon Mar 3, 2019
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:
Also, this message kept returning:
kshedden commentedon Mar 3, 2019
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.
LudvicLaberge commentedon Mar 3, 2019
Thank you for the quick answer, just read the comment in the link
Are there any plans of implementing this? Some packages in R have, like HDtweedie, but I'd prefer to stay in python.
kshedden commentedon Mar 3, 2019
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.
josef-pkt commentedon Mar 4, 2019
@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.
thequackdaddy commentedon Mar 4, 2019
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
loglikewhich is pretty trivial. My understanding is that it should be sufficient to estimate Beta parameters.LudvicLaberge commentedon Mar 4, 2019
Yes the Extended Quasi Loglikelihood as
loglikewould do the trick.@kshedden here is a link to the formula, right under this line:
karmadina commentedon Apr 9, 2019
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())kshedden commentedon Apr 9, 2019
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.FranciscoReveriano commentedon Jul 9, 2020
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.
ManuelZ commentedon Aug 6, 2020
I got into this also.
ianbstewart commentedon Oct 3, 2020
Weird bug: it seems that the solution from @karmadina (set refit=True) works only if
L1_wtis not equal to zero. Using the example code as preamble:If we run
fit_regularizedwith a higherL1_wtthen it's OK:But if we run it with a weight of 0, then we get an error:
yields
NotImplementedError.Is there something under the hood that checks
L1_wtand prevents the summary from being generated?natwille1 commentedon Feb 4, 2022
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?