tapply and weighted means

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

tapply and weighted means

Bugzilla from f_bresson@yahoo.fr
I' m trying to compute weighted mean on different
groups but it only returns NA. If I use the following
data.frame truc:

x  y  w
1  1  1
1  2  2
1  3  1
1  4  2
0  2  1
0  3  2
0  4  1
0  5  1

where x is a factor, and then use the command :

tapply(truc$y,list(truc$x),wtd.mean, weights=truc$w)

I just get NA. What's the problem ? What can I do ?

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: tapply and weighted means

Dimitris Rizopoulos
you need also to split the 'w' column, for each level of 'x'; you
could use:

lapply(split(truc, truc$x), function(z) weighted.mean(z$y, z$w))


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://www.med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm



----- Original Message -----
From: "Florent Bresson" <[hidden email]>
To: "R-help" <[hidden email]>
Sent: Thursday, January 12, 2006 3:44 PM
Subject: [R] tapply and weighted means


> I' m trying to compute weighted mean on different
> groups but it only returns NA. If I use the following
> data.frame truc:
>
> x  y  w
> 1  1  1
> 1  2  2
> 1  3  1
> 1  4  2
> 0  2  1
> 0  3  2
> 0  4  1
> 0  5  1
>
> where x is a factor, and then use the command :
>
> tapply(truc$y,list(truc$x),wtd.mean, weights=truc$w)
>
> I just get NA. What's the problem ? What can I do ?
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: tapply and weighted means

Frank Harrell
Dimitris Rizopoulos wrote:

> you need also to split the 'w' column, for each level of 'x'; you
> could use:
>
> lapply(split(truc, truc$x), function(z) weighted.mean(z$y, z$w))
>
>
> I hope it helps.
>
> Best,
> Dimitris

Or:
library(Hmisc)
?wtd.mean
The help file has a built-in example of this.
Frank

>
> ----
> Dimitris Rizopoulos
> Ph.D. Student
> Biostatistical Centre
> School of Public Health
> Catholic University of Leuven
>
> Address: Kapucijnenvoer 35, Leuven, Belgium
> Tel: +32/(0)16/336899
> Fax: +32/(0)16/337015
> Web: http://www.med.kuleuven.be/biostat/
>      http://www.student.kuleuven.be/~m0390867/dimitris.htm
>
>
>
> ----- Original Message -----
> From: "Florent Bresson" <[hidden email]>
> To: "R-help" <[hidden email]>
> Sent: Thursday, January 12, 2006 3:44 PM
> Subject: [R] tapply and weighted means
>
>
>
>>I' m trying to compute weighted mean on different
>>groups but it only returns NA. If I use the following
>>data.frame truc:
>>
>>x  y  w
>>1  1  1
>>1  2  2
>>1  3  1
>>1  4  2
>>0  2  1
>>0  3  2
>>0  4  1
>>0  5  1
>>
>>where x is a factor, and then use the command :
>>
>>tapply(truc$y,list(truc$x),wtd.mean, weights=truc$w)
>>
>>I just get NA. What's the problem ? What can I do ?
>>
>>______________________________________________
>>[hidden email] mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide!
>>http://www.R-project.org/posting-guide.html
>>
>
>
>
> Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>


--
Frank E Harrell Jr   Professor and Chair           School of Medicine
                      Department of Biostatistics   Vanderbilt University

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Frank Harrell
Department of Biostatistics, Vanderbilt University
Reply | Threaded
Open this post in threaded view
|

Re: tapply and weighted means

Gavin Simpson
In reply to this post by Bugzilla from f_bresson@yahoo.fr
On Thu, 2006-01-12 at 15:44 +0100, Florent Bresson wrote:

> I' m trying to compute weighted mean on different
> groups but it only returns NA. If I use the following
> data.frame truc:
>
> x  y  w
> 1  1  1
> 1  2  2
> 1  3  1
> 1  4  2
> 0  2  1
> 0  3  2
> 0  4  1
> 0  5  1
>
> where x is a factor, and then use the command :
>
> tapply(truc$y,list(truc$x),wtd.mean, weights=truc$w)
>
> I just get NA. What's the problem ? What can I do ?

Florent,

I guess you didn't read the help for tapply, which in the Value section
states:

     Note that optional arguments to 'FUN' supplied by the '...'
     argument are not divided into cells.  It is therefore
     inappropriate for 'FUN' to expect additional arguments with the
     same length as 'X'.

So tapply is not the right tool for this job. We can use by() instead (a
wrapper for tapply) as so:

dat <- matrix(scan(), byrow = TRUE, ncol = 3)
1  1  1
1  2  2
1  3  1
1  4  2
0  2  1
0  3  2
0  4  1
0  5  1

colnames(dat) <- c("x", "y", "w")
dat <- as.data.frame(dat)
dat
(res <- by(dat, dat$x, function(z) weighted.mean(z$y, z$w)))

but if you want to easily access the numbers you need to do a little
work, e.g.

as.vector(res)

Also, I don't see a function wtd.mean in standard R and weighted.mean()
doesn't have a weights argument, so I guess you are using a function
from another package and did not tell us.

HTH,

Gav
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson                     [T] +44 (0)20 7679 5522
ENSIS Research Fellow             [F] +44 (0)20 7679 7565
ENSIS Ltd. & ECRC                 [E] gavin.simpsonATNOSPAMucl.ac.uk
UCL Department of Geography       [W] http://www.ucl.ac.uk/~ucfagls/cv/
26 Bedford Way                    [W] http://www.ucl.ac.uk/~ucfagls/
London.  WC1H 0AP.
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html