我是否为trimWeights()使用了正确的语法?

时间:2019-03-22 15:13:56

标签: r survey

我对RStudio还是很陌生,但我正在尝试出于工作目的学习调查包。我正在尝试使用svydesign函数来调整trimWeights对象的权重。这是我到目前为止使用的代码。

library(survey)
library(tidyverse)
Sys.setenv(JAVA_HOME='C:\\Program Files\\Java\\jre1.8.0_201')

library(xlsx)

allsurveys = read.csv('allsurveys.csv', stringsAsFactors = FALSE, fileEncoding="UTF-8-BOM")

> head(allsurveys)
   case         daytype family 
1 36846 3 VA October WD     No  
2 36847 3 VA October WD     No   
3 36848 3 VA October WD     No   
4 36849 3 VA October WD     No   
5 36850 3 VA October WD    Yes   
6 36852 3 VA October WD     No 

daytype.table = read.csv('daytype_counts.csv', stringsAsFactors = FALSE, fileEncoding="UTF-8-BOM")

> head(daytype.table)
                              daytype   freq
1                     3 VA October WD 179786
2                     3 VA October WE  58237
3 3 VA October WE Autumn Half-term SH  54578
4 3 VA October WD Autumn Half-term SH  70652
5                    3 VA November WD 193441
6                    3 VA November WE  98562

allsurveys$daytype = factor(allsurveys$daytype)
allsurveys$family  = factor(allsurveys$family)

N = sum(daytype.table$freq)
na = nrow(allsurveys)
max.a = (3*N)/na
min.a = (0.3*N)/na

des.a = svydesign(ids = ~1,
                  strata = NULL,
                  fpc = rep(N,na),
                  data = allsurveys
                  )

ps.des.a <- postStratify(design = des.a, strata = ~daytype, population = daytype.table)
ps.des.c <- postStratify(design = des.c, strata = ~daytype, population = daytype.table)

ps.des.a.trim <- trimWeights(ps.des.a, lower=min.a, upper=max.c, strict=TRUE)
ps.des.c.trim <- trimWeights(ps.des.c, lower=min.c, upper=min.c, strict=TRUE)

trimWeights函数确实运行并创建了一个新的svydesign对象,但似乎不能正确应用上限。如果使用list(ps.des.a.trim$postStrata)查看权重,则会在底部获得一些权重,这些权重超过我设置的max.a。我不太确定这是怎么回事。是因为我要调整已使用svydesign加权的postStratify的权重吗?

0 个答案:

没有答案