I want to make this code into a function with a for loop so that I can plug a variable into the function (where "age" is in this code) and get an ANOVA table out that compares the fit of each polynomial. I would like the fits examined to go up to fit_10.
fit_1 = lm(wage~age, data = Wage)
fit_2 = lm(wage~poly(age,2), data = Wage)
fit_3 = lm(wage~poly(age,3), data = Wage)
fit_4 = lm(wage~poly(age,4), data = Wage)
fit_5 = lm(wage~poly(age,5), data = Wage)
print(anova(fit_1,fit_2,fit_3,fit_4,fit_5))
Any help appreciated on this basic question!