How to test for overfit in lm() regression?

For a variable x, I find that coefficients of x, x^2, x^3 are all significant in lm(). But I fear I might be overfitting the data when using higher-order polynomials.

Is there an established process to test for overfitting? Are there any R packages to do the same?

My answer will begin with assuming you don't really know cross validation.

Do cross validation, there are many ways of doing so. The simplest form is to split your data into training and testing set, let's say with 80:20 proportion. Train your model using the 80% and let it predict the unknown 20%.

If prediction on the unknown 20% (testing set) is far worse/inaccurate than prediction on the 80% (training set), then you know your model is unable to generalize (it overfits).

Do this procedure for each of model specification you want to assess, and see how they compare to one another. In your case, you may want to try adding terms sequentially starting from $$x$$, then $$x^2$$, then $$x^3$$, and so on.

You can take this further and split your data into five "fold"s. Do the above procedure five times, each using different fold of data as the testing set.

Rather than doing this procedure manually, there are packages in R that can do it for you. Package caret comes to mind, although there may be other packages.

However, as far as I know, there is no procedure to automatically determine whether overfitting happens in an already estimated model.

Train and test your model using Cross-Validation. If you overfit your Cross-validation error will be a lot higher than your training error.

That is, split your data in say 5 random folds.

Fit your model to 4 of the folds and use the last one to test on, by calculating your prediction error. Do this 5 times using each fold as test fold once and average your prediction error.

Then test each model (using only x, only x and x^2 and then using all three). If they overfit your prediction error will be higher.