2017-07-06 7 views
0

サンプリング加重を持っているときに、各グループ内で連続変数(各値を「値」と呼ぶ)の各観測値の分位(0〜100)を計算しようとしています新しい変数のそれぞれの分位点への観測。R:割り当て付きグループ別に加重分位を推定する

つまり、各行は観測値であり、各観測値は1つのグループに属します。すべてのグループに2つ以上の観測値があります。各グループ内では、データのサンプリング加重を使用して値の分布を推定し、そのパーセンテージがグループの分布内のどのパーセンタイルであるかを決定し、そのパーセンタイルをデータフレームに列として追加する必要があります。

私の知る限り、surveyパッケージは、svyby()svyquantile()を有するが、指定された変位値のための後者の戻り値ではなく、与えられた観測値の分位。

# Load survey package 
library(survey) 

# Set seed for replication 
set.seed(123) 

# Create data with value, group, weight 
dat <- data.frame(value = 1:6, 
        group = rep(1:3,2), 
        weight = abs(rnorm(6)) 
# Declare survey design 
d <- survey::svydesign(id =~1, data = dat, weights = weight) 

# Do something to calculate the quantile and add it to the data 
???? 

これは、この質問に似ているが、サブグループによって行われていません:Compute quantiles incorporating Sample Design (Survey package)

+0

https://stackoverflow.com/questions/32167390/compute-quantiles-incorporating-sample-design-survey-package/32173435#32173435またはhttps://stackoverflow.com/questions/24587499/compute- 24590340#24590340 –

+0

申し訳ありませんが、「quantile_by_stype」はサブグループ別ですか?そうではありません。私はなぜsvybyまたはサブセットを使用して、あなたが必要とする部分集団を得るのに十分でないのか混乱していますか?ありがとう –

+0

@AnthonyDamicoこれらはサブグループ別に分位数を計算するようですが、(a)前回のグループに値を追加していません。私は答えとして追加した非常にハッキリなアプローチを使用して終了しました。プロセスをスピードアップする方法があれば、変更することもできます! – user3614648

答えて

0

私は解決策をまとめます。 mutate()の以下のステートメントのシーケンスを変更して、サンプリング重みを関心のある分位数に変換することができます。これはベースRで行うことができますが、dplyr::bind_rows()のパワーのためにdplyrパッケージを使用して、2つのデータフレームを結合するときにNAsを追加します。

# Set seed for replication 
set.seed(123) 

# Create data with value, group, weight 
dat <- data.frame(value = 1:6, 
        group = rep(1:3,2), 
        weight = abs(rnorm(6)) 

# Initialize list for storing group results 
# Setting the length of the list is quicker than 
# creating an empty list and growing it 
quantile_list <- vector("list", length(unique(dat$group))) 

# Initialize variable to indicate initial iteration 
iteration <- 0 

# estimate the decile of each respondent 
# in a large for-loop 

for(group in unique(dat$group)) { 

# Keep only observations for a given group 
    temp <- dat %>% dplyr::filter(group == group) 

    # Create subset with missing values 
    temp_missing <- temp %>% dplyr::filter(is.na(value)) 

    # Create subset without missing values 
    temp_nonmissing <- temp %>% dplyr::filter(!is.na(value)) 

    # Sort observations with value on value, calculate cumulative 
    # sum of sampling weights, create variable indicating the decile 
    # of responses. 1 = lowest, 10 = highest 
    temp_nonmissing <- temp_nonmissing %>% 
          dplyr::arrange(value) %>% 
          dplyr::mutate(cumulative_weight = cumsum(weight), 
              cumulative_weight_prop = cumulative_weight/sum(weight), 
              decile = dplyr::case_when(cumulative_weight_prop < 0.10 ~ 1, 
              cumulative_weight_prop >= 0.10 & cumulative_weight_prop < 0.20 ~ 2, 
              cumulative_weight_prop >= 0.20 & cumulative_weight_prop < 0.30 ~ 3, 
              cumulative_weight_prop >= 0.30 & cumulative_weight_prop < 0.40 ~ 4, 
              cumulative_weight_prop >= 0.40 & cumulative_weight_prop < 0.50 ~ 5, 
              cumulative_weight_prop >= 0.50 & cumulative_weight_prop < 0.60 ~ 6, 
              cumulative_weight_prop >= 0.60 & cumulative_weight_prop < 0.70 ~ 7, 
              cumulative_weight_prop >= 0.70 & cumulative_weight_prop < 0.80 ~ 8, 
              cumulative_weight_prop >= 0.80 & cumulative_weight_prop < 0.90 ~ 9 , 
              cumulative_weight_prop >= 0.90 ~ 10)) 

    # Increment the iteration of the for loop 
    iteration <- iteration + 1 

    # Join the data with missing values and the data without 
    # missing values on the value variable into 
    # a single data frame 
    quantile_list[[iteration]] <- dplyr::bind_rows(temp_nonmissing, temp_missing) 
    } 

# Convert the list of data frames into a single dataframe 
out <- dplyr::bind_rows(quantile_list) 

# Show outcome 
head(out) 
関連する問題