# Re: [R] Creating a weighted sample - Help

From: Daniel Nordlund <djnordlund_at_frontier.com>
Date: Thu, 03 Mar 2011 00:11:23 -0800

> -----Original Message-----
> From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org]
> On Behalf Of P Ehlers
> Sent: Wednesday, March 02, 2011 10:29 PM
> To: LouiseS
> Cc: r-help_at_r-project.org
> Subject: Re: [R] Creating a weighted sample - Help
>
> LouiseS wrote:
> > Hi
> >
> > I'm new to R and most things I want to do I can do but I'm stuck on how
> to
> > weight a sample. I have had a look through the post but I can't find
> > anything that addresses my specific problem. I am wanting to scale up a
> > sample which has been taken based on a single variable (perf) which has
> 4
> > attributes H,I, J and K. The make up of the sample is shown below:-
> >
> > Perf Factored Count (A) Raw Count (B) Factor (A/B)
> > H 5,945 2,924
> 2.033174
> > I 1,305 2,436
> 0.535714
> > J 2,000 2,092
> 0.956023
> > K 750 1,225
> 0.612245
> >
> >
> > I then want to produce all further analysis based on this factored
> sample.
> > I can produce a weighted sample in SAS using the weight function which I
> > have shown below
> >
> > wt=0;
> > if perf='H' then wt=2.033174;
> > if perf='I ' then wt=0.535714;
> > if perf='J ' then wt=0.956023;
> > if perf='K ' then wt=0.612245;
> >
> > proc freq data=DD.new;
> > tables resdstat;
> > weight wt;
> > run;
> >
> > Does anyone know how to reproduce this in R?
>
> I don't know what you mean by "all further analysis",
> but if you want weighted mean, variance, quantile, have
> a look at ?wtd.mean in the Hmisc package. Just use your
> A/B values in a weights vector.
>
> Peter Ehlers
>

You haven't told us how you obtained these data that you want to weight, but if you used some kind of non-SRS sampling plan (e.g. stratified, or cluster sample) then you should look at the survey package.

Dan

Daniel Nordlund
Bothell, WA USA

R-help_at_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. Received on Thu 03 Mar 2011 - 08:19:56 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 03 Mar 2011 - 14:10:18 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.