rfunctionupdatesupdatemodel

R: Update model by changing modeling function


I have a gls model in R. I want to update it in two ways:

  1. Adding predictors

  2. Changing the function from gls to lm.

The update() function obviously works just fine with #1, but is there a way to update a model to change the fitting function itself???

For example: I want to change gls(y ~ x) to lm(y ~ x + z).

Note: The update approach is necessary b/c this process is going into a function that will have greatly different input models, so simply copying the model arguments seems difficult to implement.


Solution

  • It is possible, but not using any current form of the update function. This will require some more advanced programming and therefore is probably not for the new user of R.

    If you are comfortable with programming in R and some of the internals then read on, otherwise you may need to consult/hire an R programmer to help.

    You can see what the update function does by looking at the code for update.default. But that only changes the options and formula, not the actual function that will be called. Looking at the first part of the code to lm you can see an example of changing the calling function when lm uses match.call to get the lm call, then changes the first element to model.fram and then calls eval on that. You will need to write your own update function (call it something else so there is no confusion) that does the same thing as lm to change from gls to lm and then do the same thing as update.default to change the formula to add the additional predictor. then eval the result and you have the basics of what you want.