Hi All, I am trying to generate a function from a linear model. I think there should be build-in function that perform this action but I've had no luck finding it. For example, I have a model created using lm(). model <- lm(sat.d~1+sat.n+I(sat.n^2)) What I would like to have is a function (similar to the one generated by splinefun()) so that I can use it on different data-sets. Thanks in advance for the help. Tony Han Bao tonybao at mac.com
I don't think there's an automatic way to do this but you might try something like: model <- lm(sat.d~1+sat.n+I(sat.n^2)) f <- function(x) { predict(model, data.frame(sat.n = x)) } -roger Tony Han Bao wrote:> Hi All, > > I am trying to generate a function from a linear model. I think there > should be build-in function that perform this action but I've had no > luck finding it. > > For example, I have a model created using lm(). > > model <- lm(sat.d~1+sat.n+I(sat.n^2)) > > What I would like to have is a function (similar to the one generated by > splinefun()) so that I can use it on different data-sets. > > Thanks in advance for the help. > > Tony Han Bao > tonybao at mac.com > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >-- Roger D. Peng http://www.biostat.jhsph.edu/~rpeng/
predict() can do that for you without giving you the explicit form of the prediction function. I believe Prof. Harrell has facilities in his Design/Hmisc packages for producing functions from fitted models. Andy> From: Tony Han Bao > > Hi All, > > I am trying to generate a function from a linear model. I think there > should be build-in function that perform this action but I've had no > luck finding it. > > For example, I have a model created using lm(). > > model <- lm(sat.d~1+sat.n+I(sat.n^2)) > > What I would like to have is a function (similar to the one generated > by splinefun()) so that I can use it on different data-sets. > > Thanks in advance for the help. > > Tony Han Bao > tonybao at mac.com > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >