[R] Weighted mad

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Tue Feb 7 16:31:06 CET 2012


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 op 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 op r-project.org [mailto:r-help-bounces op r-project.org] Namens Bond, Stephen
Verzonden: dinsdag 7 februari 2012 15:55
Aan: r-help op 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. 

> aggregate
function (x, ...) 
UseMethod("aggregate")
<environment: namespace:stats>

Does not show anything...

Stephen B

______________________________________________
R-help op 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.



More information about the R-help mailing list