
R version 3.6.2 (2019-12-12) -- "Dark and Stormy Night"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(MASS)
> #Then use the chisq.test() function to carry out the test
> 
> # can more succinctly input frequencies instead of raw data
> 
> sex <- rep(1:2, c(293,182))
> region <- c(rep(1,199),rep(2,27),rep(3,67),rep(1,134),rep(2,25),rep(3,23))
> result <- chisq.test(table(sex,region))
> 
> # see where difference is
> result$expected
   region
sex        1        2        3
  1 205.4084 32.07579 55.51579
  2 127.5916 19.92421 34.48421
> result$observed
   region
sex   1   2   3
  1 199  27  67
  2 134  25  23
> z <- (result$observed-result$expected)/sqrt(result$expected)
> pout <- 2*pnorm(-abs(z),0,1)
> z
   region
sex          1          2          3
  1 -0.4471383 -0.8962206  1.5413203
  2  0.5673356  1.1371377 -1.9556494
> pout
   region
sex          1          2          3
  1 0.65477517 0.37013495 0.12323886
  2 0.57048620 0.25548075 0.05050647
# standardised residual additionally adjusts for sample size and divided O-E by sqrt(E(1-r)*(1-c))
> chisq.test(table(sex,region))$stdres
   region
sex         1         2         3
  1 -1.321161 -1.534273  2.765798
  2  1.321161  1.534273 -2.765798
> 
> # if any expected counts less than 5 user Fisher's test
> fisher.test(table(sex,region))

        Fisher's Exact Test for Count Data

data:  table(sex, region)
p-value = 0.0113
alternative hypothesis: two.sided

> 
> X <- cbind(c(19,2), c(10,9))
> mcnemar.test(X, correct=TRUE)

        McNemar's Chi-squared test with continuity correction

data:  X
McNemar's chi-squared = 4.0833, df = 1, p-value = 0.04331

> # check entered 2x2 table correctly
> X 
     [,1] [,2]
[1,]   19   10
[2,]    2    9
> #install.packages("fmsb")
> library(fmsb)
> Kappa.test(X,y=NULL, conf.level=0.95)
$Result

        Estimate Cohen's kappa statistics and test the null hypothesis that
        the extent of agreement is same as random (kappa=0)

data:  X
Z = 2.3881, p-value = 0.008467
95 percent confidence interval:
 0.09562582 0.67675270
sample estimates:
[1] 0.3861893


$Judgement
[1] "Fair agreement"

> 
> region2 <- c(rep(0,100),rep(1,193),rep(0,82),rep(1,100))
> glm(region2 ~ sex, family=binomial)

Call:  glm(formula = region2 ~ sex, family = binomial)

Coefficients:
(Intercept)          sex  
     1.1166      -0.4591  

Degrees of Freedom: 474 Total (i.e. Null);  473 Residual
Null Deviance:      632.3 
Residual Deviance: 626.7        AIC: 630.7
> exp(-0.459)
[1] 0.6319152
> 
table(region2,sex)
(100*100)/(82*193)
> 0.6319