[R] simplification of code using stamp?

Rainer M Krug RKrug at sun.ac.za
Wed Oct 25 11:22:12 CEST 2006


Hi

I have the following code which I would like to simplify. Id does linear 
regressions and returns the r-squares, and the coefficients.
It runs slow, as it is doing the regressions for each - is it possible 
to get the values in a dataframe which looks as follow:

expert | xx | seeds | r.squared | slope | intercept

Thanks in advance,

Rainer


library(reshape)
rsqs <- as.data.frame(
                       stamp(
                             tc.long,
                             expert * xx * seeds ~ .,
                             function(df) try( summary( lm(distance ~ 
generation, data=df))$r.squared, silent=TRUE )
                             )
                       )

slope <- as.data.frame(
                        stamp(
                              tc.long,
                              expert * xx * seeds ~ .,
                              function(df) try( summary( lm(distance ~ 
generation, data=df))$coefficients[2], silent=TRUE )
                              )
                        )

d.slope <- as.data.frame(
                          stamp(
                                tc.long,
                                expert * xx * seeds ~ .,
                                function(df) try( summary( lm(distance ~ 
generation, data=df))$coefficients[4], silent=TRUE )
                                )
                          )

intercept <- as.data.frame(
                            stamp(
                                  tc.long,
                                  expert * xx * seeds ~ .,
                                  function(df) try( summary( lm(distance 
~ generation, data=df))$coefficients[1], silent=TRUE )
                                  )
                        )

d.intercept <- as.data.frame(
                              stamp(
                                    tc.long,
                                    expert * xx * seeds ~ .,
                                    function(df) try( summary( 
lm(distance ~ generation, data=df))$coefficients[3], silent=TRUE )
                                    )
                              )

-- 
Rainer M. Krug, Dipl. Phys. (Germany), MSc Conservation
Biology (UCT)

Department of Conservation Ecology and Entomology
University of Stellenbosch
Matieland 7602
South Africa

Tel:		+27 - (0)72 808 2975 (w)
Fax:		+27 - (0)21 808 3304
Cell:		+27 - (0)83 9479 042

email:	RKrug at sun.ac.za
       	Rainer at krugs.de



More information about the R-help mailing list