library(tidyverse)
library(here)
library(janitor)
library(broom)
library(kableExtra)
Load packages
To access data, html and Rmd/qmd files:
Introduction:
This task analyzes the association between respondents’ political affiliation (coined “conservative” or “liberal” by self-identification) and if they consider themself an environmentalist. The variables in the dataset (conservation_survey.csv) I have used include:
- env_id: response to survey statement “I consider myself an environmentalist,” with outcomes 1 = Yes, 2 = No, 3 = Unsure
- pol_or: response to survey question “How do you self-identify politically?” Response values 1 - 3 are “conservative” (strongly to slightly), and values 5 - 7 are “liberal” (slightly to strongly).
## Read in survey data
<- read_csv(here("posts", "2021-02-23-chisquare-politcal", "conservation_survey.csv")) survey
Rows: 1331 Columns: 95
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (1): DBCODE
dbl (94): ENTITY, QUALTRICSID, TIME, GEND, AGE, AGE_RANGE, INCO, EDU, RACE, ...
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## only keep variables: 'env_id' and 'pol_or'
<- survey %>%
survey_clean clean_names() %>%
::select(env_id, pol_or) %>%
dplyrmutate(party = case_when(
between(pol_or, 1,3) ~ "conservative",
between(pol_or, 5,7) ~ "liberal",
TRUE ~ NA_character_))
<- survey_clean %>%
survey_environmentalist mutate(environmentalist = case_when(
== 1 ~ "yes",
env_id == 2 ~ "no",
env_id == 3 ~ "unsure")) env_id
## Finalized table that shows counts and proportions of "liberal" and "conservative" survey respondents who responded with "yes", "no" and "unsure" to the statement: "I consider myself an environmentalist"
<- survey_environmentalist %>%
survey_counts ::tabyl(party, environmentalist)
janitor
<- survey_counts %>%
survey_proportions adorn_percentages() %>%
::adorn_pct_formatting(digits = 2) %>%
janitoradorn_ns() %>%
drop_na()
<- survey_proportions %>%
survey_ct column_to_rownames(var = "party")
%>%
survey_ct kable(col.names = c("No",
"Unsure",
"Yes"),
caption = "**Table 1**: Association between respondents’ political affiliation (“conservative” or “liberal” by self-identification) vs if they consider themself an environmentalist") %>%
kable_styling(full_width = FALSE)
No | Unsure | Yes | |
---|---|---|---|
conservative | 50.24% (318) | 16.11% (102) | 33.65% (213) |
liberal | 24.47% (128) | 21.80% (114) | 53.73% (281) |
## Chi-square test
<- survey_counts %>%
survey_ct drop_na() %>%
column_to_rownames(var = "party")
<- chisq.test(survey_ct)
survey_x2 survey_x2
Pearson's Chi-squared test
data: survey_ct
X-squared = 81.237, df = 2, p-value < 2.2e-16
<- tidy(survey_x2) survey_tidy
Chi-square results and summary
A chi-square test for independence compares two variables in a contingency table to see if they are related. In a more general sense, it tests to see whether distributions of categorical variables differ from each another. For this analysis, there is a significant association (i.e. non-independence) between political affiliation and and if a person considers themself an environmentalist ($\chi$2(2) = 81.24, p-value = 2.29e-18).
Citations:
Andrews Forest LTER Site and C. Batavia. 2019. Influences on charitable giving for conservation: Online survey data of 1,331 respondents across the US, August 2017 ver 3. Environmental Data Initiative. https://doi.org/10.6073/pasta/926e6270e324a1322a900da14d38b96c
Metadata, including survey questions and possible responses, are here: https://portal.edirepository.org/nis/metadataviewer?packageid=knb-lter-and.5444.3