Skip to content
Back to formatted view

Raw Message

Message-ID: <E27308A9AE9C2C4289FE3F8C16B7581421FF2D68@CBMCC-X7-MBX02.ad.cibc.com>
Date: 2012-02-07T14:54:45Z
From: Bond, Stephen
Subject: 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