I am trying to run a Random Effects Regression Model on my datasets but i am getting this error messages as seen below

25 views Asked by At

I am getting this error messages:ValueError: Cannot predict random effects from singular covariance structure.

I have a very long line of code but i will copy and paste in here the part that i think that is causing that Error above

I have two script: one is model_resources.py and another one is model_execution.py

This is the part in model_excution.py

if 'elastic net' in model_type: model = ElasticNetModel(X, y, variables, intercept)

model.find_alpha_curves(.1, n_values=30, alpha_min_logspace=-4, alpha_max_logspace=-1)
model.order_variables()

data_final, final_model = search_for_best_alpha(model, a_min=-4, a_max=-1, min_r2=0, r2_forgiveness=.9)
preds = final_model.best_model.predict(X.loc[:, final_model.best_model.x_labels])

coef_df = final_model.best_model.coef_df.copy()

if intercept:
    coef_df.loc['intercept'] = [final_model.best_model.intercept_, abs(final_model.best_model.intercept_)]       

elif 'stepwise ols (max)' in model_type: model = StepwiseModel_all(X, y, variables, intercept=intercept) model.fit(print_=True)

if show_alternatives:
    results_dct = get_coefficients_for_each_variable_type(model, X, y, variables, intercept, name_to_type)
    alternate_results = pd.DataFrame(results_dct).T.reset_index().rename(columns={'index': 'variable_type_added'})

model.best_model.r2 = model.best_model.rsquared

if intercept:
    X_for_prediction = X.loc[:, model.best_model.params.index[:-1]]
    X_for_prediction['intercept'] = 1
    preds = model.best_model.predict(X_for_prediction)
    coef_df = pd.DataFrame(model.best_model.params).rename(columns={0: 'coef'})
else:
    preds = model.best_model.predict(X.loc[:, model.best_model.params.index])
    coef_df = pd.DataFrame(model.best_model.params).rename(columns={0: 'coef'})       

elif 'mixed media' in model_type: step_model = StepwiseModel_all(X, y, variables, intercept=intercept) step_model.fit(print_=False)

var_names_no_intercept = [col for col in step_model.best_model.params.index if col != 'intercept']

fixed_var_names = [col for col in var_names_no_intercept if not is_variable_re[col]]
re_var_names = [col for col in var_names_no_intercept if is_variable_re[col]]
    
model = MixedModel(X, y, variables, dmas, is_variable_re, intercept=intercept, independent_vars=True, metric='r2')

# Fit the mixed model using regularization and different covariance structures
cov_structures = ["diagonal", "unstructured", "compound symmetry"]
success = False

for cov_type in cov_structures:
    try:
        # Include regularization penalty (e.g., L2 penalty with alpha=0.1)
        result, one_model = model.fit_mixed_model(
            fixed_var_names + re_var_names,
            re_var_names,
            cov_type,
        )
        success = True
        break
    except LinAlgError:
        print(f"Failed to fit the model with covariance structure: {cov_type}")

if not success:
    raise Exception("Failed to fit the mixed model with all covariance structures")
    
bool_check, direction_dct = check_all_directions_step(fixed_var_names + re_var_names, variables, result, mixed=True, return_var_names=True)

while not bool_check:
    varnames_wrong_direction = [k for k, v in direction_dct.items() if not v]

    fixed_var_names = [col for col in fixed_var_names if col != varnames_wrong_direction[0]]
    re_var_names = [col for col in re_var_names if col != varnames_wrong_direction[0]]

    try:
        result, one_model = model.fit_mixed_model(
            fixed_var_names + re_var_names,
            re_var_names,
            cov_type,
        )
        bool_check, direction_dct = check_all_directions_step(fixed_var_names + re_var_names, variables, result, mixed=True, return_var_names=True)
    except LinAlgError:
        print("Failed to fit the model with the updated variables")

This is the part in model_resources.py script

function to fit a model from a dataframe - returns fitted model

def fit_mixed_model(self, fixed_variables, random_variables, cov_type):  #####
    X_fixed = self.X.loc[:, fixed_variables]
    if self.intercept_:
        X_fixed['intercept'] = 1

    X_random = self.X.loc[:, random_variables]
    ## if intercept is true - add intercept to random effects
    if self.intercept_:
        X_random['intercept'] = 1
    
    
    
    
    # Check if there are any random variables
    #if len(random_variables) == 0:   ############################################################
       # raise ValueError("No variable with random effect came into the model.") #######################KEEEEEEEEE S
    ### if there are no RE variables - set X_random to None
    
    if len(random_variables) == 0:
        X_random = None

    ## if independent_vars is set to true - we're treating the media variables as independent - 
    ## see https://www.statsmodels.org/stable/generated/statsmodels.regression.mixed_linear_model.MixedLM.html#statsmodels.regression.mixed_linear_model.MixedLM
    if (self.independent_vars_ )& (len(random_variables) >= 1):
        ### create matrix of zeros if we are treating media variables as independent 
        free = sm.regression.mixed_linear_model.MixedLMParams.from_components(
                     fe_params=np.ones(X_fixed.shape[1]),
                     cov_re=np.eye(X_random.shape[1]))
        model = sm.MixedLM(endog=self.y, exog=X_fixed, groups=self.dmas, exog_re=X_random)

        result = model.fit(free=free, method=['lbfgs'])
    else:
        #result = model.fit(method=["powell", "lbfgs"])
        model = sm.MixedLM(endog=self.y, exog=X_fixed, groups = self.dmas, exog_re = X_random)
        # result = model.fit(free=free, method=['lbfgs'])
        result = model.fit(method=['lbfgs'])
        
    residuals = get_mm_residuals(result, model, self.y)
    r2 = calculate_r2_resid(residuals, self.y)
    aic = calculate_aic(residuals, result)
    if self.metric_type == 'aic':
        result.metric = aic
    elif self.metric_type == 'r2':
        ### making r2 negative for consistency 
        result.metric = r2 * -1
    ### making r2 negative for consistency 
    result.r2 = r2 * -1
    result.aic = aic
    model.aic = aic
    model.r2 = r2 *-1
    
    ### Store cov_type as an instance variable
    self.cov_type = cov_type   ########3 Ken
    
    
    ### track total fit count 
    self.fit_count += 1

    return result, model

When i run the model_execution.py script i get this error below: ValueError: Cannot predict random effects from singular covariance structure.

Please i need help in other to fix this error message so that my script can run successfully

Thanks

0

There are 0 answers