options(scipen=999)
options(digits=9)
cat("\014") # clear screen
rm(list=ls(all=TRUE))


alpha=0.05
o=c(180,510,190,120)
e=c(160,480,200,160)
k=4
m=0
chisquare_compute=sum((o-e)^2/e);chisquare_compute
v=k-m-1;v
one_tailed_sig=1-pchisq(chisquare_compute,v);one_tailed_sig
chisq_critical=qchisq(1-alpha,v);chisq_critical


o=c(180,510,190,120)
e=c(160,480,200,160)
chisq.test(o,p=e,rescale.p=TRUE)

o=c(180,510,190,120)
p=0.4
q=0.4
r=0.2
e=c(q^2,p^2+2*p*q,r^2+2*r*q,2*p*r)
chisq.test(o,p=e,rescale.p=TRUE)


alpha=0.05
o=c(180,510,190,120)
N=sum(o);N
p=0.4
q=0.4
r=0.2
Pe=c(q^2,p^2+2*p*q,r^2+2*r*q,2*p*r)
e=N*Pe
k=4
m=0
chisquare_compute=sum((o-e)^2/e);chisquare_compute
v=k-m-1;v
one_tailed_sig=1-pchisq(chisquare_compute,v);one_tailed_sig
chisq_critical=qchisq(1-alpha,v);chisq_critical
