【问题标题】:Missing levels when weightig (raking) data using ANESRake使用 ANESRake 加权(耙)数据时缺少级别
【发布时间】:2020-02-18 11:29:35
【问题描述】:

我有一个调查数据集和一些报价:

人口报价是:

(1 = up to 29 years 0,00%)

2 = 30 to 39 years 18,10%

3 = 40 to 49 years 28,77%

4 = 50 to 59 years 33,11%

5 = 60 and more years 20,01%

在数据集中,我要权重类别 5 缺失。以下是数据集中变量的统计数据:

2 = 32,33%  
3 = 36,56%  
4 = 31,12%  

如果我执行倾斜,我会收到以下错误:

library(anesrake)

r = anesrake(list_weights,
             d, 
             verbose = FALSE, 
             caseid =  d$RESPID, 
             maxit = 1500,
             cap = 5,
             choosemethod = "max", 
             type = "nolim")


Error in rakeonvar.default(mat[, i], inputter[[i]], weightvec) : variables must be coded continuously from 1 to n with no missing values

知道如何处理数据中缺失的级别吗?

这是引用的数据

list(Rec_Age = c(`2` = 0.181, `3` = 0.2877, `4` = 0.3311))

以及一小部分数据

structure(list(RESPID = structure(c(459, 311, 223, 60, 613, 495, 
300, 273, 78, 170, 217, 61, 175, 619, 270, 218, 453, 492, 23, 
65, 33, 113, 532, 26, 119, 49, 208, 102, 200, 165, 435, 298, 
593, 220, 111, 53, 494, 271, 305, 420, 323, 607, 105, 19, 426, 
171, 330, 201, 332, 277), label = "RESPID - Respondent ID", format.spss = "F10.0", display_width = 0L), 
    Rec_Age = structure(c(4, 2, 4, 3, 4, 4, 4, 3, 2, 2, 3, 2, 
    3, 4, 4, 2, 4, 4, 2, 3, 2, 2, 2, 3, 3, 2, 2, 2, 2, 3, 2, 
    3, 2, 3, 4, 3, 4, 3, 2, 3, 3, 3, 4, 4, 4, 2, 2, 3, 4, 3), label = "Rec_Age - Recode Age")), row.names = c(NA, 
-50L), class = "data.frame")

@Yuriy Saraykin

你是对的,现在没有错误,但如果我使用你的代码,所有的权重都为 1。所以做的事情一定出错了。

我不明白这是为什么。如果我像您一样使用所有级别的列表,我会收到此错误(我之前尝试过)。

Error in rakeonvar.default(mat[, i], inputter[[i]], weightvec) : you cannot rake any variable category to 0 or a negative number

您的列表和我的列表有什么区别(即使您的代码没有提供所需的结果)?

您的清单:

your_list

[[1]]
        1         2         3         4         5 
0.0000000 0.1810181 0.2877288 0.3311331 0.2001200 
dput(your_list)

list(Rec_Age = c(`1` = 0, `2` = 0.181, `3` = 0.2877, `4` = 0.3311, 
`5` = 0.2001))

我的清单:

My_list 

    my_list:
    $Rec_Age
         1      2      3      4      5 
    0.0000 0.1810 0.2877 0.3311 0.2001 

    dput(my_list)
     list(Rec_Age = c(`1` = 0, `2` = 0.181, `3` = 0.2877, `4` = 0.3311, `5` = 
     0.2001))

我的列表是这样生成的:

REC_age =  c(0, 0.181, 0.2877, 0.3311, 0.2001)

names(REC_age) = c(1, 2, 3, 4, 5)

【问题讨论】:

    标签: r missing-data weighting


    【解决方案1】:

    像这样试试。在我看来,您可以在样本中包含有关总体的信息。 这是一篇关于该主题的好文章。 https://www.r-bloggers.com/survey-raking-an-illustration/

    library(anesrake)
    library(weights)
    library(tidiverse)
    
    d <- d %>% mutate(Rec_Age = as.factor(Rec_Age))
    
    population <- data.frame(Rec_Age = c("2", "3", "4"), 
                             fraction = c(0.181, 0.2877, 0.3311)) 
    
    list_weights <- with(population,
                         list(Rec_Age = wpct(Rec_Age, fraction)))
    
    r <- anesrake(list_weights,
                  d, 
                  caseid =  d$RESPID, 
                  maxit = 1500,
                  cap = 5,
                  choosemethod = "max", 
                  type = "nolim")
    

    【讨论】:

    • 已修复错误。变量类别应该是一个因素。
    猜你喜欢
    • 1970-01-01
    • 2013-06-21
    • 2018-02-12
    • 1970-01-01
    • 2021-12-31
    • 2019-08-31
    • 2016-05-27
    相关资源
    最近更新 更多