
# com134e of august 2025
# Star Trek deaths: here also with Bayes,
# Jeffreys prior for the six cells 

# nils-emil:
# check Story \ref{story:StarTrek} vs others with contingency tables
#    make sture that we tell different things for different stories 
#    Story Psychiatric disorders and body size
#    Story clothes and intelligence 1911
#    Story Antwerpen 1860-1924 social mobility and Turgenyev
#    so perhaps Psychiatric disorders and body size leaves the ship 

# install.packages("MCMCpack") # just to have rdirichlet on board 
library(MCMCpack)

# Uniform              crew 
#          alive dead  total 
# Blue     129     7    136  science and medical 
# Gold      46     9     55  command and helm 
# Red      215    24    239  operations, engineering, security 

# total    390    40    430 

NN <- rbind(c(129,7), c(46,9), c(215,24))

#      alive dead 
# [1,]  129    7
# [2,]   46    9
# [3,]  215   24

rr <- nrow(NN) # 3 
cc <- ncol(NN) # 2 
df <- (rr-1)*(cc-1) 

nn <- sum(NN) #
ahat <- 0*(1:rr)
bhat <- 0*(1:cc)

for (i in 1:rr)
{ ahat[i] <- sum(NN[i, ])/nn }

for (j in 1:cc)
{ bhat[j] <- sum(NN[ ,j])/nn }

EE <- 0*NN
for (i in 1:rr)
{for (j in 1:cc)
{
EE[i,j] <- nn*ahat[i] * bhat[j] }}

pearson <- (NN-EE)/sqrt(EE)
KP <- sum(pearson^2) # 6.188

cbind(NN,round(EE,2),round(pearson,2))

pvalue <- 1 - pchisq(KP,df) # 0.045 

# examining p, q, r : 

n1 <- 136
n2 <-  55
n3 <- 239 

phat <- 7/n1
qhat <- 9/n2
rhat <- 24/n3

sdp <- sqrt(phat*(1-phat)/n1)
sdq <- sqrt(qhat*(1-qhat)/n2)
sdr <- sqrt(rhat*(1-rhat)/n3)

round(cbind(c(phat,qhat,rhat), c(sdp,sdq,sdr)),4)

# [1,] 0.0515 0.0189 p 
# [2,] 0.1636 0.0499 q 
# [3,] 0.1004 0.0194 r 


phihat <- 0*NN

for (i in 1:rr)
{for (j in 1:cc)
{
phihat[i,j] <- (NN[i,j]/nn) / (ahat[i]*bhat[j]) }}


## then Bayes, for several parameters
## \phi_{1,2} = p_{1,2} / a_1 b_2 

sim <- 10^6
# ppsim <- array(0, dim=c(sim,3,2))

ppsim <- rdirichlet(sim, NN+0.5)
# this is sim x 6 matrix, with p11 p21 p31 first then p12 p22 p23
# p11 p12
# p21 p22
# p31 p32 

pphat <- 0*(1:6)
for (kk in 1:6)
{ pphat[kk] <- mean(ppsim[ ,kk]) }

round(pphat,4)

ppMsim <- array(0,dim=c(sim,rr,cc)) # sim tables of size 3 x 2 

ppMsim[ ,1,1] <- ppsim[ ,1]
ppMsim[ ,1,2] <- ppsim[ ,4]

ppMsim[ ,2,1] <- ppsim[ ,2]
ppMsim[ ,2,2] <- ppsim[ ,5]

ppMsim[ ,3,1] <- ppsim[ ,3]
ppMsim[ ,3,2] <- ppsim[ ,6]

aMsim <- array(0,dim=c(sim,rr))
bMsim <- array(0,dim=c(sim,cc))

aMsim[ ,1] <- ppMsim[ ,1,1] + ppMsim[ ,1,2]
aMsim[ ,2] <- ppMsim[ ,2,1] + ppMsim[ ,2,2]
aMsim[ ,3] <- ppMsim[ ,3,1] + ppMsim[ ,3,2]

bMsim[ ,1] <- ppMsim[ ,1,1] + ppMsim[ ,2,1] + ppMsim[ ,3,1]
bMsim[ ,2] <- ppMsim[ ,1,2] + ppMsim[ ,2,2] + ppMsim[ ,3,2]

# a1sim <- ppsim[ ,1] + ppsim[ ,4]
# a2sim <- ppsim[ ,2] + ppsim[ ,5]
# a3sim <- ppsim[ ,3] + ppsim[ ,6]

# b1sim <- ppsim[ ,1] + ppsim[ ,2] + ppsim[ ,3]
# b2sim <- ppsim[ ,4] + ppsim[ ,5] + ppsim[ ,6]

# these match ahat and bhat : 
mean(aMsim[ ,1])
mean(aMsim[ ,2])
mean(aMsim[ ,3])

mean(bMsim[ ,1])
mean(bMsim[ ,2])

# then the interesting \phi_{i,j} = p_{i,j} / (a_i b_j):
# which of these are a bit away from 1? 

phi12sim <- ppMsim[ ,1,2] / (aMsim[ ,1]*bMsim[ ,2])
round( quantile(phi12sim,c(0.025,0.50,0.975)), 4)
# 0.2619 0.5578 0.9619 so significantly lower than 1 

phi22sim <- ppMsim[ ,2,2] / (aMsim[ ,2]*bMsim[ ,2])
round( quantile(phi22sim,c(0.025,0.50,0.975)), 4)
# 0.9428 1.7497 2.7534 almost significantly bigger than 1 

hist(phi12sim)
hist(phi22sim)

# the Bayesian posterior cumulatives; 

phival <- seq(0,3.5,by=0.01)
B12val <- 0*phival
B22val <- 0*phival

# i ask to count through a million numbers a few thousand times: 

for (j in 1:length(phival))
{
B12val[j] <- mean(1*(phi12sim <= phival[j]))
B22val[j] <- mean(1*(phi22sim <= phival[j]))
print(c(j,phival[j]))
} 

par(bty="l")
par(cex.lab=1.4142) 

matplot(phival,cbind(B12val,B22val),type="l",lwd=3,
   xlab="phi12 and phi22",ylab="cdf posterior")

# so phi12 < 1 and phi22 > 1 

## then \lambda = \sum_{i,j} (p_{i,j} - a_ib_j)^2 / (a_ib_j)

w11 <- (ppMsim[ ,1,1] - aMsim[ ,1]*bMsim[ ,1])^2 / (aMsim[ ,1]*bMsim[ ,1])
w12 <- (ppMsim[ ,1,2] - aMsim[ ,1]*bMsim[ ,2])^2 / (aMsim[ ,1]*bMsim[ ,2])

w21 <- (ppMsim[ ,2,1] - aMsim[ ,2]*bMsim[ ,1])^2 / (aMsim[ ,2]*bMsim[ ,1])
w22 <- (ppMsim[ ,2,2] - aMsim[ ,2]*bMsim[ ,2])^2 / (aMsim[ ,2]*bMsim[ ,2])

w31 <- (ppMsim[ ,3,1] - aMsim[ ,3]*bMsim[ ,1])^2 / (aMsim[ ,3]*bMsim[ ,1])
w32 <- (ppMsim[ ,3,2] - aMsim[ ,3]*bMsim[ ,2])^2 / (aMsim[ ,3]*bMsim[ ,2])

lambdasim <- w11+w12 + w21+w22 + w31+w32

hist(lambdasim)

plot(ecdf(lambdasim))

## let's see: \lambda = rs d^2, d^2 = (rs)^{-1} \lambda
#  i.e. about average of (p_{i,j} - a_ib_j)^2 / (a_ib_j)
#  so d = is about average of | p_{i,j} - a_ib_j | / (a_ib_j)^{1/2} 

dval <- seq(0,0.12,by=0.001) 
lambdaval <- rr*cc*dval^2 

Gnval <- 0*dval 
for (j in 1:length(dval)) 
{ Gnval[j] <- mean(1*(lambdasim <= lambdaval[j])) }

Cval <- 1 - pchisq(KP,df,nn*rr*cc*dval^2) 

par(bty="l")
par(cex.lab=1.4142) 

aux <- seq(0,1,by=0.10) 

## making startrek_figure11.pdf : 

par(bty="l")
par(cex.lab=1.4142) 

matplot(dval,cbind(Cval,Gnval),type="l",lwd=3,ylim=c(0,1),
   xlab="average deviation d from independence",
   ylab="CD and Bayesian posterior")
grid(lwd=3)

matlines(dval,0*dval,type="l",lty=3,lwd=3)
matlines(0*aux, aux*Cval[1],type="l",lty=1,lwd=3) 

KP - df # estimating \lambda = r c d^2
#  dhat = sqrt( (KP - df)/(rr*cc) )
