Examples for 'srvyr::unweighted'


Calculate the an unweighted summary statistic from a survey

Aliases: unweighted

Keywords:

### ** Examples

library(survey)
Loading required package: grid
Loading required package: Matrix
Loading required package: survival
Attaching package: 'survey'
The following object is masked from 'package:graphics':

    dotchart
library(dplyr)
Attaching package: 'dplyr'
The following object is masked from 'package:rcloud.support':

    select
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
data(api)

dstrata <- apistrat %>%
  as_survey_design(strata = stype, weights = pw)

dstrata %>%
  summarise(api99_unw = unweighted(mean(api99)),
            n = unweighted(n()))
# A tibble: 1 × 2
  api99_unw     n
      <dbl> <int>
1      625.   200
dstrata %>%
  group_by(stype) %>%
  summarise(api_diff_unw = unweighted(mean(api00 - api99)))
# A tibble: 3 × 2
  stype api_diff_unw
  <fct>        <dbl>
1 E            38.6 
2 H             8.46
3 M            26.4 
# Some survey designs, like ones with raked weights, are not removed
# when filtered to preserve the structure. So if you don't use `unweighted()`
# your results can be wrong.
# Declare basic clustered design ----
cluster_design <- as_survey_design(
  .data = apiclus1,
  id = dnum,
  weights = pw,
  fpc = fpc
)

# Add raking weights for school type ----
pop.types <- data.frame(stype=c("E","H","M"), Freq=c(4421,755,1018))
pop.schwide <- data.frame(sch.wide=c("No","Yes"), Freq=c(1072,5122))

raked_design <- rake(
  cluster_design,
  sample.margins = list(~stype,~sch.wide),
  population.margins = list(pop.types, pop.schwide)
)

raked_design %>%
filter(cname != "Alameda") %>%
  group_by(cname) %>%
  summarize(
    direct_unw_mean = mean(api99),
    wrapped_unw_mean = unweighted(mean(api99))
  ) %>%
  filter(cname == "Alameda")
# A tibble: 1 × 3
  cname   direct_unw_mean wrapped_unw_mean
  <chr>             <dbl>            <dbl>
1 Alameda             609              NaN
# Notice how the results are different when using `unweighted()`

[Package srvyr version 1.1.1 Index]