Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I have a question regarding having an inner function

I have below code with an inner function, but it is not returning the "sorted_rsq" whenever I run it with arguments.

def improve_model (formula, covariates, data):

    """Returns a new formula which has a new added covariate which is the best single covariate in terms of R^2"""

    #Get the formulas given all covariates
    olsformulas = extend_model(formula,covariates)
    
    #Using all formulas to calculate R^2 for each
    def ols(formula, data):

        """Calculates the R^2 of a given formula."""

        all = {}
        for formula in olsformulas:
            fit = smf.ols(formula,data).fit().rsquared
            all[formula] = fit

        sorted_rsq = sorted(all.items(), key = lambda x: x[1], reverse = True)
        return sorted_rsq[0]

    ols(formula,data)

Just giving me blank result. Can anyone help me what is wrong with the code?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Replace ols(formula,data) with return ols(formula,data) at the end of the outer function.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading