# Examples from ?survey::twophase
# two-phase simple random sampling.
data(pbc, package="survival")
library(dplyr)
pbc <- pbc %>%
mutate(randomized = !is.na(trt) & trt > 0,
id = row_number())
d2pbc <- pbc %>%
as_survey_twophase(id = list(id, id), subset = randomized)
d2pbc %>% summarize(mean = survey_mean(bili))
# two-stage sampling as two-phase
library(survey)
data(mu284)
mu284_1 <- mu284 %>%
dplyr::slice(c(1:15, rep(1:5, n2[1:5] - 3))) %>%
mutate(id = row_number(),
sub = rep(c(TRUE, FALSE), c(15, 34-15)))
dmu284 <- mu284 %>%
as_survey_design(ids = c(id1, id2), fpc = c(n1, n2))
# first phase cluster sample, second phase stratified within cluster
d2mu284 <- mu284_1 %>%
as_survey_twophase(id = list(id1, id), strata = list(NULL, id1),
fpc = list(n1, NULL), subset = sub)
dmu284 %>%
summarize(total = survey_total(y1),
mean = survey_mean(y1))
d2mu284 %>%
summarize(total = survey_total(y1),
mean = survey_mean(y1))
# dplyr 0.7 introduced new style of NSE called quosures
# See `vignette("programming", package = "dplyr")` for details
ids <- quo(list(id, id))
d2pbc <- pbc %>%
as_survey_twophase(id = !!ids, subset = "randomized")
Run the code above in your browser using DataLab