addcategorytopolls: Adds Category assignment to Poll Data This function returns a...

Description Usage Arguments Value Examples

View source: R/priorgrouping.R

Description

Adds Category assignment to Poll Data This function returns a new data frame with a priorcat column to be accessed in other functions.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
addcategorytopolls(
  poll_data,
  proploc,
  stateloc,
  election_data,
  cutoffs = c(-0.2, -0.1, -0.025, 0.025, 0.1, 0.2),
  groupnames = c("Strong Red", "Red", "Lean Red", "Competitive", "Lean Blue", "Blue",
    "Strong Blue"),
  logit = F
)

Arguments

poll_data

the data to fit the prior distribution , the first column must be the state name

proploc

the column the adjusted poll proportion is located

stateloc

the column the state is located in

election_data

the election data in df form. The first column must contain the name of that state.

cutoffs

the cutoffs used to split the data into the categories

groupnames

labels for the categories

logit

if a logit transformation for the prior means is desired

Value

poll_data with the prior category appended at the end

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
require(politicaldata)
elect2008  = subset(pres_results , year == 2008)
elect2008$margin = elect2008$dem - elect2008$rep
elect2012 = subset(pres_results , year == 2012)
elect2012$margin = elect2012$dem - elect2012$rep
electdata = data.frame("state" = elect2008$state, "2008" =  elect2008$margin, "2012" = elect2012$margin)
set.seed(16)
data(polls2016)
poll1 = polls2016[sample(1:nrow(polls2016), 500) ,]
poll2 = polls2016[sample(1:nrow(polls2016), 500), ]
propnormpolls1 = propnormdf(poll1, c(2,3))
propnormpolls2 = propnormdf(poll2, c(2,3))
addcategorytopolls(propnormpolls1, 30, 18, electdata)
newcutoff = c(-.15, -.1, -0.05, 0.05, .1, .15)
addcategorytopolls(propnormpolls2, 30, 18, electdata, cutoffs = newcutoff)

balexanderstats/bayesurvey documentation built on Sept. 20, 2020, 11:40 a.m.