I have reviewed the following similar questions and answers but believe my situation is different enough to warrant another question.
Getting Warning: " 'newdata' had 1 row but variables found have 32 rows" on predict.lm in R
R Warning: newdata' had 15 rows but variables found have 22 rows
Warning message 'newdata' had 1 row but variables found have 16 rows in R
warning when calculating predicted values
Trouble using predict with linear model in R
Predict.lm in R fails to recognize newdata
The last question listed has a great answer from Joran that gets to the heart of the naming convention between what was modeled and what is being scored.
The model I am fitting is a polynomial which generates some naming problems.
mdl <- lm(val ~ poly(grp,2), data = mRetCurv)
model.frame(mdl)
Generates the following output:
val poly(grp, 2).1 poly(grp, 2).2
1 39.54227 -0.290170670 0.374017601
2 48.68225 -0.272368788 0.308602552
Note the name of my predictor variables. If I call
predict.lm(mdl, newdata = apl$grp)
I get the standard warning as the variable grp != poly(grp, 2).1 or poly(grp, 2).2 as far as predict.lm is concerned. I tried making a duplicate column of grp and renaming the two to match the model.frame but R doesn't like "poly(grp, 2).1" as a column name. Nor is this a data efficient solution replicating a column when I apply it to many rows.
Any help is appreciated.
Thank you