library(googlesheets4)
library(googledrive)
library(ggplot2)
library(DescTools)

Table Dog Death Cause

Datos en el G-sheets adjunto link

ss= "https://docs.google.com/spreadsheets/d/1A0Leo-pYgqgBuGgHLdpO1HW_uEgz1LjC8EVilgwrzGY/edit?usp=sharing"
hoja= "Hoja2"
rango = "A6:D12"
#
gs4_deauth()
dd <- read_sheet(ss,
                  sheet= hoja,
                  range= rango,
                  col_names= TRUE)
dd$total <- dd$animInter+dd$disease+dd$human
dd$Pr.disease <- dd$disease/dd$total
head(dd)
## # A tibble: 6 x 6
##   comm  animInter disease human total Pr.disease
##   <chr>     <dbl>   <dbl> <dbl> <dbl>      <dbl>
## 1 CA            7       9     4    20      0.45 
## 2 CAP           7       9    28    44      0.205
## 3 ePo           2      11    11    24      0.458
## 4 PC            2      10     4    16      0.625
## 5 UA           15      18    17    50      0.36 
## 6 Zo            5      19     5    29      0.655
p <- ggplot(aes(x=comm, y= Pr.disease, fill=comm), data=dd)
p + geom_col()

dd$other <- dd$animInter + dd$human 
head(dd)
## # A tibble: 6 x 7
##   comm  animInter disease human total Pr.disease other
##   <chr>     <dbl>   <dbl> <dbl> <dbl>      <dbl> <dbl>
## 1 CA            7       9     4    20      0.45     11
## 2 CAP           7       9    28    44      0.205    35
## 3 ePo           2      11    11    24      0.458    13
## 4 PC            2      10     4    16      0.625     6
## 5 UA           15      18    17    50      0.36     32
## 6 Zo            5      19     5    29      0.655    10
obs <- cbind(dd$disease, dd$other)
chi2 <- chisq.test(obs)
chi2
## 
##  Pearson's Chi-squared test
## 
## data:  obs
## X-squared = 18.73, df = 5, p-value = 0.002158
dd[,c(1,3,7)]
## # A tibble: 6 x 3
##   comm  disease other
##   <chr>   <dbl> <dbl>
## 1 CA          9    11
## 2 CAP         9    35
## 3 ePo        11    13
## 4 PC         10     6
## 5 UA         18    32
## 6 Zo         19    10
chi2$expected
##           [,1]      [,2]
## [1,]  8.306011 11.693989
## [2,] 18.273224 25.726776
## [3,]  9.967213 14.032787
## [4,]  6.644809  9.355191
## [5,] 20.765027 29.234973
## [6,] 12.043716 16.956284
p <- ggplot(aes(x=comm, y= Pr.disease, fill=comm), data=dd)
p + geom_col() + 
  geom_hline(yintercept = 0.42, lty="dashed", lwd=1) +
  geom_text(x=1.5, y=0.5, label= "Pr.general", col="dark green")

chi2$stdres
##            [,1]       [,2]
## [1,]  0.3336735 -0.3336735
## [2,] -3.2551797  3.2551797
## [3,]  0.4589702 -0.4589702
## [4,]  1.7818741 -1.7818741
## [5,] -0.9308218  0.9308218
## [6,]  2.8575636 -2.8575636