Greetings UseRs, Pls advise if there is a way to write a func that can be supplied to aggregate to compute weighted MeanAbsolute Dev (MAD). I am having trouble passing the correct weights from each group level and cannot see the code behind aggregate. But maybe 'aggregate' is not the best way to do that.> m1 <- aggregate(pool[,c("SMM")],by=list(time=pool$ym),weighted.mean,w=pool$wght)Error in weighted.mean.default(X[[1L]], ...) : 'x' and 'w' must have the same length Apparently the grouping does not work on the additional argument. I am using weighted mean here just to be explicit and avoid supplying a custom function gor weighted MAD, which is not difficult to write by itself. It's making it work with aggreagte that is the problem.> aggregatefunction (x, ...) UseMethod("aggregate") <environment: namespace:stats> Does not show anything... Stephen B
You could try something like this. library(plyr) ddply(pool, .(ym), function(z){ weighted.mean(x= z$SMM, w = z$wght) }) ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance Kliniekstraat 25 1070 Anderlecht Belgium + 32 2 525 02 51 + 32 54 43 61 85 Thierry.Onkelinx at inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens Bond, Stephen Verzonden: dinsdag 7 februari 2012 15:55 Aan: r-help at r-project.org Onderwerp: [R] Weighted mad Greetings UseRs, Pls advise if there is a way to write a func that can be supplied to aggregate to compute weighted MeanAbsolute Dev (MAD). I am having trouble passing the correct weights from each group level and cannot see the code behind aggregate. But maybe 'aggregate' is not the best way to do that.> m1 <- aggregate(pool[,c("SMM")],by=list(time=pool$ym),weighted.mean,w=pool$wght)Error in weighted.mean.default(X[[1L]], ...) : 'x' and 'w' must have the same length Apparently the grouping does not work on the additional argument. I am using weighted mean here just to be explicit and avoid supplying a custom function gor weighted MAD, which is not difficult to write by itself. It's making it work with aggreagte that is the problem.> aggregatefunction (x, ...) UseMethod("aggregate") <environment: namespace:stats> Does not show anything... Stephen B ______________________________________________ R-help at r-project.org 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.
Dear Stephen The names of methods of aggregate can be viewed with the methods argument. Typing aggregate.data.frame shows you the method used for data frames.> methods(aggregate)[1] aggregate.data.frame aggregate.default aggregate.formula* [4] aggregate.ts Non-visible functions are asterisked However, you need the function by, which passes a data frame to the function.> pool <- data.frame(SMM = rnorm(10), ym = rep(1:2, each = 5), wght = 1) > poolSMM ym wght 1 0.904640493 1 1 2 1.021857174 1 1 3 1.234153020 1 1 4 -0.697625918 1 1 5 0.073146470 1 1 6 1.438553786 2 1 7 -0.831183333 2 1 8 -0.055872825 2 1 9 0.858622161 2 1 10 0.001968816 2 1> mad <- by(pool, pool$ym,+ function(pool) { weighted.mean(x = pool$SMM, w = pool$wght) })> mad[1:2]pool$ym 1 2 0.5072342 0.2824177 Regards, Chris Campbell MANGO SOLUTIONS Data Analysis that Delivers +44 1249 767700 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Bond, Stephen Sent: 07 February 2012 14:55 To: r-help at r-project.org Subject: [R] Weighted mad Greetings UseRs, Pls advise if there is a way to write a func that can be supplied to aggregate to compute weighted MeanAbsolute Dev (MAD). I am having trouble passing the correct weights from each group level and cannot see the code behind aggregate. But maybe 'aggregate' is not the best way to do that.> m1 <- aggregate(pool[,c("SMM")],by=list(time=pool$ym),weighted.mean,w=pool$wght)Error in weighted.mean.default(X[[1L]], ...) : 'x' and 'w' must have the same length Apparently the grouping does not work on the additional argument. I am using weighted mean here just to be explicit and avoid supplying a custom function gor weighted MAD, which is not difficult to write by itself. It's making it work with aggreagte that is the problem.> aggregatefunction (x, ...) UseMethod("aggregate") <environment: namespace:stats> Does not show anything... Stephen B ______________________________________________ R-help at r-project.org 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. Kind Regards Chris Campbell [Mango Solutions] T:+44 (0)1249 767700 F: +44 (0)1249 767707 Mango Solutions Website<http://www.mango-solutions.com> Unit 2 Greenways Business Park Bellinger Close Chippenham Wilts SN15 1BN UK ________________________________ With effect from Friday 24th February 2012 Mango Business Solutions Ltd main office will be moving to: 2 Methuen Park Chippenham Wilts SN14 0GB UK Please note, there may be some disruption to services; including emails, from 1730 hrs on Thursday 23rd February. For any urgent enquiries, please use the following mobile numbers: 07966062462, 07900580808, 07967808091 ________________________________ LEGAL NOTICE This message is intended for the use o...{{dropped:10}}