vignettes/xmar.R

## ----knitr_init, echo=FALSE, cache=FALSE, warning=FALSE, message=FALSE--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
options(knitr.table.format = "html")
options(max.print="75", scipen=999, width = 800)
knitr::opts_chunk$set(echo=FALSE,
	             cache=FALSE,
               prompt=FALSE,
               tidy=TRUE,
               root.dir = "..",
               fig.height = 8,
               fig.width = 20,
               comment=NA,
               message=FALSE,
               warning=FALSE)
knitr::opts_knit$set(width=100, figr.prefix = T, figr.link = T)
knitr::knit_hooks$set(inline = function(x) {
  prettyNum(x, big.mark=",")
})

## ----variableTbl, echo = F, message = FALSE, warning=FALSE--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
variables <- read.csv("../data/variables.csv")
knitr::kable(variables) %>%  
  kableExtra::kable_styling(bootstrap_options = c("hover", "condensed", "responsive"), full_width = T, position = "center")

## ----edaOpinionStats, echo = F, message = FALSE, warning=FALSE----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(eda$opinion$stats, align = c("l", rep("c", 5))) %>%  
  kableExtra::kable_styling(bootstrap_options = c("hover", "condensed", "responsive"), full_width = T, position = "center")

## ----edaOpinionPlot, echo = F, message = FALSE, warning=FALSE-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
eda$opinion$plot

## ----edaPeriodStats, echo = F, message = FALSE, warning=FALSE-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(eda$period$stats, align = c("l", rep("c", 5))) %>%  
  kableExtra::kable_styling(bootstrap_options = c("hover", "condensed", "responsive"), full_width = T, position = "center")

## ----edaPeriodPlot, echo = F, message = FALSE, warning=FALSE------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
eda$period$plot

## ----edaAgeStats, echo = F, message = FALSE, warning=FALSE--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(eda$age$stats, align = c("l", rep("c", 5))) %>%  
  kableExtra::kable_styling(bootstrap_options = c("hover", "condensed", "responsive"), full_width = T, position = "center")

## ----edaAgePlot, echo = F, message = FALSE, warning=FALSE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
eda$age$plot

## ----edaGenderStats, echo = F, message = FALSE, warning=FALSE-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(eda$gender$stats, align = c("l", rep("c", 5))) %>%  
  kableExtra::kable_styling(bootstrap_options = c("hover", "condensed", "responsive"), full_width = T, position = "center")

## ----edaGenderPlot, echo = F, message = FALSE, warning=FALSE------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
eda$gender$plot

## ----edaEducationStats, echo = F, message = FALSE, warning=FALSE--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(eda$educ$stats, align = c("l", rep("c", 5))) %>%  
  kableExtra::kable_styling(bootstrap_options = c("hover", "condensed", "responsive"), full_width = T, position = "center")

## ----edaEducationPlot, echo = F, message = FALSE, warning=FALSE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
eda$educ$plot

## ----edaRegionStats, echo = F, message = FALSE, warning=FALSE-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(eda$region$stats, align = c("l", rep("c", 5))) %>%  
  kableExtra::kable_styling(bootstrap_options = c("hover", "condensed", "responsive"), full_width = T, position = "center")

## ----edaRegionPlot, echo = F, message = FALSE, warning=FALSE------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
eda$region$plot

## ----ageAnalysis, echo = F, message = FALSE, warning=FALSE--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
data0 <- xmar$bivariate$ageGroup %>% select(Opinion, Period, AgeGroup) 
data1 <- xmar$bivariate$ageGroup %>% filter(AgeGroup == "15-24") %>% select(Opinion, Period) 
data2 <- xmar$bivariate$ageGroup %>% filter(AgeGroup == "25-44") %>% select(Opinion, Period) 
data3 <- xmar$bivariate$ageGroup %>% filter(AgeGroup == "45-64") %>% select(Opinion, Period) 
data4 <- xmar$bivariate$ageGroup %>% filter(AgeGroup == "65+") %>% select(Opinion, Period) 

age0 <- X23D(data0)
age1 <- analyze(data1, y = "Opinion", x = "Period", success = "Traditional", scope = "Age Group 15-24", title = "Opinions by Period (Age Group 15-24)")
age2 <- analyze(data2, y = "Opinion", x = "Period", success = "Traditional", scope = "Age Group 25-45", title = "Opinions by Period (Age Group 25-44)")
age3 <- analyze(data3, y = "Opinion", x = "Period", success = "Traditional", scope = "Age Group 45-65", title = "Opinions by Period (Age Group 45-64)")
age4 <- analyze(data4, y = "Opinion", x = "Period", success = "Traditional", scope = "Age Group 65+", title = "Opinions by Period (Age Group 65+)")
age <- rbind(age1$test$df, age2$test$df, age3$test$df, age4$test$df)

## ----ageExp, echo = F, message = FALSE, warning=FALSE, results = 'asis'-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
stargazer::stargazer(format(age0$expected, quote = FALSE), type = 'html')

## ----age1Plot, echo = F, message = FALSE, warning=FALSE, results = 'html'-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
age1$plots$observed

## ----age1ZTest, echo = F, results='html'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(age1$test$df) %>%  
  kableExtra::kable_styling(bootstrap_options = c("striped", "condensed"), full_width = T, position = "center")

## ----age2Plot, echo = F, message = FALSE, warning=FALSE-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
age2$plots$observed

## ----age2ZTest, echo = F, results='html'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(age2$test$df) %>%  
  kableExtra::kable_styling(bootstrap_options = c("striped", "condensed"), full_width = T, position = "center")

## ----age3Plot, echo = F, message = FALSE, warning=FALSE-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
age3$plots$observed

## ----age3ZTest, echo = F, results='html'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(age3$test$df) %>%  
  kableExtra::kable_styling(bootstrap_options = c("striped", "condensed"), full_width = T, position = "center")

## ----age4Plot, echo = F, message = FALSE, warning=FALSE-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
age4Plot <- age4$plots$observed

## ----age4ZTest, echo = F, results='html'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(age4$test$df) %>%  
  kableExtra::kable_styling(bootstrap_options = c("striped", "condensed"), full_width = T, position = "center")

## ----genderAnalysis, echo = F, message = FALSE, warning=FALSE-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
data0 <- xmar$bivariate$gender %>% select(Opinion, Period, Gender) 
data1 <- xmar$bivariate$gender %>% filter(Gender == "Male") %>% select(Opinion, Period) 
data2 <- xmar$bivariate$gender %>% filter(Gender == "Female") %>% select(Opinion, Period) 

gender0 <- X23D(data0)
gender1 <- analyze(data1, y = "Opinion", x = "Period", success = "Traditional", scope = "Male",   title = "Opinions by Period (Male)")
gender2 <- analyze(data2, y = "Opinion", x = "Period", success = "Traditional", scope = "Female", title = "Opinions by Period (Female)")
gender <- rbind(gender1$test$df, gender2$test$df)

## ----genderExp, echo = F, message = FALSE, warning=FALSE, results = 'asis'----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
stargazer::stargazer(format(gender0$expected, quote = FALSE), type = 'html')

## ----gender1Plot, echo = F, message = FALSE, warning=FALSE--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
gender1$plots$observed

## ----gender1ZTest, echo = F, results='html'-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(gender1$test$df) %>%  
  kableExtra::kable_styling(bootstrap_options = c("striped", "condensed"), full_width = T, position = "center")

## ----gender2Plot, echo = F, message = FALSE, warning=FALSE--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
gender2$plots$observed

## ----gender2ZTest, echo = F, results='html'-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(gender2$test$df) %>%  
  kableExtra::kable_styling(bootstrap_options = c("striped", "condensed"), full_width = T, position = "center")

## ----educAnalysis, echo = F, messeduc = FALSE, warning=FALSE------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
data0 <- xmar$bivariate$educ %>% select(Opinion, Period, Educ) 
data1 <- xmar$bivariate$educ %>% filter(Educ == "High School") %>% select(Opinion, Period) 
data2 <- xmar$bivariate$educ %>% filter(Educ == "Community College") %>% select(Opinion, Period) 
data3 <- xmar$bivariate$educ %>% filter(Educ == "UnderGraduate") %>% select(Opinion, Period) 
data4 <- xmar$bivariate$educ %>% filter(Educ == "Graduate") %>% select(Opinion, Period) 
data5 <- xmar$bivariate$educ %>% filter(Educ == "Post-Graduate") %>% select(Opinion, Period) 

educ0 <- X23D(data0)
educ1 <- analyze(data1, y = "Opinion", x = "Period", success = "Traditional", scope = "High School Graduate", title = "Opinions by Period (High School)")
educ2 <- analyze(data2, y = "Opinion", x = "Period", success = "Traditional", scope = "Community College Graduate", title = "Opinions by Period (Community College)")
educ3 <- analyze(data3, y = "Opinion", x = "Period", success = "Traditional", scope = "UnderGraduate", title = "Opinions by Period (UnderGraduate)")
educ4 <- analyze(data4, y = "Opinion", x = "Period", success = "Traditional", scope = "Graduate", title = "Opinions by Period (Graduate)")
educ5 <- analyze(data4, y = "Opinion", x = "Period", success = "Traditional", scope = "Post-Graduate", title = "Opinions by Period (Post-Graduate)")
educ <- rbind(educ1$test$df, educ2$test$df, educ3$test$df, educ4$test$df, educ5$test$df)

## ----educExp, echo = F, message = FALSE, warning=FALSE, results = 'asis'------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
stargazer::stargazer(format(educ0$expected, quote = FALSE), type = 'html')

## ----educ1Plot, echo = F, messeduc = FALSE, warning=FALSE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
educ1$plots$observed

## ----educ1ZTest, echo = F, results='html'-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(educ1$test$df) %>%  
  kableExtra::kable_styling(bootstrap_options = c("striped", "condensed"), full_width = T, position = "center")

## ----educ2Plot, echo = F, messeduc = FALSE, warning=FALSE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
educ2$plots$observed

## ----educ2ZTest, echo = F, results='html'-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(educ2$test$df) %>%  
  kableExtra::kable_styling(bootstrap_options = c("striped", "condensed"), full_width = T, position = "center")

## ----educ3Plot, echo = F, messeduc = FALSE, warning=FALSE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
educ3$plots$observed

## ----educ3ZTest, echo = F, results='html'-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(educ3$test$df) %>%  
  kableExtra::kable_styling(bootstrap_options = c("striped", "condensed"), full_width = T, position = "center")

## ----educ4Plot, echo = F, messeduc = FALSE, warning=FALSE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
educ4$plots$observed

## ----educ4ZTest, echo = F, results='html'-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(educ4$test$df) %>%  
  kableExtra::kable_styling(bootstrap_options = c("striped", "condensed"), full_width = T, position = "center")

## ----educ5Plot, echo = F, messeduc = FALSE, warning=FALSE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
educ5$plots$observed

## ----educ5ZTest, echo = F, results='html'-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(educ5$test$df) %>%  
  kableExtra::kable_styling(bootstrap_options = c("striped", "condensed"), full_width = T, position = "center")

## ----regionAnalysis, echo = F, message = FALSE, warning=FALSE-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
data0 <- xmar$bivariate$region %>% select(Opinion, Period, Region) 
data1 <- xmar$bivariate$region %>% filter(Region == "Northeast") %>% select(Opinion, Period) 
data2 <- xmar$bivariate$region %>% filter(Region == "Midwest") %>% select(Opinion, Period) 
data3 <- xmar$bivariate$region %>% filter(Region == "South") %>% select(Opinion, Period) 
data4 <- xmar$bivariate$region %>% filter(Region == "Mountain") %>% select(Opinion, Period) 
data5 <- xmar$bivariate$region %>% filter(Region == "Pacific") %>% select(Opinion, Period) 

region0 <- X23D(data0)
region1 <- analyze(data1, y = "Opinion", x = "Period", success = "Traditional", scope = "Northeast Region", title = "Opinions by Period (Northeast)")
region2 <- analyze(data2, y = "Opinion", x = "Period", success = "Traditional", scope = "Midwest Region", title = "Opinions by Period (Midwest)")
region3 <- analyze(data3, y = "Opinion", x = "Period", success = "Traditional", scope = "South Region", title = "Opinions by Period (South)")
region4 <- analyze(data4, y = "Opinion", x = "Period", success = "Traditional", scope = "Mountain Region", title = "Opinions by Period (Mountain)")
region5 <- analyze(data4, y = "Opinion", x = "Period", success = "Traditional", scope = "Pacific Region", title = "Opinions by Period (Pacific)")
region <- rbind(region1$test$df, region2$test$df, region3$test$df, region4$test$df, region5$test$df)

## ----regionExp, echo = F, message = FALSE, warning=FALSE, results = 'asis'----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
stargazer::stargazer(format(region0$expected, quote = FALSE), type = 'html')

## ----region1Plot, echo = F, message = FALSE, warning=FALSE--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
region1$plots$observed

## ----region1ZTest, echo = F, results='html'-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(region1$test$df) %>%  
  kableExtra::kable_styling(bootstrap_options = c("striped", "condensed"), full_width = T, position = "center")

## ----region2Plot, echo = F, message = FALSE, warning=FALSE--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
region2$plots$observed

## ----region2ZTest, echo = F, results='html'-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(region2$test$df) %>%  
  kableExtra::kable_styling(bootstrap_options = c("striped", "condensed"), full_width = T, position = "center")

## ----region3Plot, echo = F, message = FALSE, warning=FALSE--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
region3$plots$observed

## ----region3ZTest, echo = F, results='html'-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(region3$test$df) %>%  
  kableExtra::kable_styling(bootstrap_options = c("striped", "condensed"), full_width = T, position = "center")

## ----region4Plot, echo = F, message = FALSE, warning=FALSE--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
region4$plots$observed

## ----region4ZTest, echo = F, results='html'-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(region4$test$df) %>%  
  kableExtra::kable_styling(bootstrap_options = c("striped", "condensed"), full_width = T, position = "center")

## ----region5Plot, echo = F, message = FALSE, warning=FALSE--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
region5$plots$observed

## ----region5ZTest, echo = F, results='html'-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(region5$test$df) %>%  
  kableExtra::kable_styling(bootstrap_options = c("striped", "condensed"), full_width = T, position = "center")

## ----discussion, echo = F, message = FALSE, warning=FALSE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ageGroupDiscussion <- discussion(age)
genderDiscussion <- discussion(gender)
educDiscussion <- discussion(educ)

## ----ageDisc, echo = F, results='html'----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(ageGroupDiscussion$data, align = c("l", rep("c", 4))) %>%  
  kableExtra::kable_styling(bootstrap_options = c("striped", "condensed"), full_width = T, position = "center")

## ----agePlot, echo = F, message = FALSE, warning=FALSE------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ageGroupDiscussion$plot

## ----genderDisc, echo = F, results='html'-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(genderDiscussion$data, align = c("l", rep("c", 4))) %>%  
  kableExtra::kable_styling(bootstrap_options = c("striped", "condensed"), full_width = T, position = "center")

## ----genderPlot, echo = F, message = FALSE, warning=FALSE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
genderDiscussion$plot

## ----educDisc, echo = F, results='html'---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
knitr::kable(educDiscussion$data, align = c("l", rep("c", 4))) %>%  
  kableExtra::kable_styling(bootstrap_options = c("striped", "condensed"), full_width = T, position = "center")

## ----educPlot, echo = F, message = FALSE, warning=FALSE-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
educDiscussion$plot

## ----a-preprocess, code=readLines('../R/preprocess.R')[11:47], echo=TRUE, eval=FALSE------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#  preprocess <- function(GSS) {
#  
#    #---------------------------------------------------------------------------#
#    #                            Period Variable                                #
#    #---------------------------------------------------------------------------#
#    periodVar <- GSS %>%
#      dplyr::mutate(
#        Period = ifelse(YEAR < 2000, "Prior to 2000", "Since 2000")) %>%
#      dplyr::select(Period)
#  
#    # Order factors
#    periodVar$Period <- factor(periodVar$Period,
#                                levels = c("Prior to 2000", "Since 2000"))
#    periodVar <- periodVar %>% filter(Period != "NA")
#  
#  
#    #---------------------------------------------------------------------------#
#    #                           Opinion Variable                                #
#    #---------------------------------------------------------------------------#
#    opinionVar <- GSS %>%
#      dplyr::mutate(
#        Opinion = ifelse(XMARSEX %in% c(1,2), "Traditional",
#                         ifelse(XMARSEX  %in%  c(3,4),
#                                "Non-Traditional", "NA"))) %>%
#      dplyr::select(Opinion)
#  
#    # Order factors
#    opinionVar$Opinion <- factor(opinionVar$Opinion,
#                                 levels = c("Traditional", "Non-Traditional"))
#    opinionVar <- opinionVar %>% filter(Opinion != "NA")
#    #---------------------------------------------------------------------------#
#    #                            Gender Variable                                #
#    #---------------------------------------------------------------------------#
#    genderVar <- GSS %>%
#      dplyr::mutate(
#        Gender = ifelse(SEX == 1, "Male",
#                        ifelse(SEX == 2, "Female", "NA"))) %>%

## ----a-eda, code=readLines('../R/univariate.R')[11:104], echo=TRUE, eval=FALSE------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#  univariate <- function(xmar) {
#  
#    analyze <- function(data, var) {
#  
#      me <- 0.05
#      alpha <- 0.05
#      z <- (1-alpha/2)
#  
#      stats <- data %>%
#        mutate(Level = data[,1],
#               `Minimum N` = round(p * (1-p) / (me / qnorm(z))^2, 0),
#               N = N,
#               Proportion = round(p, 2),
#               Cumulative = round(cumsum(Proportion), 2),
#               `Confidence Interval (95%)` =
#                 paste0("[",
#                        round(Proportion -
#                                (qnorm(z) *
#                                   sqrt(Proportion * (1 - Proportion) / N)),3),
#                        ", ",
#                        round(Proportion +
#                                (qnorm(z) *
#                                   sqrt(Proportion * (1 - Proportion) / N)),3),
#                        "]"),
#               pos = N / 2)
#  
#      barplot <- ggplot2::ggplot(data = stats,
#                                 ggplot2::aes(x = Level, y = N, fill = Level)) +
#        ggplot2::geom_bar(stat='identity') + ggplot2::theme_minimal(base_size = 24) +
#        ggplot2::geom_text(
#          data = stats,
#          ggplot2::aes(x = Level, y = pos,
#                       label = paste0(N, " (",round(Proportion * 100, 0),"%)")),
#          colour="black", family="Tahoma", size = 8) +
#        ggplot2::theme(text = ggplot2::element_text(family="Open Sans"),
#                       axis.title.x = ggplot2::element_blank(),
#                       axis.ticks.x = ggplot2::element_blank(),
#                       legend.position = "none") +
#        ggplot2::scale_fill_brewer(palette = 'Greens') +
#        ggplot2::ggtitle(paste('Frequency and Proportion of Responses by', var))
#  
#      stats <- stats %>% select(Level, `Minimum N`, N, Proportion, Cumulative,
#                                `Confidence Interval (95%)`)
#  
#      analysis <- list(
#        stats = stats,
#        plot = barplot
#      )
#  
#      return(analysis)
#    }
#  
#    # Format Data
#    period <- as.data.frame(xmar$period %>% select(Period) %>% group_by(Period) %>%
#                               summarize(N = n()) %>%
#                               mutate(p = N/ sum(N)), row.names = NULL)
#    opinion <- as.data.frame(xmar$opinion %>% select(Opinion) %>% group_by(Opinion) %>%
#                             summarize(N = n()) %>%
#                             mutate(p = N/ sum(N)), row.names = NULL)
#    age <- as.data.frame(xmar$age %>% select(AgeGroup) %>% group_by(AgeGroup) %>%
#                           summarize(N = n()) %>%
#                           mutate(p = N/ sum(N)), row.names = NULL)
#    gender <- as.data.frame(xmar$gender %>% select(Gender) %>% group_by(Gender) %>%
#                              summarize(N = n()) %>%
#                              mutate(p = N/ sum(N)), row.names = NULL)
#    educ <- as.data.frame(xmar$educ %>% select(Educ) %>% group_by(Educ) %>%
#                              summarize(N = n()) %>%
#                              mutate(p = N/ sum(N)), row.names = NULL)
#    region <- as.data.frame(xmar$region %>% select(Region) %>% group_by(Region) %>%
#                              summarize(N = n()) %>%
#                              mutate(p = N/ sum(N)), row.names = NULL)
#  
#    # Conduct Analysis
#    period <- analyze(period, "Period")
#    opinion <- analyze(opinion, "Opinion")
#    age <- analyze(age, "Age Group")
#    gender <- analyze(gender, "Gender")
#    educ <- analyze(educ, "Education")
#    region <- analyze(region, "Region")
#  
#    # Return analysis
#    analysis <- list(
#      period = period,
#      opinion = opinion,
#      age = age,
#      gender = gender,
#      educ = educ,
#      region = region
#      )
#    return(analysis)
#  }
#  NA
#  NA
#  NA

## ----a-eda, code=readLines('../R/univariate.R')[11:104], echo=TRUE, eval=FALSE------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#  univariate <- function(xmar) {
#  
#    analyze <- function(data, var) {
#  
#      me <- 0.05
#      alpha <- 0.05
#      z <- (1-alpha/2)
#  
#      stats <- data %>%
#        mutate(Level = data[,1],
#               `Minimum N` = round(p * (1-p) / (me / qnorm(z))^2, 0),
#               N = N,
#               Proportion = round(p, 2),
#               Cumulative = round(cumsum(Proportion), 2),
#               `Confidence Interval (95%)` =
#                 paste0("[",
#                        round(Proportion -
#                                (qnorm(z) *
#                                   sqrt(Proportion * (1 - Proportion) / N)),3),
#                        ", ",
#                        round(Proportion +
#                                (qnorm(z) *
#                                   sqrt(Proportion * (1 - Proportion) / N)),3),
#                        "]"),
#               pos = N / 2)
#  
#      barplot <- ggplot2::ggplot(data = stats,
#                                 ggplot2::aes(x = Level, y = N, fill = Level)) +
#        ggplot2::geom_bar(stat='identity') + ggplot2::theme_minimal(base_size = 24) +
#        ggplot2::geom_text(
#          data = stats,
#          ggplot2::aes(x = Level, y = pos,
#                       label = paste0(N, " (",round(Proportion * 100, 0),"%)")),
#          colour="black", family="Tahoma", size = 8) +
#        ggplot2::theme(text = ggplot2::element_text(family="Open Sans"),
#                       axis.title.x = ggplot2::element_blank(),
#                       axis.ticks.x = ggplot2::element_blank(),
#                       legend.position = "none") +
#        ggplot2::scale_fill_brewer(palette = 'Greens') +
#        ggplot2::ggtitle(paste('Frequency and Proportion of Responses by', var))
#  
#      stats <- stats %>% select(Level, `Minimum N`, N, Proportion, Cumulative,
#                                `Confidence Interval (95%)`)
#  
#      analysis <- list(
#        stats = stats,
#        plot = barplot
#      )
#  
#      return(analysis)
#    }
#  
#    # Format Data
#    period <- as.data.frame(xmar$period %>% select(Period) %>% group_by(Period) %>%
#                               summarize(N = n()) %>%
#                               mutate(p = N/ sum(N)), row.names = NULL)
#    opinion <- as.data.frame(xmar$opinion %>% select(Opinion) %>% group_by(Opinion) %>%
#                             summarize(N = n()) %>%
#                             mutate(p = N/ sum(N)), row.names = NULL)
#    age <- as.data.frame(xmar$age %>% select(AgeGroup) %>% group_by(AgeGroup) %>%
#                           summarize(N = n()) %>%
#                           mutate(p = N/ sum(N)), row.names = NULL)
#    gender <- as.data.frame(xmar$gender %>% select(Gender) %>% group_by(Gender) %>%
#                              summarize(N = n()) %>%
#                              mutate(p = N/ sum(N)), row.names = NULL)
#    educ <- as.data.frame(xmar$educ %>% select(Educ) %>% group_by(Educ) %>%
#                              summarize(N = n()) %>%
#                              mutate(p = N/ sum(N)), row.names = NULL)
#    region <- as.data.frame(xmar$region %>% select(Region) %>% group_by(Region) %>%
#                              summarize(N = n()) %>%
#                              mutate(p = N/ sum(N)), row.names = NULL)
#  
#    # Conduct Analysis
#    period <- analyze(period, "Period")
#    opinion <- analyze(opinion, "Opinion")
#    age <- analyze(age, "Age Group")
#    gender <- analyze(gender, "Gender")
#    educ <- analyze(educ, "Education")
#    region <- analyze(region, "Region")
#  
#    # Return analysis
#    analysis <- list(
#      period = period,
#      opinion = opinion,
#      age = age,
#      gender = gender,
#      educ = educ,
#      region = region
#      )
#    return(analysis)
#  }
#  NA
#  NA
#  NA
DataScienceSalon/xmar documentation built on May 28, 2019, 12:24 p.m.