liang.che at us.pwc.com
2012-Oct-09 15:03 UTC
[R] How to write out this regression equation in R?
For example: How to make R write out: Balance = 2 + 3 * IntGDP + 5 * IntUnemployment + 0.3 * d1 from the table below: Balance Intercept IntGDP GDPNum IntUnemployment IntInflation d1 d2 d3 30000 2 3 5 0.3 0 0 and if I have 20 rows, how to make it a batch process? thanks ______________________________________________________________________ The information transmitted, including any attachments, is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited, and all liability arising therefrom is disclaimed. If you received this in error, please contact the sender and delete the material from any computer. PricewaterhouseCoopers LLP is a Delaware limited liability partnership. This communication may come from PricewaterhouseCoopers LLP or one of its subsidiaries. [[alternative HTML version deleted]]
> How to make R write out: > > Balance = 2 + 3 * IntGDP + 5 * IntUnemployment + 0.3 * d1 > > from the table below: > > Balance Intercept IntGDP GDPNum IntUnemployment > IntInflation d1 d2 d3 > 30000 2 3 5 > 0.3 0 0Maybe ?sprintf would help? And if you wrap that in a function that takes a vector, using apply() on the table would give you one string per row, ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
But sprintf is itself vectorized. If you give it vectors, it returns vectors. So you could obtain that apply-result more efficiently by passing a bunch of column vectors of data. There happens to be a convenient object called a data frame that holds a bunch of similar-length vectors. DF <- data.frame( m=c(1,2), b=c(-3,4) ) result <- sprintf( "y=(%d)*x+(%d)", DF$m, DF$b) cat(paste(result, collapse="\n")) --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. S Ellison <S.Ellison at LGCGroup.com> wrote:> > >> How to make R write out: >> >> Balance = 2 + 3 * IntGDP + 5 * IntUnemployment + 0.3 * d1 >> >> from the table below: >> >> Balance Intercept IntGDP GDPNum IntUnemployment >> IntInflation d1 d2 d3 >> 30000 2 3 5 >> 0.3 0 0 > > >Maybe ?sprintf would help? > >And if you wrap that in a function that takes a vector, using apply() >on the table would give you one string per row, > >******************************************************************* >This email and any attachments are confidential. Any >use...{{dropped:8}} > >______________________________________________ >R-help at r-project.org 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.