我试图通过anesrake
软件包计算样本重量,并不断收到上述错误,尽管多次试图排除故障。我正在用两个Vars(政党身份识别和政治意识形态)加权,直到我试图执行 anesrake 命令时,才得到错误。我尝试了目标变量的乘法,将其编码为数字,并确保没有NAs,没有运气。我无法附加数据文件,但我试图加权的数据格式低于数据,代码低于数据。
partyid7_ | ideology |
---|---|
Democrat | Liberal |
Republican | Conservative |
Independent | Moderate |
#recoding/labeling target variables
df <- df %>% mutate(pid = case_when(partyid7_ %in% c(1,2) ~ "Democrat",
partyid7_ %in% c(3,4,5) ~ "Independent",
partyid7_ %in% c(6,7) ~ "Republican"),
ideol = case_when(ideology %in% c(1,2) ~ "Liberal",
ideology == 3 ~ "Moderate",
ideology %in% c(4,5) ~
"Conservative"))
# set target variable parameters
ideol <- c("Liberal", "Moderate", "Conservative")
ideo_prop <- c(0.25, 0.37, 0.36) #population proportions
pid <- c("Democrat", "Independent", "Republican")
pid_prop <- c(0.28, 0.41, 0.28) #population parameters
population <- dplyr::data_frame(ideol, pid, ideo_prop, pid_prop)
# combining population parameters for target variables into one df
pop <- population %>% mutate(ideol = as.factor(ideol),
pid = as.factor(pid))
# create target list of variables to be weighted
target <- with(pop, list(
ideol = weights::wpct(ideol, ideo_prop),
pid = weights::wpct(pid, pid_prop)
))
df$caseid <- 1:length(df$pid)
## creating anesrake object w/vector of weights per respondent
dfrake <- anesrake(target,
df,
caseid = df$caseid,
cap = 3,
choosemethod = "total",
type = "pctlim",
pctlim = 0.05
)