简体   繁体   English

使用十分位来定义层

[英]Using deciles to define strata

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). 我有一个调查数据集,并希望使用十分位来定义地层,U1 =(数据集中的所有单位都在D0 = min和D1之间),U2 =(在D1和D2之间)...... U10 =(之间) D9至D10 =最大)。

How do I use the deciles to define strata? 我如何使用十分位来定义地层?

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 : 使用quantile函数计算一个变量的十分位数,然后使用cut函数计算基于这些十分位数的因子,然后通过tapply在其他计算中使用该因子的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 . 另请参见by函数,该函数应该与yd类的变量一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM