Home > Enterprise >  I have a question regarding having an inner function
I have a question regarding having an inner function

Time:11-23

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?

CodePudding user response:

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

  • Related