library(haven)
lec<-read.csv("LEC.Sex.csv")
 Gender <- factor(lec$chsex_d1, levels = 1:2, labels = c("Male", "Female")) #Turning gender into a factor
summary(Gender) #Getting summary of gender
##   Male Female 
##    111    109
 Ethnicity <- factor(lec$cethn_d1, levels = 1:6, labels = c("White", "Black", "Hispanic", "Asian-Oriental", "Mixed", "Other")) #Turning ethnicity into a factor
 summary(Ethnicity) #Getting summary of ethnicity 
##          White          Black       Hispanic Asian-Oriental          Mixed 
##            154             35              4              6             21 
##          Other 
##              0
Income <- factor(lec$inc_d1, levels = 1:6, labels = c("Less than $20k", "$20k-40k", "$41k-$60k", "$61k-$80k", "$81k-$100k", "Over $100k")) #Turning income into a factor
summary(Income) #Getting summary of income 
## Less than $20k       $20k-40k      $41k-$60k      $61k-$80k     $81k-$100k 
##             12             25             40             41             35 
##     Over $100k           NA's 
##             66              1
mean(lec$cage_d1) #Calculating adolescents' mean age
## [1] 13.67034
lecchi <- subset(lec,select=c(chsex_d1,lc31e_c1)) #Designating columns to run chi-square test on
table(lecchi) #Getting table of adolescents' sex and arguments with parents
##         lc31e_c1
## chsex_d1  0  1
##        1 91 18
##        2 82 25
library(naniar)
vis_miss(lecchi) #Looking at missing data
## Warning: `gather_()` was deprecated in tidyr 1.2.0.
## Please use `gather()` instead.

lecchi <- na.omit(lecchi) #Excluding missing data 
lecchi$chsex_d1 <- as.factor(lecchi$chsex_d1)
levels(lecchi$chsex_d1) <- list('Male'="1",'Female'="2") #Relabel adolescents' sex to ensure it is treated as a factor, not numerical

lecchi$lc31e_c1 <- as.factor(lecchi$lc31e_c1)
levels(lecchi$lc31e_c1) <- list('Did not experience life event'="0",'Did experience life event'="1") #Relabel adolescents' arguments with parents to ensure it is treated as a factor, not numerical
table(lecchi) #Getting table of adolescents' sex and arguments with parents without missing data 
##         lc31e_c1
## chsex_d1 Did not experience life event Did experience life event
##   Male                              91                        18
##   Female                            82                        25
chiSquareTable <- table(lecchi) #Saving chi-square table as an object
chiSq <- chisq.test(chiSquareTable) #Running chi-square test
chiSq$statistic #Getting chi-square statistic
## X-squared 
##  1.188735
chiSq$p.value #Getting chi-square p-value
## [1] 0.2755849
chiSq$residuals #Getting chi-square residuals 
##         lc31e_c1
## chsex_d1 Did not experience life event Did experience life event
##   Male                       0.3958983                -0.7940949
##   Female                    -0.3995811                 0.8014820
library(rcompanion)
cramerV(chiSquareTable) #Getting chi-square Cramer's V value 
## Cramer V 
##  0.08578
library(ggplot2)
ggplot(data = lecchi, aes(x = lc31e_c1)) + 
  geom_bar(aes(fill = chsex_d1),position="dodge") +
  xlab("Increased Arguing with Parents") +
  ylab("Frequency") +
  scale_fill_manual("Adolescent Sex", values = c(
    "Male" = "green3", "Female" = "red")) #Creating chi-square bar graph 

sessionInfo()
## R version 4.2.1 (2022-06-23)
## Platform: aarch64-apple-darwin20 (64-bit)
## Running under: macOS Monterey 12.6
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] ggplot2_3.3.6     rcompanion_2.4.18 naniar_0.6.1      haven_2.5.1      
## 
## loaded via a namespace (and not attached):
##  [1] httr_1.4.4         sass_0.4.2         tidyr_1.2.1        jsonlite_1.8.0    
##  [5] splines_4.2.1      bslib_0.4.0        assertthat_0.2.1   expm_0.999-6      
##  [9] gld_2.6.5          lmom_2.9           highr_0.9          stats4_4.2.1      
## [13] coin_1.4-2         cellranger_1.1.0   yaml_2.3.5         pillar_1.8.1      
## [17] lattice_0.20-45    glue_1.6.2         visdat_0.5.3       digest_0.6.29     
## [21] sandwich_3.0-2     colorspace_2.0-3   plyr_1.8.7         htmltools_0.5.3   
## [25] Matrix_1.4-1       pkgconfig_2.0.3    purrr_0.3.4        mvtnorm_1.1-3     
## [29] scales_1.2.1       rootSolve_1.8.2.3  tibble_3.1.8       proxy_0.4-27      
## [33] generics_0.1.3     farver_2.1.1       ellipsis_0.3.2     withr_2.5.0       
## [37] TH.data_1.1-1      cachem_1.0.6       cli_3.4.0          survival_3.3-1    
## [41] magrittr_2.0.3     readxl_1.4.1       evaluate_0.16      fansi_1.0.3       
## [45] MASS_7.3-57        forcats_0.5.2      class_7.3-20       tools_4.2.1       
## [49] data.table_1.14.2  hms_1.1.2          multcomp_1.4-20    lifecycle_1.0.2   
## [53] matrixStats_0.62.0 stringr_1.4.1      Exact_3.2          munsell_0.5.0     
## [57] compiler_4.2.1     jquerylib_0.1.4    e1071_1.7-11       multcompView_0.1-8
## [61] rlang_1.0.5        grid_4.2.1         rstudioapi_0.14    labeling_0.4.2    
## [65] rmarkdown_2.16     boot_1.3-28        DescTools_0.99.46  codetools_0.2-18  
## [69] gtable_0.3.1       DBI_1.1.3          R6_2.5.1           zoo_1.8-11        
## [73] knitr_1.40         dplyr_1.0.10       fastmap_1.1.0      utf8_1.2.2        
## [77] nortest_1.0-4      libcoin_1.0-9      modeltools_0.2-23  stringi_1.7.8     
## [81] parallel_4.2.1     Rcpp_1.0.9         vctrs_0.4.1        lmtest_0.9-40     
## [85] tidyselect_1.1.2   xfun_0.33
citation("haven")
## 
## To cite package 'haven' in publications use:
## 
##   Wickham H, Miller E, Smith D (2022). _haven: Import and Export
##   'SPSS', 'Stata' and 'SAS' Files_. R package version 2.5.1,
##   <https://CRAN.R-project.org/package=haven>.
## 
## A BibTeX entry for LaTeX users is
## 
##   @Manual{,
##     title = {haven: Import and Export 'SPSS', 'Stata' and 'SAS' Files},
##     author = {Hadley Wickham and Evan Miller and Danny Smith},
##     year = {2022},
##     note = {R package version 2.5.1},
##     url = {https://CRAN.R-project.org/package=haven},
##   }
citation("naniar")
## 
## To cite package 'naniar' in publications use:
## 
##   Tierney N, Cook D, McBain M, Fay C (2021). _naniar: Data Structures,
##   Summaries, and Visualisations for Missing Data_. R package version
##   0.6.1, <https://CRAN.R-project.org/package=naniar>.
## 
## A BibTeX entry for LaTeX users is
## 
##   @Manual{,
##     title = {naniar: Data Structures, Summaries, and Visualisations for Missing Data},
##     author = {Nicholas Tierney and Di Cook and Miles McBain and Colin Fay},
##     year = {2021},
##     note = {R package version 0.6.1},
##     url = {https://CRAN.R-project.org/package=naniar},
##   }
citation("ggplot2")
## 
## To cite ggplot2 in publications, please use:
## 
##   H. Wickham. ggplot2: Elegant Graphics for Data Analysis.
##   Springer-Verlag New York, 2016.
## 
## A BibTeX entry for LaTeX users is
## 
##   @Book{,
##     author = {Hadley Wickham},
##     title = {ggplot2: Elegant Graphics for Data Analysis},
##     publisher = {Springer-Verlag New York},
##     year = {2016},
##     isbn = {978-3-319-24277-4},
##     url = {https://ggplot2.tidyverse.org},
##   }
citation("rcompanion") #Getting citations 
## 
## To cite package 'rcompanion' in publications use:
## 
##   Mangiafico S (2022). _rcompanion: Functions to Support Extension
##   Education Program Evaluation_. R package version 2.4.18,
##   <https://CRAN.R-project.org/package=rcompanion>.
## 
## A BibTeX entry for LaTeX users is
## 
##   @Manual{,
##     title = {rcompanion: Functions to Support Extension Education Program Evaluation},
##     author = {Salvatore Mangiafico},
##     year = {2022},
##     note = {R package version 2.4.18},
##     url = {https://CRAN.R-project.org/package=rcompanion},
##   }