[R] code optimization

Thomas Lumley tlumley at u.washington.edu
Mon Apr 29 18:08:31 CEST 2002


On Mon, 24 Jun 2002, Peter B. Mandeville wrote:

> I have a function "eval.delta" which does what I want but isn't very
> elegant. I have consulted the R documents, MASS, and S Programming. Is
> there a practical way to optimize the code? Thank you very much.
>
<snip>
> Function Call and Output:
>
>  > eval.delta(delta)
> VALUES
> <= 2 (no credible evidence)      4       0 1.4 2 2
>  > 2 y <= 4 (weak evidence)       2       2.3 2.3
>  > 4 y <= 7 (definite evidence)   2       4.5 6.4
>  > 7 y <= 10 (strong evidence)    2       8.9 7.4
>  > 10 (very strong evidence)      2       12.4 11.5
>
> MODELS
> <= 2 (no credible evidence)      4       1 2 11 12
>  > 2 y <= 4 (weak evidence)       2       3 5
>  > 4 y <= 7 (definite evidence)   2       4 8
>  > 7 y <= 10 (strong evidence)    2       6 9
>  > 10 (very strong evidence)      2       7 10
>

You can get something like this, but not quite the same, fairly easily:
   labels<-c("<= 2  (no credible evidence)",
             " 2-4  (weak evidence) ",
             " 4-7  (definite evidence)",
             " 7-10 (strong evidence)",
              ">10  (very strong evidence)")
    classes<-cut(delta,c(0,2,4,7,10,Inf),include.lowest=TRUE,labels=labels)
    models<-by(1:length(delta),list(level=classes),
		function(x) c(n=length(x),x))
    values<-by(1:length(delta),list(level=classes),
                function(x) c(n=length(x),x))


So, for example, models prints as
> models
level: <= 2  (no credible evidence)
 n
 4  1  2 11 12
------------------------------------------------------------
level:  2-4  (weak evidence)
n
2 3 5
------------------------------------------------------------
level:  4-7  (definite evidence)
n
2 4 8
------------------------------------------------------------
level:  7-10 (strong evidence)
n
2 6 9
------------------------------------------------------------
level: >10  (very strong evidence)
 n
 2  7 10



	-thomas


Thomas Lumley			Asst. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list