Movatterモバイル変換


[0]ホーム

URL:


Type:Package
Title:Estimate and Visualize Voting Blocs' Partisan Contributions
Version:0.1.1
Maintainer:Cole Tanigawa-Lau <coletl@stanford.edu>
Description:Functions to combine data on voting blocs' size, turnout, and vote choice to estimate each bloc's vote contributions to the Democratic and Republican parties. The package also includes functions for uncertainty estimation and plotting. Users may define voting blocs along a discrete or continuous variable. The package implements methods described in Grimmer, Marble, and Tanigawa-Lau (2023) <doi:10.31235/osf.io/c9fkg>.
License:GPL (≥ 3)
Encoding:UTF-8
LazyData:true
Suggests:devtools (≥ 2.4.3), questionr (≥ 0.7.7), reldist (≥1.7.0), testthat (≥ 3.1.3)
Config/testthat/edition:3
RoxygenNote:7.2.0
Imports:collapse (≥ 1.7.6), dplyr (≥ 1.0.6), ggplot2 (≥ 3.2.0), ks(≥ 1.13.4), mgcv (≥ 1.8.39), rlang (≥ 1.0.0), tibble (≥3.0.0)
Depends:R (≥ 3.6.0)
NeedsCompilation:no
Packaged:2023-04-02 20:40:51 UTC; cole
Author:Justin Grimmer [aut], Will MarbleORCID iD [aut], Cole Tanigawa-LauORCID iD [aut, cre]
Repository:CRAN
Date/Publication:2023-04-02 21:00:02 UTC

Sample of 2020 ANES cumulative data file

Description

Selected columns from the American National Election Studies' 2020 cumulative data file.The final column is an example of the three-valued variable for voting behavior,to be passed to the 'dv_vote3' argument,

Usage

anes

Format

A data frame with 68,224 rows and 13 columns:

year

election year

respid

respondent identifier

weight

survey weight

race

respondent race

gender

respondent gender

educ

respondent education level

age

respondent age

voted

respondent's voter turnout

vote_pres

respondent's presidential vote

vote_pres_dem

flag indicating Democratic presidential vote choice

vote_pres_rep

flag indicating Republican presidential vote choice

vote_pres3

Three-valued voting behavior DV coded as follows: -1 for Democrat vote choice, 0 for third-party vote, 1 for Republican vote choice, and NA for no vote.

Source

https://electionstudies.org/data-center/anes-time-series-cumulative-data-file/


Validator for class vbdf

Description

Validator for class vbdf

Usage

check_vbdf(x, tolerance = sqrt(.Machine$double.eps))

Arguments

x

object to check

tolerance

tolerance used when checking range of probability estimates


Estimate density

Description

Runkde for weighted density estimationof ax atn_pointsevenly spaced points betweenmin and max.

Usage

estimate_density(x, min, max, n_points = 100, w = NULL, ...)

Arguments

x

numeric vector or matrix

min

numeric vector giving the lower bound of evaluation points for each variable inx

max

numeric vector giving the upper bound of evaluation points for each variable inx

n_points

number of evaluation points (estimates)

w

vector of weights. Default uses uniform weighting.

...

further arguments to pass tokde


Constructor for class vbdf

Description

Constructor for class vbdf

Usage

new_vbdf(x, bloc_var = character(), var_type = c("discrete", "continuous"))

Arguments

x

a data.frame

bloc_var

character vector naming the variables to define voting blocs

var_type

string, the type, discrete or continuous


Constructor for vbdf summaries

Description

Constructor for vbdf summaries

Usage

new_vbsum(x, bloc_var, var_type, summary_type, resamples)

Arguments

x

data.frame of uncertainty summary

bloc_var

string, the name of the variable that defines the voting blocs

var_type

string, the type of variable, discrete or continuous

summary_type

string, the type of variable, discrete or continuous

resamples

numeric, the number of bootstrap resamples

Value

Avbsum object


Continuous voting bloc analysis

Description

Define voting blocs along acontinuous variable and estimate theirpartisan vote contributions.

Usage

vb_continuous(  data,  data_density = data,  data_turnout = data,  data_vote = data,  indep,  dv_vote3,  dv_turnout,  weight = NULL,  min_val = NULL,  max_val = NULL,  n_points = 100,  boot_iters = FALSE,  verbose = FALSE,  tolerance = sqrt(.Machine$double.eps),  ...)

Arguments

data

default data.frame to use as the source fordensity, turnout, and vote choice data.

data_density

data.frame of blocs' composition/density data. Mustinclude any columns named byindep andweight.

data_turnout

data.frame of blocs' turnout data. Must include anycolumns named bydv_turnout,indep andweight.

data_vote

data.frame of blocs' vote choice data. Must include anycolumns named bydv_vote3,indep, andweight.

indep

string, column name of the independent variable definingdiscrete voting blocs.

dv_vote3

string, column name of the dependent variable indata_vote, coded asfollows: -1 for Democrat vote choice, 0 for third-party vote, 1 forRepublican vote choice, and NA for no vote.

dv_turnout

string, column name of the dependent variable flaggingvoter turnout indata_turnout. That column must be coded 0 = no vote, 1 = voted.

weight

optional string naming the column of sample weights.

min_val

numeric vector of the same length asindep, Lower bound for the density estimation of each respectiveindep. See[estimate_density].

max_val

numeric vector of the same length asindep, Upper bound for the density estimation of each respectiveindep. See[estimate_density].

n_points

scalar, number of points at which to estimate density. See[estimate_density].

boot_iters

integer, number of bootstrap iterations for uncertaintyestimation. The defaultFALSE is equivalent to 0 and does not estimateuncertainty.

verbose

logical, whether to print iteration number.

tolerance

tolerance used when checking range of probability estimates

...

further arguments to pass tokde for density estimation.

Value

avbdf data.frame with columns for the resample, bloc variable,and, for each resample-bloc combination, four estimates:probability density, turnout, Republican vote choice conditional on turnout,and net Republican votes.


Calculate differences in bloc contributions

Description

Use vbdf output to calculate differencesin blocs' net Republican vote contributions.

Usage

vb_difference(  vbdf,  estimates = grep("prob|pr_turnout|pr_votedem|pr_voterep|cond_rep|net_rep",    names(vbdf), value = TRUE),  sort_col = "year",  tolerance = sqrt(.Machine$double.eps))

Arguments

vbdf

data.frame holding the results of voting bloc analyses.

estimates

character vector naming the column(s) invbdf withwhich to compute differences.

sort_col

character vector naming the column(s) invbdf to usefor sorting before callingdiff.

tolerance

tolerance used when checking range of probability estimates

Value

Avbdf object, plus two types of columns:for each column named inestimates, a column nameddiff_* containing thedifference in each estimate acrosssort_col values,comp, which contains a string tag for the rows compared (e.g., 2020-2016),

Avbdf object.


Discrete voting bloc analysis

Description

Define voting blocs along adiscrete variable and estimate their partisanvote contributions.

Usage

vb_discrete(  data,  data_density = data,  data_turnout = data,  data_vote = data,  indep,  dv_vote3,  dv_turnout,  weight = NULL,  boot_iters = FALSE,  verbose = FALSE,  check_discrete = TRUE)

Arguments

data

default data.frame to use as the source fordensity, turnout, and vote choice data.

data_density

data.frame of blocs' composition/density data. Mustinclude any columns named byindep andweight.

data_turnout

data.frame of blocs' turnout data. Must include anycolumns named bydv_turnout,indep andweight.

data_vote

data.frame of blocs' vote choice data. Must include anycolumns named bydv_vote3,indep, andweight.

indep

string, column name of the independent variable definingdiscrete voting blocs.

dv_vote3

string, column name of the dependent variable indata_vote, coded asfollows: -1 for Democrat vote choice, 0 for third-party vote, 1 forRepublican vote choice, and NA for no vote.

dv_turnout

string, column name of the dependent variable flaggingvoter turnout indata_turnout. That column must be coded 0 = no vote, 1 = voted.

weight

optional string naming the column of sample weights.

boot_iters

integer, number of bootstrap iterations for uncertaintyestimation. The defaultFALSE is equivalent to 0 and does not estimateuncertainty.

verbose

logical, whether to print iteration number.

check_discrete

logical, whether to check ifindep is a discrete variable.

Value

Avbdf object.


Plot the summary of a voting bloc analysis

Description

Plot the summary of a voting bloc analysis

Usage

vb_plot(  data,  x_col = get_bloc_var(data),  y_col,  ymin_col,  ymax_col,  discrete = length(unique(data[[x_col]])) < 20)

Arguments

data

avbsum data.frame, the result of [vb_summary].

x_col

string naming the column that defines voting blocs.

y_col

string naming the column of point estimates.

ymin_col

string naming the column to plot as the lower bound of the confidence interval.

ymax_col

string naming the column to plot as the upper bound of the confidence interval.

discrete

logical indicating whether voting blocs are defined along a discrete (not continuous) variable.

Value

a ggplot object


Summarize uncertainty for a vbdf objects

Description

Summarize uncertainty for a vbdf objects. Analysis must have run with bootstrap iterations.vb_uncertainty is just an alias forvb_summary.

Usage

vb_summary(  object,  type = c("discrete", "continuous", "binned"),  estimates = grep("prob|pr_turnout|pr_votedem|pr_voterep|cond_rep|net_rep",    names(object), value = TRUE),  na.rm = FALSE,  funcs = c("mean", "median", "low", "high"),  low_ci = 0.025,  high_ci = 0.975,  bin_col,  tolerance = sqrt(.Machine$double.eps))vb_uncertainty(  object,  type = c("discrete", "continuous", "binned"),  estimates = grep("prob|pr_turnout|pr_votedem|pr_voterep|cond_rep|net_rep",    names(object), value = TRUE),  na.rm = FALSE,  funcs = c("mean", "median", "low", "high"),  low_ci = 0.025,  high_ci = 0.975,  bin_col,  tolerance = sqrt(.Machine$double.eps))

Arguments

object

avbdf object, usually the output of [vb_discrete], [vb_continuous], or [vb_difference].

type

a string naming the type of independent variable summary. Use"binned" when using the output of [vb_continuous] plus a binned version of the continuous bloc variable.

estimates

character vector naming columns for which to calculateuncertainty estimates.

na.rm

logical indicating whether to removeNA values inestimates.

funcs

character vector of summary functions to apply toestimates. Alternatively, supply your own list of functions, whichshould accept a numeric vector input and return a scalar.

low_ci

numeric. If you include the string"low" infuncs, then use this argument to control the lower bound of the confidence interval.

high_ci

numeric. If you include the string"high" infuncs, then use this argument to control the upper bound of the confidence interval.

bin_col

character vector naming the column(s) that define the bins. Used only whentype is"binned".

tolerance

tolerance used when checking range of probability estimates

Value

A summary object with additional columns for each combinationofestimates andfuncs.


Create a vbdf object

Description

Create a vbdf object holding bloc-level estimates of composition, turnout,and/or vote choice. This function is mostly for internal use, but you maywant it to create avbdf object from your own voting bloc analysis.A validvbdf object can be used in [vb_difference] and [vb_plot].

Usage

vbdf(  data,  bloc_var,  var_type = c("discrete", "continuous"),  tolerance = sqrt(.Machine$double.eps))

Arguments

data

data.frame of voting-bloc results to convert to avbdf object

bloc_var

string, the name of the variable that defines the voting blocs

var_type

string, the type of variable, discrete or continuous

tolerance

tolerance used when checking range of probability estimates

Value

Avbdf object.


Weighted frequency table or proportions

Description

Weighted frequency table or proportions

Usage

wtd_table(  ...,  weight = NULL,  na.rm = FALSE,  prop = FALSE,  return_tibble = FALSE,  normwt = FALSE)

Arguments

...

vectors of class factor or character, or a list/data.frame of such vectors.

weight

optional vector of weights. The default uses uniform weights of 1.

na.rm

logical, whether to remove NA values.

prop

logical, whether to return proportions or counts. Default returns counts.

return_tibble

logical, whether to return a tibble or named vector.

normwt

logical, whether to normalize weights such that they sum to 1.

Value

a vector or tibble of counts or proportions by group


[8]ページ先頭

©2009-2025 Movatter.jp