### 7 UVOD DO TESTOVANI HYPOTEZ
# 7.1
data <- 'TO-DO'
'TO-DO' # prohlednuti struktury dat
skull.HM <- 'TO-DO' # vyber muzi a promenna skull.H
skull.HM <- 'TO-DO' # odstraneni NA hodnot
n <- 'TO-DO' # pocet pozorovani

# graficke overeni normality
# histogram
r <- 3.3*log10(n) + 1 #Sturges. pravidlo
'TO-DO' # uprava rozsahu podle poctu intervalu
b <- 'TO-DO' # sekvence pro arg. breaks v histogramu

par(mar = c(4, 4, 1, 1))
hist('TO-DO', breaks = b, prob = 'TO-DO', col = 'TO-DO', border = 'TO-DO',
     density = 30, main = '', axes = F, ylab = 'relativni cetnost', xlab = 'vyska lebky (v mm)', 
     ylim = c(0, 0.09))
box(bty = 'o')
axis(side = 1, at =  seq(120.5, 144.5, by = 3))
axis(side = 2, las = 1)
xfit <- 'TO-DO' # sekvence pro vykresleni hustoty
yfit <- 'TO-DO' # hustota norm. rozdeleni s parametry mean(skull.HM) a sd(skull.HM)
'TO-DO' # vykresleni krivky hustoty norm. rozd.

# q-q graf
qqnorm(skull.HM, pch = 21, bg = 'lightpink1', col = 'hotpink4', main = '',
       xlab = 'teoreticky kvantil', ylab = 'vyberovy kvantil', las = 1)
qqline(skull.HM, col = 'darkmagenta', lwd = 2)

# install.packages("nortest") - pokud nemate nainstalovano
nortest::lillie.test(skull.HM)$p.val # p-hodnota testu

# 7.2 
data <- 'TO-DO'
upface.HN <- 'TO-DO' # vyber nemecke populace promenne upface.H
upface.HN <- 'TO-DO' # odstraneni NA
n <- 'TO-DO'

# histogram
'TO-DO'


# q-q graf
qqnorm('TO-DO', pch = 21, bg = 'orange', col = 'darkorange3', main = '',
       xlab = 'teoreticky kvantil', ylab = 'vyberovy kvantil', las = 1)
qqline('TO-DO', col = 'darkorange4', lwd = 2)


# S-W test normality
shapiro.test(upface.HN)


# 7.3 (Test o dvourozmerne normalite)
'TO-DO' # nacteni dat
udaje <- 'TO-DO' # vyber populace per, promenne nose.B a intorb.B
udaje <- 'TO-DO' # odstraneni NA
n <- # pocet objektu

# install.packages("MVN")
MVN::mvn(udaje, mvnTest = 'mardia')$multivariateNormality


# jadrovy odhad
range(udaje$nose.B)
range(udaje$intorb.B)
# range potrebujeme pro nastaveni lims ve funkci kde2d
Z <- MASS::kde2d(udaje$nose.B, udaje$intorb.B, n = 40, 
                 lims = c(17, 28, 17, 30))
par(mar = c(0, 4, 0, 4))
GA::persp3D(Z$x, Z$y, Z$z, xlab = 'sirka nosu (v mm)', ticktype = 'simple',
            ylab = 'interorbitalni sirka (v mm)', zlab = "relativni cetnost", 
            col.palette = terrain.colors, border = 'black',
            theta = 5, phi = 30)

# elipsa spolehlivosti
par(mar = c(4, 4, 2, 1))
car::dataEllipse(udaje$nose.B, udaje$intorb.B, level = 0.95, 
                 xlim = c(18, 28), ylim = c(17, 29), xlab = '', 
                 ylab = 'interorbitalni sirka (v mm)', main = '', pch = 21,  
                 col = 'green4', bg = 'yellow', lwd = 2, las = 1)
mtext('sirka nosu (v mm)', side = 1, line = 2.1)


# 7.4 (Test o dvourozmerne normalite)
data <- 'TO-DO'
udaje <- 'TO-DO' # vyber zeny, prom. head.L a bizyg.W
udaje <- 'TO-DO' # odstraneni NA
n <- 'TO-DO'

MVN::mvn('TO-DO', mvnTest = 'TO-DO')$multivariateNormality

# jadrovy odhad
'TO-DO'

# elipsa spolehlivosti
'TO-DO'


# install.packages("sp")
library(sp) # funkce point.in.polygon()
# lezi bod uvnitr (TRUE; 1) nebo vne (FALSE; 0) elipsy?
rozhodnuti <- as.logical(point.in.polygon(udaje$head.L, udaje$bizyg.W, elipsa[,1], elipsa[,2])) 
table(rozhodnuti) # 10 vne, 90 uvnitr
10 / n # kolik procent lezi mimo elipsu?
