I'd like to have or write a function that automatically provides "adjusted predictions" for a linear model. For simplicity I could accept that the models must not have any interactions. With "adjusted predictions" I mean that the predictions are for the values of a specific predictor ("target predictor") are calculated with all factors at the reference levels and all metric predictors at their mean values (these cal be determined from model frame).
For this task the function has to find a sensible range for the continuous target predictors. This is relatively simple when the predictors are untransformed. However, I have not found any solution for transformed predictors.
Example:
consider
model = lm(Y ~ A + log(B) + exp(C) + sqrt(D) + poly(E,3))
I will not consider other transformations (like I(.)). The given model is just an example. It could look different, with different variable names and transformations.
To get the adjusted predictions for a target predictor the desired function should be called like
predictions = calcPrediction(model, "C")
to the fitted values varying the values of the variable "C" while all other variables (A,B,D,E) are fixed.
My problem is to set up (automatically, given only the model passed as argument to the function) the required data.frame "newdata" for lm.predict. This data.frame must have the colnames "A","B","C"...= the names of the variables. How can I extract the variable names from the model? The names I can extract all contain the transformations/functions.
The second problem is to find an appropriate range for the target predictor. model$model contains the values used by lm, and they are transformed. To determine the range for newdata I need the untransformed values. How can the applied transformation be undone (automatically)?
I hope this was understandable...
Thanks in advance for any help!