library(SDaA) library(survey) par(mfrow=c(1,2)) data(htpop) popecdf <- ecdf(htpop$height) plot(popecdf, do.points = FALSE, ylab = "F(y)", xlab = "Height Value, y") minht <- min(htpop$height) breaks <- c(minht - 1, seq(from = minht, to = max(htpop$height), by = 1)) hist(htpop$height, ylab = "f(y)", breaks = breaks, xlab = "Height Value, y", freq = FALSE) par(mfrow=c(1,3)) minht <- min(htpop$height) breaks <- c(minht - 1, seq(from = minht, to = max(htpop$height), by = 1)) hist(htpop$height, ylab = "f(y)", breaks = breaks, xlab = "Height Value, y", freq = FALSE) data(htsrs) hist(htsrs$height, ylab = "Relative Frequency", xlab = "Height (cm)", freq = FALSE) data(htstrat) hist(htstrat$height, ylab = "Relative Frequency", xlab = "Height (cm)", freq = FALSE) par(mfrow=c(1,2)) minht <- min(htstrat$height) breaks <- c(minht - 1, seq(from = minht, to = max(htstrat$height), by = 1)) hist(htstrat$height, ylab = expression(hat(f)(y)), breaks = breaks, xlab = "Height Value, y", freq = FALSE) stratecdf <- ecdf(htstrat$height) plot(stratecdf, do.points = FALSE, ylab = expression(hat(F)(y)), xlab = "Height Value, y") data(htpop) mean(htpop$height) quantile(htpop$height,probs=c(25,50,90)/100) var(htpop$height) data(htsrs) mean(htsrs$height) quantile(htsrs$height,probs=c(25,50,90)/100) var(htsrs$height) data(htstrat) mean(htstrat$height) quantile(htstrat$height,probs=c(25,50,90)/100) var(htstrat$height) str<-c(rep(1,160),rep(2,40)) strdesign<-svydesign(id = ~1, strata = ~str, fpc = c(rep(1000,160),rep(1000,40)), data = htstrat) svymean(~height,strdesign) svyquantile(~height,strdesign,c(.25,.50,.90)) svyvar(htstrat$height,strdesign) par(mfrow=c(1,2)) data(syc) par(mfrow=c(1,2)) hist(syc$age, main="Sample unweighted",col="purple", freq = FALSE) oo <- options(survey.lonely.psu = "certainty") sycdesign <- svydesign(ids = ~psu, strata = ~stratum, data = syc, weights = ~finalwt) svyhist(~age, sycdesign, main="Sample weighted",col="purple") options(oo) sycdesign <- svydesign(ids = ~psu, strata = ~stratum, data = syc, weights = ~finalwt) oo <- options(survey.lonely.psu = "certainty") svyboxplot(age ~ factor(stratum), design = sycdesign) options(oo) library(ggplot2) p <- ggplot(syc, aes(x = factor(stratum), y = factor(age))) g <- p + stat_sum(aes(group = 1, weight = finalwt, size = ..n..)) print(g) oo <- options(survey.lonely.psu = "certainty") sycstrat5 <- subset(sycdesign, stratum == 5) svyboxplot(age ~ factor(psu), design = sycstrat5) options(oo) sycstrat5df <- subset(syc, stratum == 5) p <- ggplot(sycstrat5df, aes(x = factor(psu), y = factor(age))) g <- p + stat_sum(aes(group = 1, weight = finalwt, size = ..n..)) print(g)