r - (RIM) R 中的加权样本

标签 r survey expss

我有一些调查数据。例如,我使用 credit来自 ÌSLR 的数据
包裹。

library(ISLR)

数据中性别的分布看起来像这样
prop.table(table(Credit$Gender))
  Male Female 
0.4825 0.5175 

学生的分布看起来像这样。
prop.table(table(Credit$Student))
 No Yes 
0.9 0.1  

假设在人口中,性别的实际分布是​​男/女(0.35/0.65),学生的分布是是/否(0.2/0.8)。

在 SPSS 中,可以通过将“人口分布”除以“样本分布”来对样本进行加权,以模拟总体分布。此过程称为“RIM 加权”。数据将仅通过交叉表进行分析(即无回归、t 检验等)。在 R 中给样本加权的好方法是什么,以便稍后通过交叉表分析数据?

可以在 R 中计算 RIM 权重。
install.packages("devtools")
devtools::install_github("ttrodrigz/iterake")


credit_uni = universe(df = Credit,
    category(
        name = "Gender",
        buckets = c(" Male", "Female"),
        targets = c(.35, .65)),
    category(
        name = "Student",
        buckets = c("Yes", "No"),
        targets = c(.2, .8)))

credit_weighted = iterake(Credit, credit_uni)



-- iterake summary -------------------------------------------------------------
 Convergence: Success
  Iterations: 5

Unweighted N: 400.00
 Effective N: 339.58
  Weighted N: 400.00
  Efficiency: 84.9%
        Loss: 0.178

这里是加权数据的 SPSS 输出(交叉表)
                Student     
                No  Yes 
Gender  Male    117 23  140
        Female  203 57  260
                320 80  400

和这里来自未加权的数据(我导出两个文件并在 SPSS 中进行计算。我通过计算的权重对加权样本进行加权)。
                Student     
                No  Yes 
Gender   Male   177 16  193
         Female 183 24  20          
                360 40  400

在加权数据集中,我有所需的分布 Student: Yes/No(0.2/0.8) 和 Gender male/female(0.35/0.65)。

这是另一个使用 SPSS of Gender and Married(加权)的示例
    Married     
                No  Yes 
Gender   Male   57  83  140
         Female 102 158 260
                159 241 400

和未加权。
                Married 
                No  Yes 
Gender   Male   76  117 193
         Female 79  128 207
                155 245 400

这在 R 中不起作用(即两个交叉表看起来都像未加权的)。
library(expss)

cro(Credit$Gender, Credit$Married)

cro(credit_weighted$Gender, credit_weighted$Married)



 |               |              | Credit$Married |     |
 |               |              |             No | Yes |
 | ------------- | ------------ | -------------- | --- |
 | Credit$Gender |         Male |             76 | 117 |
 |               |       Female |             79 | 128 |
 |               | #Total cases |            155 | 245 |

 |                        |              | credit_weighted$Married |     |
 |                        |              |                      No | Yes |
 | ---------------------- | ------------ | ----------------------- | --- |
 | credit_weighted$Gender |         Male |                      76 | 117 |
 |                        |       Female |                      79 | 128 |
 |                        | #Total cases |                     155 | 245 |

最佳答案

expss包您需要明确提供您的权重变量。据我了解 iterake添加特殊变量 weight到数据集:

library(expss)

cro(Credit$Gender, Credit$Married) # unweighted result

cro(credit_weighted$Gender, credit_weighted$Married, weight = credit_weighted$weight) # weighted result

关于r - (RIM) R 中的加权样本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57545819/

相关文章:

r - R 中的数据可视化,对 geom_raster() 进行排序

r - 关于 bind_rows() 的非常简单但令人困惑的 R 问题

r - 加入/合并数据框内的两列

r - 使用 tidyverse 清理排名选择调查

R expss use_labels 和 dplyr 逻辑

r - 在后台使用矩形分隔 ggplot

r - 复杂调查设计的插补 (nhanesIII)

r - 使用 R expss 和 data.table 是否可以从 csv 文件加载 data.table 标签,而不是手动输入代码?

r - 如何在函数内部使用 R 字符向量元素作为字符串和变量?