[R] simplification of code using stamp?

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


ONKELINX, Thierry wrote:
> Resending the function because of a typo in the result vector.
> 
> function(df){
> 	fit <- summary(lm(distance ~ generation, data=df))
> 	result <- c(fit$r.squared, fit$coefficients[2],
> fit$coefficients[4],
> fit$coefficients[1], fit$coefficients[3])
> 	names(result) <- c("rsqs", "slope", "d.slope", "intercept",
> "d.intercept"),
> }

You are right - the problem is that it isd calculating the regressions 
five time. I implemented your code, and when I print the resulting 
data.frame from

test <- as.data.frame(
                       stamp(
                             tc.long,
                             expert * xx * seeds * run ~ .,
                             function(df)
                             {
                               try(
                                   {
                                     fit <- summary( lm(distance ~ 
generation, data=df) )
                                     result <- c(fit$r.squared, 
fit$coefficients[2], fit$coefficients[4], fit$coefficients[1], 
fit$coefficients[3])
                                     names(result) <- c("rsqs", "slope", 
"d.slope", "intercept", "d.intercept")
                                   },
                                   silent=TRUE
                                   )
                             }
                             )
                       )

I get the following, which is not what I am looking for.

 > test[1:10,]
    expert    xx seeds run                                        value
1      BW x0010    25   1 rsqs, slope, d.slope, intercept, d.intercept
2      BW x0010    25   2 rsqs, slope, d.slope, intercept, d.intercept
3      BW x0010    25   3 rsqs, slope, d.slope, intercept, d.intercept
4      BW x0010    25   4 rsqs, slope, d.slope, intercept, d.intercept
5      BW x0010    25   5 rsqs, slope, d.slope, intercept, d.intercept
6      BW x0010    28   1 rsqs, slope, d.slope, intercept, d.intercept
7      BW x0010    28   2 rsqs, slope, d.slope, intercept, d.intercept
8      BW x0010    28   3 rsqs, slope, d.slope, intercept, d.intercept
9      BW x0010    28   4 rsqs, slope, d.slope, intercept, d.intercept
10     BW x0010    28   5 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 at 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
> 
> 
-- 
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