Create a function "automatically" from lm formula and coefficients?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Create a function "automatically" from lm formula and coefficients?

Keith S Weintraub
I hope the subject says it all.

I want to be able to use an lm object and the associated coefficients to create  function that can produce "expected" "y" values given inputs.

Thanks,
KW

--


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Create a function "automatically" from lm formula and coefficients?

Frank Harrell
require(rms)
f <- ols(y ~ rcs(age,4)*sex + blood.pressure + rcs(height,5))
latex(f)   # typeset algebraic form of model
Function(f)   # create an R function that computes Y hat

Frank
Frank Harrell
Department of Biostatistics, Vanderbilt University
Reply | Threaded
Open this post in threaded view
|

Re : Create a function "automatically" from lm formula and coefficients?

Pascal Oettli-2
In reply to this post by Keith S Weintraub
Hi Keith,

Do you mean as "predict.lm" can do?

Regards,
Pascal


________________________________
De : Keith Weintraub <[hidden email]>
À : [hidden email]
Envoyé le : Jeudi 1 mars 2012 11h41
Objet : [R] Create a function "automatically" from lm formula and coefficients?

I hope the subject says it all.

I want to be able to use an lm object and the associated coefficients to create  function that can produce "expected" "y" values given inputs.

Thanks,
KW

--


    [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Re : Create a function "automatically" from lm formula and coefficients?

Frank Harrell
yes, e.g.
require(rms)
f <- ols( ) # ols is a wrapper for lm
g <- Function(f)
g(age=30) # get predicted mean at age=30 and defaults for other variables (medians & modes)
Frank


Pascal Oettli-2 wrote
Hi Keith,

Do you mean as "predict.lm" can do?

Regards,
Pascal


________________________________
De : Keith Weintraub <[hidden email]>
À : [hidden email] 
Envoyé le : Jeudi 1 mars 2012 11h41
Objet : [R] Create a function "automatically" from lm formula and coefficients?

I hope the subject says it all.

I want to be able to use an lm object and the associated coefficients to create  function that can produce "expected" "y" values given inputs.

Thanks,
KW

--


    [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Frank Harrell
Department of Biostatistics, Vanderbilt University
Reply | Threaded
Open this post in threaded view
|

Re: Create a function "automatically" from lm formula and coefficients?

taheri
In reply to this post by Frank Harrell
Hi,
I need to create a function from lm formula and coefficients to use it in my c++ code later.
but when I do it as you said
"require(rms)
f <- ols( )
g <- Function(f)
g(x1=2,x2=3,...)
I realize that it didn't give me the same result as predict.lm?
how can I reach a function to give me the same results in predict?
I really need it, please answer me.

Regards,
Taheri