[R] simplification of code using stamp?

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Wed Oct 25 11:35:37 CEST 2006


I think your script is slow because it has to recalculate the same model
five times. I've tried to avoid this by rewriting your function(df).

function(df){
	fit <- summary(lm(distance ~ generation, data=df))
	result <- c(fit$$r.squared, $coefficients[2], $coefficients[4],
$coefficients[1], $coefficients[3])
	names(result) <- c("rsqs", "slope", "d.slope", "intercept",
"d.intercept"),
}

Cheers,

Thierry
------------------------------------------------------------------------
----

ir. Thierry Onkelinx

Instituut voor natuur- en bosonderzoek / Reseach Institute for Nature
and Forest

Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance

Gaverstraat 4

9500 Geraardsbergen

Belgium

tel. + 32 54/436 185

Thierry.Onkelinx op inbo.be

www.inbo.be 

 

Do not put your faith in what statistics say until you have carefully
considered what they do not say.  ~William W. Watt

A statistical analysis, properly conducted, is a delicate dissection of
uncertainties, a surgery of suppositions. ~M.J.Moroney

-----Oorspronkelijk bericht-----
Van: r-help-bounces op stat.math.ethz.ch
[mailto:r-help-bounces op stat.math.ethz.ch] Namens Rainer M Krug
Verzonden: woensdag 25 oktober 2006 11:22
Aan: r-help op stat.math.ethz.ch
Onderwerp: [R] simplification of code using stamp?

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 op sun.ac.za
       	Rainer op krugs.de

______________________________________________
R-help op stat.math.ethz.ch 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.



More information about the R-help mailing list