How do I go about generating a WEIGHTED mean (and standard error) of a variable (e.g., expenditures) for each level of a categorical variable (e.g., geographic region)? I'm looking for something comparable to PROC MEANS in SAS with both a class and weight statement. Thanks. Marc [[alternative HTML version deleted]]
MZodet at ahrq.gov wrote:> How do I go about generating a WEIGHTED mean (and standard error) of a > variable (e.g., expenditures) for each level of a categorical variable > (e.g., geographic region)? I'm looking for something comparable to PROC > MEANS in SAS with both a class and weight statement.That's two questions. 1) to apply a weighted mean to a vector, see ?weighted.mean 2) to apply a function to data grouped by categorical variable, you probably need "by" or "tapply". See the help pages and examples for both. Cheers Jason -- Indigo Industrial Controls Ltd. http://www.indigoindustrial.co.nz 64-21-343-545 jasont at indigoindustrial.co.nz
Dear Marc, For the weighted mean, one possible solution is as follows and will hopefully give you the general idea: tmp <- data.frame(x=sample(1:5, 100, replace=TRUE), y=sample(1:100, 100, replace=TRUE), w=runif(100)) lapply(split(tmp[, 2:3], tmp[, "x"]), function(x) { weighted.mean(x=x$y, w=x$w)}) Regards, Andrew C. Ward CAPE Centre Department of Chemical Engineering The University of Queensland Brisbane Qld 4072 Australia Quoting MZodet at ahrq.gov:> How do I go about generating a WEIGHTED mean (and > standard error) of a > variable (e.g., expenditures) for each level of a > categorical variable > (e.g., geographic region)? I'm looking for something > comparable to PROC > MEANS in SAS with both a class and weight statement. > > > > Thanks. > > > > Marc > > > > > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >
On Wed, 2003-11-26 at 01:28, MZodet at ahrq.gov wrote:> How do I go about generating a WEIGHTED mean (and standard error) of a > variable (e.g., expenditures) for each level of a categorical variable > (e.g., geographic region)? I'm looking for something comparable to PROC > MEANS in SAS with both a class and weight statement. >I asked this question a few years ago and this is the anwer I got works: see http://www.r-project.org/nocvs/mail/r-help/1999/2160.html tapply(seq(along=wage), list(Educc,Year), function(i, x=wage, w=weight) weighted.mean(x[i], w[i])) (wage and weight are the variable of interest and weight, while Educc and Year are the factors) Regards, Markus> > > Thanks. > > > > Marc > > > > > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help-- Markus J?ntti <markus.jantti at iki.fi> Abo Akademi University