[R] weighting means

Frank E Harrell Jr fharrell at virginia.edu
Thu Aug 22 15:04:25 CEST 2002


On Thu, 22 Aug 2002 14:51:14 +0200
Oscar Rueda <orp at intras.es> wrote:

> Hello to all
> 
> Is there a way to get weighted means using summary.formula() from Hmisc?
> 
> For example, the following doesn't work:
> 
> >a<-c(rep("A",3),rep("B",7))
> >b<-c(1:10)
> >c<-c(rep("X",2),rep("Y",8))
> >w<-c(10:1)
> >z<-data.frame(a,b,c,w)
> >library(Hmisc)
> >summary(b~a+c,data=z,method="cross",fun=function(y)wtd.mean(y,weights=z$w))
> 
> Error in wtd.mean(y, weights = z$w) : dim<- length of dims do not match the length of object
> 
> Thanks for the replies,
> 
> Oscar Rueda

Not at present.  That would not be very hard to add to summary.formula and I will add that to my "to do" list.  -Frank
> 
> 
> 
> 22/08/02 1:16:15, Frank E Harrell Jr <fharrell at virginia.edu> escribi:
> 
> >On Wed, 21 Aug 2002 12:10:44 -0400
> >kjetil halvorsen <kjetilh at umsanet.edu.bo> wrote:
> >
> >> What about:
> >> 
> >> > ?weighted.mean
> >> > ?tapply
> >> > Q8 <- rnorm(100)
> >> > Faculty <- factor(c(rep(1,50), rep(2,50)))
> >> > w <- 1:100
> >> > tapply(Q8, Faculty, weighted.mean, w)
> >>          1          2 
> >> -0.0976302 -0.1457895 
> >> 
> >> 
> >> Kjetil Halvorsen
> >> 
> >> Tim Wilson wrote:
> >> > 
> >> > Hi everyone,
> >> > 
> >> > I've got a dataframe called 'faculty'. I want to do a weighted mean on
> >> > the column called 'Q8' weighted by the contents of column 'CETP'. In
> >> > addition, I need to operate on the result of splitting 'faculty'
> >> > according to the contents of a column 'FACULTY'. For example
> >> > 
> >> > > lapply(split(faculty$Q8, faculty$FACULTY), mean)
> >> > $"1"
> >> > [1] 2.2
> >> > 
> >> > $"2"
> >> > [1] 1.888889
> >> > 
> >> > gives the mean of 'Q8' after splitting by 'FACULTY'. When I try this
> >> > with weighted.mean, it doesn't work because I don't know how to specify
> >> > the result of the split as the argument for weighted.mean. For example,
> >> > 
> >> > > lapply(split(faculty$Q8, faculty$FACULTY), weighted.mean)
> >> > $"1"
> >> > [1] 2.2
> >> > 
> >> > $"2"
> >> > [1] 1.888889
> >> > 
> >> > gives the same results as above because I haven't specified the vector
> >> > to use for weighting. The following doesn't work at all for the obvious
> >> > reason that weighted.mean is not operating on the result of the split.
> >> > 
> >> > > lapply(split(faculty$Q8, faculty$FACULTY), weighted.mean(faculty$Q8,
> >> > faculty$CETP))
> >> > Error in match.fun(FUN) : not function, character, or symbol:
> >> > "weighted.mean(faculty$Q8, faculty$CETP)"
> >> > >
> >> > 
> >> > Any ideas?
> >> > 
> >> > -Tim
> >> > 
> >> > --
> >> > Tim Wilson      |   Visit Sibley online:   | Check out:
> >> > Henry Sibley HS |  http://www.isd197.org   | http://www.zope.com
> >> > W. St. Paul, MN |                          | http://slashdot.org
> >> > wilson at visi.com |  <dtml-var pithy_quote>  | http://linux.com
> >
> >Also take a look at the many weighted stats functions in the Hmisc library.  Do 
> >library(Hmisc)
> >?wtd.mean
> >
> >-Frank
> >
> >-- 
> >Frank E Harrell Jr              Prof. of Biostatistics & Statistics
> >Div. of Biostatistics & Epidem. Dept. of Health Evaluation Sciences
> >U. Virginia School of Medicine  http://hesweb1.med.virginia.edu/biostat
> >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> >r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> >Send "info", "help", or "[un]subscribe"
> >(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> >_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> >
> >
> 
> 
> 


-- 
Frank E Harrell Jr              Prof. of Biostatistics & Statistics
Div. of Biostatistics & Epidem. Dept. of Health Evaluation Sciences
U. Virginia School of Medicine  http://hesweb1.med.virginia.edu/biostat
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list