This Document

This document contains a set of tasks that you need to do to complete the optional lab assignment. The lab assignment is focused on analyzing data related to the domestic politics and foreign policies of the United States since 1945. Successfully completing this assignment will require you to implement most of the techniques we’ve covered in the lab, including various descriptive statistics, creating new variables based on existing variables, creating numeric variables based on categoric variables and vice-versa, visualizing variables on their own and in terms of other variables, and statistically analyzing bivariate relationships.

Two things are important to note about this lab assignment. First, this lab is completely optional. However, if you complete the lab, I will replace your lowest grade of the semester with the grade you get on this lab if your grade on this lab is higher than your previous lowest lab grade. If your grade on this lab assign is worse than all of your other lab assignments, I will not replace your previous lowest grade with your grade on this lab assignment. Framed differently, completing this lab assignment can potentially raise your grade in the lab but it cannot hurt your grade.

Second, this is the only RStudio/RMarkdown file associated with the optional lab. The reason for this is that everything you need to know to complete the tasks in the assignment have been covered in the previous three lab assignments. Accordingly, there are no additional documents that provide you with information about the lab assignment and/or the code needed to complete the lab assignment.

Add your Name and the Date

Add your name and date in the lines underneath this document’s title.

Set Your Working Directory

You need to set your working directory in this section.

setwd("~/Documents/OptionalLab")

Loading Packages

You’ll need to load the following packages to complete the lab assignment.

library("dplyr")
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library("tidyverse")
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ readr     2.1.5
## ✔ ggplot2   3.5.1     ✔ stringr   1.5.1
## ✔ lubridate 1.9.3     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library("openxlsx")
library("forcats")
library("corrr")
library("janitor")
## 
## Attaching package: 'janitor'
## 
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test

Loading Data Set

You need to load the data set you’ll use for the lab assignment in this section.

read.xlsx("~/Documents/OptionalLab/OptionalAssignmentData.xlsx")
##    year rep.ideology dem.ideology polarization president.party divided war
## 1  1945    0.3034564   -0.2646040    0.5680604        democrat       0   1
## 2  1946    0.3034564   -0.2646040    0.5680604        democrat       0   0
## 3  1947    0.2853439   -0.2373247    0.5226686        democrat       1   0
## 4  1948    0.2853439   -0.2373247    0.5226686        democrat       1   0
## 5  1949    0.2770057   -0.2755693    0.5525750        democrat       1   0
## 6  1950    0.2770057   -0.2755693    0.5525750        democrat       0   1
## 7  1951    0.2800193   -0.2623333    0.5423527        democrat       0   1
## 8  1952    0.2800193   -0.2623333    0.5423527        democrat       0   1
## 9  1953    0.2776577   -0.2533440    0.5310017      republican       0   1
## 10 1954    0.2776577   -0.2533440    0.5310017      republican       0   0
## 11 1955    0.2772512   -0.2790979    0.5563491      republican       1   0
## 12 1956    0.2772512   -0.2790979    0.5563491      republican       1   0
## 13 1957    0.2682206   -0.2827792    0.5509998      republican       1   0
## 14 1958    0.2682206   -0.2827792    0.5509998      republican       1   0
## 15 1959    0.2667179   -0.2957098    0.5624277      republican       1   0
## 16 1960    0.2667179   -0.2957098    0.5624277      republican       1   0
## 17 1961    0.2623693   -0.2772857    0.5396550        democrat       0   0
## 18 1962    0.2623693   -0.2772857    0.5396550        democrat       0   0
## 19 1963    0.2560166   -0.2920458    0.5480624        democrat       0   0
## 20 1964    0.2560166   -0.2920458    0.5480624        democrat       0   1
## 21 1965    0.2458252   -0.3007641    0.5465893        democrat       0   1
## 22 1966    0.2458252   -0.3007641    0.5465893        democrat       0   1
## 23 1967    0.2430265   -0.2955200    0.5385465        democrat       0   1
## 24 1968    0.2430265   -0.2955200    0.5385465        democrat       0   1
## 25 1969    0.2546231   -0.3097680    0.5643911      republican       1   1
## 26 1970    0.2546231   -0.3097680    0.5643911      republican       1   1
## 27 1971    0.2544000   -0.3105483    0.5649483      republican       1   1
## 28 1972    0.2544000   -0.3105483    0.5649483      republican       1   1
## 29 1973    0.2609436   -0.3234675    0.5844111      republican       1   1
## 30 1974    0.2609436   -0.3234675    0.5844111      republican       1   0
## 31 1975    0.2650136   -0.3152891    0.5803027      republican       1   0
## 32 1976    0.2650136   -0.3152891    0.5803027      republican       1   0
## 33 1977    0.2671497   -0.3059048    0.5730544        democrat       0   0
## 34 1977    0.2671497   -0.3059048    0.5730544        democrat       0   0
## 35 1978    0.2671497   -0.3059048    0.5730544        democrat       0   0
## 36 1979    0.2947826   -0.3013238    0.5961065        democrat       0   0
## 37 1980    0.2947826   -0.3013238    0.5961065        democrat       0   0
## 38 1981    0.3065051   -0.3004008    0.6069059      republican       1   0
## 39 1982    0.3065051   -0.3004008    0.6069059      republican       1   0
## 40 1983    0.3256527   -0.3017868    0.6274395      republican       1   0
## 41 1984    0.3256527   -0.3017868    0.6274395      republican       1   0
## 42 1985    0.3337637   -0.3095292    0.6432929      republican       1   0
## 43 1986    0.3337637   -0.3095292    0.6432929      republican       1   0
## 44 1987    0.3353128   -0.3093168    0.6446296      republican       1   0
## 45 1988    0.3353128   -0.3093168    0.6446296      republican       1   0
## 46 1989    0.3396167   -0.3130377    0.6526544      republican       1   0
## 47 1990    0.3396167   -0.3130377    0.6526544      republican       1   1
## 48 1991    0.3447059   -0.3142185    0.6589244      republican       1   1
## 49 1992    0.3447059   -0.3142185    0.6589244      republican       1   0
## 50 1993    0.3709333   -0.3338893    0.7048226        democrat       0   0
## 51 1994    0.3709333   -0.3338893    0.7048226        democrat       0   0
## 52 1995    0.3965678   -0.3613702    0.7579380        democrat       1   0
## 53 1996    0.3965678   -0.3613702    0.7579380        democrat       1   0
## 54 1997    0.4015281   -0.3755660    0.7770942        democrat       1   1
## 55 1998    0.4015281   -0.3755660    0.7770942        democrat       1   1
## 56 1999    0.4040000   -0.3728310    0.7768310        democrat       1   1
## 57 2000    0.4040000   -0.3728310    0.7768310        democrat       1   1
## 58 2001    0.4096140   -0.3767710    0.7863851      republican       1   1
## 59 2002    0.4096140   -0.3767710    0.7863851      republican       1   1
## 60 2003    0.4167229   -0.3762212    0.7929441      republican       0   1
## 61 2004    0.4167229   -0.3762212    0.7929441      republican       0   0
## 62 2005    0.4227532   -0.3871626    0.8099158      republican       0   0
## 63 2006    0.4227532   -0.3871626    0.8099158      republican       0   0
## 64 2007    0.4394951   -0.3684033    0.8078984      republican       1   0
## 65 2008    0.4394951   -0.3684033    0.8078984      republican       1   0
## 66 2009    0.4564590   -0.3488792    0.8053383        democrat       0   0
## 67 2010    0.4564590   -0.3488792    0.8053383        democrat       0   0
## 68 2011    0.4675224   -0.3932000    0.8607224        democrat       1   0
## 69 2012    0.4675224   -0.3932000    0.8607224        democrat       1   0
## 70 2013    0.4820167   -0.3842892    0.8663059        democrat       1   0
## 71 2014    0.4820167   -0.3842892    0.8663059        democrat       1   0
## 72 2015    0.4802709   -0.3945895    0.8748604        democrat       1   0
## 73 2016    0.4802709   -0.3945895    0.8748604        democrat       1   0
## 74 2017    0.4891440   -0.3888150    0.8779590      republican       0   0
## 75 2018    0.4891440   -0.3888150    0.8779590      republican       0   0
## 76 2019    0.5003077   -0.3710958    0.8714035      republican       1   0
## 77 2020    0.5003077   -0.3710958    0.8714035      republican       1   0
## 78 2021    0.5025291   -0.3792069    0.8817360        democrat       0   0
## 79 2022    0.5025291   -0.3792069    0.8817360        democrat       0   0
## 80 2023    0.5081354   -0.3841050    0.8922404        democrat       1   0
##    dissimilar.foreign.policy defense.burden          gdp econ.percent.growth
## 1                  0.1938571      36.775460 2.598616e+12                  NA
## 2                  0.6026000      19.388044 2.479307e+12         -4.59126024
## 3                  0.6086000       6.286465 2.430213e+12         -1.98013267
## 4                  0.6302000       4.689680 2.494227e+12          2.63409485
## 5                  0.6210000       5.858731 2.466941e+12         -1.09397212
## 6                  0.6641667       6.063156 2.562489e+12          3.87312329
## 7                  0.7608333      13.020397 2.729119e+12          6.50268392
## 8                  0.7723333      18.013726 2.823505e+12          3.45846067
## 9                  0.7825000      17.644699 2.986132e+12          5.75976837
## 10                 0.7143333      15.123231 3.001100e+12          0.50125209
## 11                 0.7178333      13.842830 3.104892e+12          3.45846067
## 12                 0.6866667      13.753200 3.218704e+12          3.66558465
## 13                 0.6675000      14.219132 3.316728e+12          3.04545340
## 14                 0.6673333      14.538489 3.313413e+12         -0.09995002
## 15                 0.6716667      13.900462 3.543018e+12          6.92954782
## 16                 0.5685000      13.317682 3.600162e+12          1.61286854
## 17                 0.5538333      13.453161 3.747088e+12          4.08107742
## 18                 0.5056667      13.909405 3.962911e+12          5.75976837
## 19                 0.4833333      13.248999 4.149463e+12          4.70744110
## 20                 0.4716667      12.243739 4.388462e+12          5.75976837
## 21                 0.4783333      11.727453 4.627325e+12          5.44296451
## 22                 0.4635000      14.443071 4.888956e+12          5.65406147
## 23                 0.4856667      15.432332 5.103767e+12          4.39378949
## 24                 0.4736667      15.770737 5.333346e+12          4.49823549
## 25                 0.4836667      15.378360 5.512283e+12          3.35505392
## 26                 0.4791667      14.592528 5.551004e+12          0.70245573
## 27                 0.4580000      13.635375 5.714340e+12          2.94245945
## 28                 0.4566667      13.491920 5.977359e+12          4.60278599
## 29                 0.4576667      13.061301 6.227524e+12          4.18521055
## 30                 0.4511667      13.836012 6.436461e+12          3.35505392
## 31                 0.4453333      14.604198 6.455800e+12          0.30045045
## 32                 0.4425000      14.168532 6.652408e+12          3.04545340
## 33                 0.4488333      14.990232 6.972535e+12          4.81220091
## 34                 0.4488333      14.990232 6.972535e+12          0.00000000
## 35                 0.4775000      15.605642 7.235351e+12          3.76930208
## 36                 0.4895000      16.648605 7.583531e+12          4.81220091
## 37                 0.5010000      19.466644 7.636802e+12          0.70245573
## 38                 0.5000000      22.469538 7.806673e+12          2.22437845
## 39                 0.5131667      26.361232 7.698141e+12         -1.39024557
## 40                 0.5101667      27.905687 8.028349e+12          4.28944788
## 41                 0.5175000      28.654218 8.524794e+12          6.18365465
## 42                 0.5175000      28.357958 8.899356e+12          4.39378949
## 43                 0.5175000      29.712286 9.197934e+12          3.35505392
## 44                 0.5050000      29.371509 9.592474e+12          4.28944788
## 45                 0.5050000      28.838107 1.003400e+13          4.60278599
## 46                 0.4408333      29.683449 1.020603e+13          1.71453223
## 47                 0.4205000      28.390565 1.047487e+13          2.63409485
## 48                 0.4253750      26.045611 1.033958e+13         -1.29158650
## 49                 0.4228750      27.197552 1.082631e+13          4.70744110
## 50                 0.4115000      27.598776 1.105606e+13          2.12220516
## 51                 0.4092500      26.256769 1.144988e+13          3.56197088
## 52                 0.4120000      23.951835 1.188146e+13          3.76930208
## 53                 0.4113750      22.827254 1.244077e+13          4.70744110
## 54                 0.4113750      21.880486 1.292262e+13          3.87312329
## 55                 0.4113750      21.004693 1.361237e+13          5.33757425
## 56                 0.4190000      21.227090 1.405501e+13          3.25175053
## 57                 0.4176250      21.204272 1.458479e+13          3.76930208
## 58                 0.4176250      22.036619 1.493905e+13          2.42903179
## 59                 0.4150000      23.946380 1.486455e+13         -0.49875208
## 60                 0.4150000      26.781481 1.542484e+13          3.76930208
## 61                 0.4143750      29.174999 1.594235e+13          3.35505392
## 62                 0.4143750      30.424167 1.659297e+13          4.08107742
## 63                 0.4122500      31.386634 1.694510e+13          2.12220516
## 64                 0.4122500      32.707278 1.720120e+13          1.51130646
## 65                 0.4101250      36.248086 1.732203e+13          0.70245573
## 66                 0.4108750      39.305352 1.718400e+13         -0.79680852
## 67                 0.4108750      40.850397 1.730471e+13          0.70245573
## 68                 0.4086250      39.581246 1.768964e+13          2.22437845
## 69                 0.4085000      37.197760 1.795698e+13          1.51130646
## 70                        NA      33.003619 1.854090e+13          3.25175053
## 71                        NA      31.179844 1.897228e+13          2.32665395
## 72                        NA      32.001425 1.901026e+13          0.20020013
## 73                        NA             NA           NA                  NA
## 74                        NA             NA           NA                  NA
## 75                        NA             NA           NA                  NA
## 76                        NA             NA           NA                  NA
## 77                        NA             NA           NA                  NA
## 78                        NA             NA           NA                  NA
## 79                        NA             NA           NA                  NA
## 80                        NA             NA           NA                  NA
##    foreign.aid.percent
## 1                   NA
## 2           0.33141427
## 3           0.05735751
## 4           0.08298984
## 5           0.06938981
## 6           0.03875136
## 7           0.03881649
## 8           0.06438465
## 9           0.08059470
## 10          0.06453518
## 11          0.07254750
## 12          0.08859813
## 13          0.09819885
## 14          0.07581042
## 15          0.13043124
## 16          0.08427406
## 17          0.10678153
## 18          0.11600428
## 19          0.12899344
## 20          0.08203374
## 21          0.10527884
## 22          0.10401764
## 23          0.13340083
## 24          0.09536614
## 25          0.09510187
## 26          0.08979995
## 27          0.05861356
## 28          0.05508882
## 29          0.10186727
## 30          0.09875081
## 31          0.09533907
## 32          0.04348905
## 33          0.03097931
## 34          0.09296907
## 35          0.10572214
## 36          0.11437631
## 37          0.10819735
## 38          0.08442568
## 39          0.09219788
## 40          0.08815590
## 41          0.08150589
## 42          0.09121489
## 43          0.06693787
## 44          0.05567689
## 45          0.06125100
## 46          0.06176950
## 47          0.06776508
## 48          0.07166684
## 49          0.06330642
## 50          0.05656860
## 51          0.05238960
## 52          0.05741737
## 53          0.04290701
## 54          0.03531344
## 55          0.03655715
## 56          0.03464265
## 57          0.02128918
## 58          0.02782833
## 59          0.03452385
## 60          0.02824071
## 61          0.05054988
## 62          0.04782374
## 63          0.04055921
## 64          0.04460680
## 65          0.04625167
## 66          0.05282596
## 67          0.05763035
## 68          0.05667333
## 69          0.06475394
## 70          0.06210703
## 71          0.06297460
## 72          0.06005571
## 73                  NA
## 74                  NA
## 75                  NA
## 76                  NA
## 77                  NA
## 78                  NA
## 79                  NA
## 80                  NA

Identify the Variables in the data set

You need to identify the names of the variables in the data set for the assignment in this section. You should consult the codebook in the “OptionalLabMaterials” folder for information about what the variables are measuring/capturing.

OptionalData <- read.xlsx("~/Documents/OptionalLab/OptionalAssignmentData.xlsx")
names(OptionalData)
##  [1] "year"                      "rep.ideology"             
##  [3] "dem.ideology"              "polarization"             
##  [5] "president.party"           "divided"                  
##  [7] "war"                       "dissimilar.foreign.policy"
##  [9] "defense.burden"            "gdp"                      
## [11] "econ.percent.growth"       "foreign.aid.percent"

Descriptive Statistics

You need to report a set of descriptive statistics in this section. First, identify the means of variables that identify 1) how much of its available economic resources the United States spends on its military; 2) how much of its available economic resources the United States spends on foreign aid; and 3) annual economic growth.

mean(OptionalData$defense.burden, na.rm = TRUE)
## [1] 21.33192
mean(OptionalData$foreign.aid.percent, na.rm = TRUE)
## [1] 0.07465675
mean(OptionalData$econ.percent.growth, na.rm = TRUE)
## [1] 2.866975

Second, you need to identify the median of variables that identify: 1) whether the United States had divided government and 2) whether the United States was involved in an interstate war.

median(OptionalData$divided, na.rm = TRUE)
## [1] 1
median(OptionalData$divided, na.rm = TRUE)
## [1] 1

Third, you need to identify the minimum, maximum, and range of variables that identify 1) the average ideology of Republican members of the House; 2) the average ideology of Democratic members of the House; and 3) the average polarization of the two parties in the House.

min(OptionalData$rep.ideology, na.rm = TRUE)
## [1] 0.2430265
max(OptionalData$rep.ideology, na.rm = TRUE)
## [1] 0.5081354
range(OptionalData$rep.ideology, na.rm = TRUE)
## [1] 0.2430265 0.5081354
min(OptionalData$dem.ideology, na.rm = TRUE)
## [1] -0.3945895
max(OptionalData$dem.ideology, na.rm = TRUE)
## [1] -0.2373247
range(OptionalData$dem.ideology, na.rm = TRUE)
## [1] -0.3945895 -0.2373247
min(OptionalData$polarization, na.rm = TRUE)
## [1] 0.5226686
max(OptionalData$polarization, na.rm = TRUE)
## [1] 0.8922404
range(OptionalData$polarization, na.rm = TRUE)
## [1] 0.5226686 0.8922404

Visualizing Variables

You need to create a set of graphs in this section. First, create a graph that plots the total number of years the United States was involved in an interstate war and the number of years that the United States was not involved in an interstate war. When making this graph, make sure to identify the total number of years the United States was and was not involved in an interstate war.

OptionalData <- OptionalData %>%
  mutate(war.label = ifelse(war == 1, "war", "no war"))
ggplot(data = OptionalData, mapping = aes(x = war.label)) + 
  geom_bar() + 
  geom_text(stat = 'count', aes(label = ..count..), vjust = -0.5) +  # Display counts on bars
  guides(fill = guide_legend("Number of Years the U.S. was and was not Involved in war")) +
  theme_void() + scale_x_discrete(labels = c("War", "No War"))
## Warning: The dot-dot notation (`..count..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(count)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

Second, create a single graph that plots the percentage of years that the United States was involved in an interstate war and the percentage of years that the United States was not involved in an interstate war. This graph should identify the percentage of total years the United States was and was not involved in a war on the graph.

OptionalData %>% count(war.label) -> OptionalData2
head(OptionalData2)
##   war.label  n
## 1    no war 56
## 2       war 24
ggplot(OptionalData2, aes(x="",y=n,fill = war.label)) + geom_bar(stat = "identity",width=1,color="white") +coord_polar("y",start = 0) + guides(fill=guide_legend("Years with and Without War"))+theme_void()

Third, you need to graph the distributions of variables that identify 1) how much of its available economic resources the United States spends on its military and 2) how much of its available economic resources the United States spends on foreign aid. Both of these distributions should be filled in with the color “red.”

ggplot(OptionalData,      # data argument
       aes(x = foreign.aid.percent)) +   # mapping and aesthetics arguments
  geom_histogram(fill = "red", bins = 60) +   # specifies bins
  labs(x = "Foreign Aid Percentage", y = "Count") + 
  theme(panel.grid.major = element_blank(),     # getting rid of grid lines and background color
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        axis.line = element_line()) 
## Warning: Removed 9 rows containing non-finite outside the scale range
## (`stat_bin()`).

ggplot(OptionalData,                     # data argument
       aes(x = defense.burden)) +    # mapping and aesthetics arguments
  geom_histogram(fill = "red",           # geom argument, specifies color
                 bins = 60) +            # specifies bins
  labs(x = "Defense Budget",     # changing labels
       y = "Count") + 
  theme(panel.grid.major = element_blank(),   # getting rid of grid lines and background color
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line())
## Warning: Removed 8 rows containing non-finite outside the scale range
## (`stat_bin()`).

Finally, all of the graphs in this section should look “clean” and professional. Specifically, they should 1) not have extraneous lines, colors, and/or components in the background and 2) have descriptive axis labels, not the names of the variables. Details about these points are provided in previous lab assignments.

Visualizing Bivariate Relationships

You need to create a set of graphs that allow you to visualize bivariate relationships.

First, create a set of graphs that visualize 1) the average ideology of Republican members of the House over the years in the data set; 2) the average ideology of Democratic members of the House over the years in the data set; and 3) how much of its available economic resources the United States spends on foreign aid over annual economic growth.

ggplot(OptionalData, aes(x=year, y=rep.ideology)) + geom_point() + labs(title = "Republican Ideology Over the Years",x = "Year",y = "Republican Ideology")

ggplot(OptionalData, aes(x=year, y=dem.ideology)) + geom_point() + labs(title = "Democrat Ideology Over the Years",x = "Year",y = "Democrat Ideology")

ggplot(OptionalData, aes(x=econ.percent.growth, y=foreign.aid.percent)) + geom_point() + labs(title = "Foreign Aid Spending Percentage Over Annual Economic Growth Percentage",x = "Economic Growth",y = "Foreign Aid Spending")
## Warning: Removed 9 rows containing missing values or values outside the scale range
## (`geom_point()`).

Second, create a set of graphs that visualize 1) how much of its available economic resources the United States spends on foreign aid depending on whether or not the United States has a divided government; 2) annual economic growth depending on whether the president is a member of the Democratic Party or the Republican Party; and 3) how much of its available economic resources the United States spends on foreign aid depending on whether or not the United States is involved in an interstate war.

OptionalData <- OptionalData %>%
  mutate(DividedGov = ifelse(divided == 1, 1, 0))
ggplot(OptionalData,aes(x=as.factor(DividedGov),y=foreign.aid.percent)) + geom_point(position = "jitter")
## Warning: Removed 9 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggplot(OptionalData,aes(x=as.factor(president.party),y=econ.percent.growth)) + geom_point(position = "jitter")
## Warning: Removed 9 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggplot(OptionalData,aes(x=as.factor(war),y=foreign.aid.percent)) + geom_point(position = "jitter")
## Warning: Removed 9 rows containing missing values or values outside the scale range
## (`geom_point()`).

Third, create a contingency table between variables that identify whether or not the United States is involved in an interstate war and whether or not the United States has divided government.

OptionalData %>% mutate(Divided.label=NA) %>% mutate(Divided.label=replace(Divided.label,divided==1,"Divided")) %>% mutate(Divided.label=replace(Divided.label,divided==0,"Unified")) ->OptionalData
OptionalData %>% tabyl(war.label, Divided.label) %>% adorn_title(row_name = "War Status",col_name = "Divided Government")
##             Divided Government        
##  War Status            Divided Unified
##      no war                 35      21
##         war                 13      11

Statistically Analyzing Bivariate Relationships

You need to statistically analyze a set of bivariate relationships in this section.

First, identify the bivariate correlation between variables that identify 1) how much of its available economic resources the United States spends on its military and how much of its available economic resources the United States spends on foreign aid; 2) how much of its available economic resources the United States spends on its military and how dissimilar the United States’ foreign policy preferences are from its neighbors and other major powers; and 3) polarization and economic growth.

OptionalData %>% select(defense.burden,foreign.aid.percent)%>% correlate()
## Correlation computed with
## • Method: 'pearson'
## • Missing treated using: 'pairwise.complete.obs'
## # A tibble: 2 × 3
##   term                defense.burden foreign.aid.percent
##   <chr>                        <dbl>               <dbl>
## 1 defense.burden              NA                  -0.269
## 2 foreign.aid.percent         -0.269              NA
OptionalData %>% select(defense.burden,dissimilar.foreign.policy)%>% correlate()
## Correlation computed with
## • Method: 'pearson'
## • Missing treated using: 'pairwise.complete.obs'
## # A tibble: 2 × 3
##   term                      defense.burden dissimilar.foreign.policy
##   <chr>                              <dbl>                     <dbl>
## 1 defense.burden                    NA                        -0.558
## 2 dissimilar.foreign.policy         -0.558                    NA
OptionalData %>% select(polarization,econ.percent.growth)%>% correlate()
## Correlation computed with
## • Method: 'pearson'
## • Missing treated using: 'pairwise.complete.obs'
## # A tibble: 2 × 3
##   term                polarization econ.percent.growth
##   <chr>                      <dbl>               <dbl>
## 1 polarization              NA                  -0.151
## 2 econ.percent.growth       -0.151              NA

Second, identify whether we observe systematically different patterns of 1) economic growth depending on whether the president is a democrat or a republican; 2) how dissimilar the United States’ foreign policy preferences are from its neighbors and other major powers depending on whether the president is a democrat or a republican; and 3) how much of its available economic resources the United States spends on its military depending on whether polarization is greater than average in the United States.

t.test(econ.percent.growth ~ president.party, data = OptionalData)
## 
##  Welch Two Sample t-test
## 
## data:  econ.percent.growth by president.party
## t = 0.27867, df = 65.061, p-value = 0.7814
## alternative hypothesis: true difference in means between group democrat and group republican is not equal to 0
## 95 percent confidence interval:
##  -0.9259384  1.2262493
## sample estimates:
##   mean in group democrat mean in group republican 
##                 2.943111                 2.792955
t.test(dissimilar.foreign.policy ~ president.party, data = OptionalData)
## 
##  Welch Two Sample t-test
## 
## data:  dissimilar.foreign.policy by president.party
## t = -0.75003, df = 65.278, p-value = 0.4559
## alternative hypothesis: true difference in means between group democrat and group republican is not equal to 0
## 95 percent confidence interval:
##  -0.07305188  0.03316048
## sample estimates:
##   mean in group democrat mean in group republican 
##                0.4871909                0.5071366
OptionalData %>%
  mutate(polarization = ifelse(polarization > 0.67103, "Above.Mean", "Below.Mean")) %>%
  mutate(polarization = factor(polarization, levels = c("Below.Mean", "Above.Mean"))) -> OptionalData
t.test(defense.burden ~ polarization, data = OptionalData)
## 
##  Welch Two Sample t-test
## 
## data:  defense.burden by polarization
## t = -6.7849, df = 48.578, p-value = 1.487e-08
## alternative hypothesis: true difference in means between group Below.Mean and group Above.Mean is not equal to 0
## 95 percent confidence interval:
##  -15.000907  -8.144169
## sample estimates:
## mean in group Below.Mean mean in group Above.Mean 
##                 17.63513                 29.20767

Third, identify whether the following things are related to one another: 1) whether the United States is involved in a war and whether the president is a democrat or a republican and 2) whether the United States has divided government and whether the United States is involved in an interstate war.

chisq.test(OptionalData$war.label,OptionalData$president.party)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  OptionalData$war.label and OptionalData$president.party
## X-squared = 0.059524, df = 1, p-value = 0.8073
chisq.test(OptionalData$Divided.label, OptionalData$war.label)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  OptionalData$Divided.label and OptionalData$war.label
## X-squared = 0.20089, df = 1, p-value = 0.654

Publish Document

Click the “Knit” button to publish your work as an html document. This document or file will appear in the folder specified by your working directory. You will need to upload both this RMarkdown file and the html file it produces to AsU Learn to get all of the points associated with this lab.