mrp_model: Multilevel Regression with Poststratification

Description Usage Arguments Details Value See Also Examples

Description

Implements a multilevel regression model with postratification to create estimates of public opinion at differing strata using census and survey data.

Usage

1
2
mrp_model(formula, data, mrp.data, method = c("glmer", "bglmer"),
  family = binomial(link = "logit"), ...)

Arguments

formula

A formula specifying the regression model. The formula must be in a valid format for glmer.

data

A data frame containing the survey data. All variables in formula must be columns in the dataset.

mrp.data

A data frame created by mrp_table of class mrp.table with marginal distributions of individual level characteristics. Any variables (column names) in mrp.data that are also in formula (either as random or fixed effects) will be used in the poststratification stage to create weighted cell predictions.

method

character: The regression function to use. Options are 'glmer' for the glmer() function or 'bglmer' for the bglmer function from the blme package.

family

a GLM family, see glm and family.

...

Other options passed to glmer or bglmer.

Details

Model Specifications:

Value

A list object of class mrp.model containing the fitted model from glmer; a three-column data frame with the grouping variable index and the weighted and unweighted cell predictions; and the function call.

See Also

mrp_table, mrp_merge, glmer

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
mrp_load_libs()

### Needed to get 2012 vote share
library(RCurl)
library(XML)
library(stringr)

### Define model for poststratification
model <- list(~state, ~age4, ~sex, ~race4, ~educ3)
### Load in merged trend dataset, recode to match CPS variables
data <- tsdat[tsdat$wts > 0 & !is.na(tsdat[, 'nr1']), ]
## Take a random sample of the data
data <- sample_frac(data, size = .2)
data <- copy_lev(data, tsdat)
data.format()

## Recodes
data$state <- recode(data$demState, paste0(paste(1:51, 1:51, sep = "=", collapse = ";"), ";else=NA"))
levels(data$state) <- c(state.abb[1:8], "DC", state.abb[9:50])
data$sex <- data$demGender
data$age4 <- data$age
data$race4 <- data$demRace4
data$educ3 <- data$xeduc3
data$grp <- mrp_grp(data, model) # Make variable for later merging

## Make mariginal distribution table using 2012 CPS data
cps.tbl <- mrp_table(model = model)

## 2012 vote share by state, will be used as fixed-effect
presdat <- readHTMLTable(getURL("http://www.archives.gov/federal-register/electoral-college/2012/popular-vote.html"),
                         stringsAsFactors = FALSE)[[2]][1:51, 1:3]
names(presdat) <- c('state', 'democrat', 'republican')
for (i in 2:3)
  presdat[, i] <- as.numeric(presdat[, i])
presdat[, 2:3] <- presdat[, 2:3] / rowSums(presdat[, 2:3])
presdat$state <- gsub("NY**", "NY", presdat$state, fixed = TRUE)

## Add vote share to main data and population table
data$st.repvote <- presdat$republican[data$state]
data$z.st.repvote <- rescale(data$st.repvote)  # needs to be rescaled to play nice with glmer

presdat$stname <- presdat$state
presdat <- presdat[, c('stname', 'republican')]
colnames(presdat)[2] <- 'st.repvote'
presdat[, 'z.st.repvote'] <- rescale(presdat[, 2])

cps.tbl <- merge(cps.tbl, presdat, by = 'stname', all.x = TRUE, sort = FALSE) # see merge.mrp.table

## Run the MRP regression
mrp.fit1 <- mrp_model(y ~ z.st.repvote + (1 | age4) + (1 | sex) + (1 | race4) + (1 | educ3),
                      data = data, mrp.data = cps.tbl)

### Extract cell predictions and merge with data
res <- mrp_merge(data, mrp.fit1)

alexdulin/MCmrp documentation built on May 11, 2019, 11:29 p.m.