Creating a weighted sample - Help

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

Creating a weighted sample - Help

LouiseS
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?

Thanks very much
Reply | Threaded
Open this post in threaded view
|

Re: Creating a weighted sample - Help

Peter Ehlers
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

>
> Thanks very much
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Creating-a-weighted-sample-Help-tp3331842p3331842.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Creating a weighted sample - Help

Daniel Nordlund-4
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]]
> On Behalf Of P Ehlers
> Sent: Wednesday, March 02, 2011 10:29 PM
> To: LouiseS
> Cc: [hidden email]
> 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

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Creating a weighted sample - Help

LouiseS
Hi

Thanks for responses.  The sample I have taken is a random sample from H, I, J and K.  The further analysis I want to do is all around bad debt rates so it could be (H/H+I)*100 = Bad rate percentage also population stability calculations that are all related to credit scoring.  I want to be able to report back on any variable that I have in my data set based on my factored counts (A) of 10,000 - so every calculation is based on 10,000 account in the correct proportions.

Does his help?

Thanks once again
Louise
Reply | Threaded
Open this post in threaded view
|

Re: Creating a weighted sample - Help

Nordlund, Dan (DSHS/RDA)
> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of LouiseS
> Sent: Thursday, March 03, 2011 5:21 AM
> To: [hidden email]
> Subject: Re: [R] Creating a weighted sample - Help
>
> Hi
>
> Thanks for responses.  The sample I have taken is a random sample from
> H, I,
> J and K.  The further analysis I want to do is all around bad debt
> rates so
> it could be (H/H+I)*100 = Bad rate percentage also population stability
> calculations that are all related to credit scoring.  I want to be able
> to
> report back on any variable that I have in my data set based on my
> factored
> counts (A) of 10,000 - so every calculation is based on 10,000 account
> in
> the correct proportions.
>
> Does his help?
>
> Thanks once again
> Louise
>

Louise,

It appears that you have done a stratified random sample of four types of accounts and have oversampled the less frequent account types.  You definitely should consider doing your analyses using the survey package (or similar package) that appropriately accounts for the sampling variability.  Otherwise, your variances / standard errors are going to be incorrect.

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204


______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.