Re: [R] simplification of code using stamp?
hadley wickham wrote: I get the following, which is not what I am looking for. test[1:10,] expertxx seeds runvalue 1 BW x001025 1 rsqs, slope, d.slope, intercept, d.intercept 2 BW x001025 2 rsqs, slope, d.slope, intercept, d.intercept 3 BW x001025 3 rsqs, slope, d.slope, intercept, d.intercept 4 BW x001025 4 rsqs, slope, d.slope, intercept, d.intercept 5 BW x001025 5 rsqs, slope, d.slope, intercept, d.intercept 6 BW x001028 1 rsqs, slope, d.slope, intercept, d.intercept 7 BW x001028 2 rsqs, slope, d.slope, intercept, d.intercept 8 BW x001028 3 rsqs, slope, d.slope, intercept, d.intercept 9 BW x001028 4 rsqs, slope, d.slope, intercept, d.intercept 10 BW x001028 5 rsqs, slope, d.slope, intercept, d.intercept That's because stamp doesn't produce great output at the moment (have a look at the str(test) to see that you have a data frame containing a list of vectors) The following code should put it a more reasonable form: tidy - function(x) { bind - function(i) data.frame(x[i, -ncol(x),drop=FALSE], t(a$value[[i]])) l - lapply(1:nrow(x), bind) do.call(rbind.fill, l) } tidy(test) Thanks Hadley - I'll look at the code. (but unless you provide a reproducible example I can't be sure that it works with your data) Regards, Hadley -- 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: [EMAIL PROTECTED] [EMAIL PROTECTED] __ R-help@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.
[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: [EMAIL PROTECTED] [EMAIL PROTECTED] __ R-help@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.
Re: [R] simplification of code using stamp?
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 [EMAIL PROTECTED] 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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Namens Rainer M Krug Verzonden: woensdag 25 oktober 2006 11:22 Aan: r-help@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: [EMAIL PROTECTED] [EMAIL PROTECTED] __ R-help@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. __ R-help@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.
Re: [R] simplification of code using stamp?
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), } 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 [EMAIL PROTECTED] 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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Namens ONKELINX, Thierry Verzonden: woensdag 25 oktober 2006 11:36 Aan: [EMAIL PROTECTED]; r-help@stat.math.ethz.ch Onderwerp: Re: [R] simplification of code using stamp? 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 [EMAIL PROTECTED] 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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Namens Rainer M Krug Verzonden: woensdag 25 oktober 2006 11:22 Aan: r-help@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: [EMAIL PROTECTED] [EMAIL PROTECTED
Re: [R] simplification of code using stamp?
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,] expertxx seeds runvalue 1 BW x001025 1 rsqs, slope, d.slope, intercept, d.intercept 2 BW x001025 2 rsqs, slope, d.slope, intercept, d.intercept 3 BW x001025 3 rsqs, slope, d.slope, intercept, d.intercept 4 BW x001025 4 rsqs, slope, d.slope, intercept, d.intercept 5 BW x001025 5 rsqs, slope, d.slope, intercept, d.intercept 6 BW x001028 1 rsqs, slope, d.slope, intercept, d.intercept 7 BW x001028 2 rsqs, slope, d.slope, intercept, d.intercept 8 BW x001028 3 rsqs, slope, d.slope, intercept, d.intercept 9 BW x001028 4 rsqs, slope, d.slope, intercept, d.intercept 10 BW x001028 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 [EMAIL PROTECTED] 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: [EMAIL PROTECTED] [EMAIL PROTECTED] __ R-help@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.
Re: [R] simplification of code using stamp?
I get the following, which is not what I am looking for. test[1:10,] expertxx seeds runvalue 1 BW x001025 1 rsqs, slope, d.slope, intercept, d.intercept 2 BW x001025 2 rsqs, slope, d.slope, intercept, d.intercept 3 BW x001025 3 rsqs, slope, d.slope, intercept, d.intercept 4 BW x001025 4 rsqs, slope, d.slope, intercept, d.intercept 5 BW x001025 5 rsqs, slope, d.slope, intercept, d.intercept 6 BW x001028 1 rsqs, slope, d.slope, intercept, d.intercept 7 BW x001028 2 rsqs, slope, d.slope, intercept, d.intercept 8 BW x001028 3 rsqs, slope, d.slope, intercept, d.intercept 9 BW x001028 4 rsqs, slope, d.slope, intercept, d.intercept 10 BW x001028 5 rsqs, slope, d.slope, intercept, d.intercept That's because stamp doesn't produce great output at the moment (have a look at the str(test) to see that you have a data frame containing a list of vectors) The following code should put it a more reasonable form: tidy - function(x) { bind - function(i) data.frame(x[i, -ncol(x),drop=FALSE], t(a$value[[i]])) l - lapply(1:nrow(x), bind) do.call(rbind.fill, l) } tidy(test) (but unless you provide a reproducible example I can't be sure that it works with your data) Regards, Hadley __ R-help@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.