0

I am writing a loop that would output a formula used for regression - I want to test which pair of variables would give the best fit. I am expecting the following output:

ClaimNb ~ C(DrivAgeGroup)+ C(BMGroup)

ClaimNb ~ C(DrivAgeGroup)+ C(Area)

ClaimNb ~ C(BMGroup)+ C(Area)

But instead I am getting:

ClaimNb ~ C(DrivAgeGroup)+ C(BMGroup)

ClaimNb ~ C(DrivAgeGroup)

Why is that? Thanks.

remaining = ['C(Area)','C(DrivAgeGroup)','C(BMGroup)']
for candidate in remaining:            
    fitted_var = remaining
    fitted_var.remove(candidate)
    formula = "{} ~ {} ".format('ClaimNb', '+  '.join(fitted_var))
    print(formula)
lostwanderer
  • 143
  • 1
  • 1
  • 7
  • You are altering the list while looping over it. This does not make a copy `fitted_var = remaining` it's just a reference to the same list. – Mark Dec 24 '21 at 01:52
  • Thanks - but how can I restore fitted_var to the full "remaining list"? – lostwanderer Dec 24 '21 at 01:54

0 Answers0