How to get function from lm object?

View: New views
4 Messages — Rating Filter:   Alert me  

How to get function from lm object?

by Alix :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Dear experts,

I am trying to obtain a function from a model, so that I could further manipulate it, plot it, etc. I can get model estimates and  manually construct a function, but this gets tedious when trying out different functions to fit the data. There must be a better way of doing it, no?

x <- c(1:10)
y <- c(1:10)

fit <- lm(y ~ x)
f <- function(x){fit$coef[1] + fit$coef[2]*x}  # Manually constructing function
                                               # Would be nice to do something like this:
                                               # f<-getFunction(fit)
plot(f, 0, 10)

Thanks,
  Alex Stolpovsky



-----------------------------------------
This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law.  If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED.  Although this transmission and
any attachments are believed to be free of any virus or other
defect that might affect any computer system into which it is
received and opened, it is the responsibility of the recipient to
ensure that it is virus free and no responsibility is accepted by
JPMorgan Chase & Co., its subsidiaries and affiliates, as
applicable, for any loss or damage arising in any way from its use.
 If you received this transmission in error, please immediately
contact the sender and destroy the material in its entirety,
whether in electronic or hard copy format. Thank you.
        [[alternative HTML version deleted]]

______________________________________________
R-help@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Re: How to get function from lm object?

by Marc Schwartz-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Jul 9, 2009, at 6:18 PM, Alexander V Stolpovsky wrote:

> Dear experts,
>
> I am trying to obtain a function from a model, so that I could  
> further manipulate it, plot it, etc. I can get model estimates and  
> manually construct a function, but this gets tedious when trying out  
> different functions to fit the data. There must be a better way of  
> doing it, no?
>
> x <- c(1:10)
> y <- c(1:10)
>
> fit <- lm(y ~ x)
> f <- function(x){fit$coef[1] + fit$coef[2]*x}  # Manually  
> constructing function
>                                               # Would be nice to do  
> something like this:
>                                               # f<-getFunction(fit)
> plot(f, 0, 10)
>
> Thanks,
>  Alex Stolpovsky

If you want to get the model fitted y values, just use:

   fitted(fit)

As an aside, to get the model coefficients, there is also:

   coef(fit)

and

   coef(summary(fit))

which like fitted(), is one of several 'extractor' functions that can  
be used on model object to get specific components.

If you want to generate model predicted y values based upon 'new' x  
values, use:

   newdata <- data.frame(x = YourNewValues)
   predict(fit, newdata = newdata)

Note that the 'newdata' data frame must contain columns with the SAME  
names as the independent variables in your original model.

See ?predict.lm for more information, which can also generate various  
intervals, etc.

If you want to plot your original data in a scatterplot and then add  
the model fitted line, use:

   plot(x, y)
   abline(fit)

See ?abline for more information there.

BTW, much of this is covered in An Introduction to R, which is  
included in your R installation and on the main R web site under  
Manuals link.

HTH,

Marc Schwartz

______________________________________________
R-help@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Re: How to get function from lm object?

by Alix :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you much for the useful pointers. Turns out, I can get what I want with predict(fit, newdata = newdata). But for the record, there is no way to get the fit function, from lm object, is there?
  Alex


-----------------------------------------
This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law.  If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED.  Although this transmission and
any attachments are believed to be free of any virus or other
defect that might affect any computer system into which it is
received and opened, it is the responsibility of the recipient to
ensure that it is virus free and no responsibility is accepted by
JPMorgan Chase & Co., its subsidiaries and affiliates, as
applicable, for any loss or damage arising in any way from its use.
 If you received this transmission in error, please immediately
contact the sender and destroy the material in its entirety,
whether in electronic or hard copy format. Thank you.

______________________________________________
R-help@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Re: How to get function from lm object?

by Marc Schwartz-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Jul 9, 2009, at 8:20 PM, Alexander V Stolpovsky wrote:

> Thank you much for the useful pointers. Turns out, I can get what I  
> want with predict(fit, newdata = newdata). But for the record, there  
> is no way to get the fit function, from lm object, is there?
>  Alex

Alex,

There is no function returned within the model object.

You can review the structure of the returned model object by using:

   str(fit)

HTH,

Marc Schwartz

______________________________________________
R-help@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.