我有一些调查数据。作为示例,我使用ÌSLR包中的credit数据。
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在加权数据集中,我有期望的分布学生:是/否(0.2/0.8)和性别男性/女性(0.35/0.65)。
这是另一个使用性别和结婚(加权)的SPSS的例子
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 |发布于 2019-08-20 04:31:14
对于expss包,您需要显式地提供权重变量。据我所知,iterake将特殊变量weight添加到数据集中:
library(expss)
cro(Credit$Gender, Credit$Married) # unweighted result
cro(credit_weighted$Gender, credit_weighted$Married, weight = credit_weighted$weight) # weighted resulthttps://stackoverflow.com/questions/57545819
复制相似问题