Reputation:
I have a survey data set and want to use the deciles to define strata, U1 = (all units in data set is between D0=min and D1), U2=(between D1 and D2).....U10=(between D9 to D10=max).
How do I use the deciles to define strata?
Upvotes: 1
Views: 397
Reputation: 8252
An example of using the quantile
function to compute deciles of one variable, and then the cut
function to compute a factor based on those deciles, and then using that factor in other calculations, via tapply
:
# Let's set up some data:
y <- rnorm(30, 100, 20)
x <- rpois(30, 25-y/20) # make x depend on y a little
surveyres <- data.frame(y=y,x=x)
# set up the deciles of one variable
yd <- cut(y, breaks=c(-Inf,quantile(y,seq(0.1,0.9,by=0.1)),Inf) )
# compute means of another variable over deciles of the first:
tapply(surveyres$x, yd, mean)
(-Inf,84.2] (84.2,88.8] (88.8,93.8] (93.8,97.5] (97.5,100] (100,104]
23.66667 28.00000 22.33333 20.00000 20.33333 17.33333
(104,110] (110,114] (114,123] (123, Inf]
20.66667 19.33333 21.00000 20.33333
See also the by
function which should work with a variable like yd
.
Upvotes: 4