library(AER) library(MASS) data(Boston) # find the optimal polynomial order of the polylog model # extract the R^2 from the selected model and assign it to R2 # find the optimal polynomial order of the polylog model for(i in 4:1){ mod <- lm(medv ~ poly(log(lstat), i, raw = T), data = Boston) pval <- coeftest(mod, vcov = vcovHC)[(i+1), 4] if(pval < 0.05){ print(i) break } } # extract the R^2 from the selected model and assign it to R2 R2 <- summary(mod)$r.squared ex() %>% check_for() ex() %>% check_object("R2") %>% check_equal() success_msg("Correct! Using the sequential testing approach we get an optimal order of r=2.")