weighting_fun_from_samplingframe: creates a weighting function from a sampling frame

Description Usage Arguments Value Examples

View source: R/weighting_fun_from_samplingframe.R

Description

creates a weighting function from a sampling frame

Usage

1
2
3
weighting_fun_from_samplingframe(sampling.frame, data.stratum.column,
  sampling.frame.population.column = "population",
  sampling.frame.stratum.column = "stratum", data = NULL)

Arguments

data.stratum.column

data column name that holds the record's strata names

sampling.frame.population.column

sampling frame name of column holding population counts. defaults to "population"

sampling.frame.stratum.column

sampling frame name of column holding stratum names. defaults to "stratum". Stratum names must match exactly values in:

data

optional but recommended: you can provide an example data frame of data supposed to match the sampling frame to check if the provided variable names match and whether all strata in the data appear in the sampling frame.

sampling.frame.file

data frame containing the sampling frame. should contain columns "stratum" and "population", otherwise column names must be specified.

Value

returns a new function that takes a data frame as input returns a vector of weights corresponding to each row in the data frame.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# laod data and sampling frames:
mydata<-read.csv("mydata.csv")
mysamplingframe<-read.csv("mysamplingframe.csv")
# create weighting function:
weighting<-weighting_fun_from_samplingframe(sampling.frame = mysamplingframe,
                                 data.stratum.column = "strata_names",
                                 sampling.frame.population.column = "pop",
                                 sampling.frame.stratum.column = "strat_name")
# use weighting function:
mydata$weights<-weighting(mydata)

# this also works on subsets of the data:
mydata_subset<-mydata[1:100,]
subset_weights<- weighting(mydata)

mabafaba/surveyweights documentation built on Sept. 28, 2019, 8:18 p.m.