python - To create loop that lets my regression formula include one variable each tome -
i've been working on stepwise regression. i'll find loop lets regression formula include 1 variable each time, i'm having trouble loop. code follows:
def forward_selected(data, response): remaining = set(data.columns) remaining.remove(response) selected = [] current_aic, best_new_aic = 0.0, 0.0 while remaining , current_aic == best_new_aic: aics_with_candidates = [] candidate in remaining: formula = "{} ~ {} + 1".format(response,' + '.join(selected + [candidate])) aic = smf.ols(formula, data).fit().aic aics_with_candidates.append((aic, candidate)) aics_with_candidates.sort(reverse=true) best_new_score, best_candidate = aics_with_candidates.pop() if current_aic < best_new_aic: remaining.remove(best_candidate) selected.append(best_candidate) current_aic = best_new_aic formula = "{} ~ {} + 1".format(response, ' + '.join(selected)) model = smf.ols(formula, data).fit() return model
the problem result formula this: y~x+1
. it's selecting best x
(lowest aic) y~x+1
, not formula: y~x1+x2+..+1
lowest aic.
Comments
Post a Comment