## NORMALNI ROZDELENI
# 5.1
'TO-DO' # nacteni dat a odstraneni NA hodnot

n <- 'TO-DO' # pocet objektu 
mu <- 'TO-DO' # stredni hodnota (prumer)
sigma <- 'TO-DO' # smerodatna odchylka

r <- 'TO-DO' # pocet tridicich intervalu podle Sturgesova pravidla
rozsah <- 'TO-DO'
# rucni zaokrouhleni na nejbizsi vyssi cele cislo delitelne r
# a prepocet hranic intervalu
b <- seq(575, 4975, by = 400)


par(mar = c(4, 5, 1, 1)) # prostor okolo ramecku: c(dole, vlevo, nahore, vpravo)
'TO-DO'('TO-DO', breaks = 'TO-DO', prob = T, density = 30, col = 'TO-DO',
        xlab = '', ylab = '', main = '', las = 1) # vykresleni histogramu
box(bty = 'o')  # ramecek okolo grafu
mtext('porodni hmotnost (v g)', side = 1, line = 2)
mtext('f(x)', side = 2, line = 4)
lines(density(wei), col = 'blue', lwd = 2)  # jadrovy odhad hustoty z dat
xfit <- seq(min(wei) - 100, max(wei) + 100, length = 300)  # x-ove hodnoty
yfit <- dnorm(xfit, mu, sigma)  # hustota N(mu, sigma^2) s odhadnutymi parametry
'TO-DO' # pridani krivky teoreticke hustoty do grafu

## zmena znaceni osy x tak, aby hodnoty odpovidaly stredum tridicich intervalu:
centr <- seq(775, 4775, by = 400)
hist('TO-DO', breaks = 'TO-DO', prob = T, density = 30, 
     col = 'blue', xlab = '', ylab = '', main = '', las = 1, axes = F)
box(bty = 'o') 
axis(1, centr)
axis(2, las = 1)


## pridani legendy:
legend("topleft" , bty = "n" , lty = c (2,1) , col = c("red", "blue"),
       legend = c("hustota", "jadrovy odhad hustoty"), lwd = 2, cex = 0.7)

# 5.2
mu <- 'TO-DO'
sigma <- 'TO-DO'

# a
'TO-DO'

# b
'TO-DO'

# c
'TO-DO'

# d
'TO-DO' 

# 5.3
n <- 'TO-DO'
mu <- 'TO-DO'
sigma <- 'TO-DO'
sigma5 <- 'TO-DO'

# a
'TO-DO'

# b
'TO-DO'
# c
'TO-DO'

# d
'TO-DO'

# 5.4
# hustota
x  <- seq(from = mu - 2200, to = mu + 2200, length = 500)
fx <- 'TO-DO'
fx5 <- 'TO-DO'
par(mar = c(4, 5, 1, 1))
plot('TO-DO', 'TO-DO', type = 'l', lwd = 2, ylim = c(0, max(fx5)),
     col = 'TO-DO', xlab = '', ylab = '', main = '', las = 1)
'TO-DO' # popis os x a y
'TO-DO' # vykresleni hustoty prumeru 5 nov.
legend('topright', col = c('blue', 'red'), lwd = c(2, 2),
       legend = c('jeden nov.', 'prumer peti nov.'), bty = 'n')
# pri potrebe zmenseni pisma legendy pouzijeme argument cex


# distribucni funkce
Fx <- 'TO-DO'
Fx5 <- 'TO-DO'

'TO-DO' # graf distribucnich fci vcetne legendy



## DVOUROZMERNE NORMALNI ROZDELENI
# 5.5
'TO-DO' # nacteni dat, odstraneni NA hodnot
'TO-DO' # vyber muzu

'TO-DO' # bodovy diagram delky leve a prave klicni kosti


# odhady
mL <- 'TO-DO' # prumer pro levou k.k.
mR <- 'TO-DO' # prumer pro pravou k.k.
sL <- 'TO-DO' # sm. odchylka pro levou k.k.
sR <- 'TO-DO' # sm. odchylka pro pravou k.k.
rLR <- 'TO-DO' # korelacni koeficient

tab <- data.frame(mean = c(mL, mR), sd = c(sL, sR), 
                  rho = rep(rLR, 2), row.names = c("leva strana", "prava strana"))
'TO-DO' # zaokrouhleni tabulky na 4 desetinna mista

# 5.6
Mu <- c(mL, mR)
Sig <- matrix(c(sL ^ 2, rLR * sL * sR, rLR * sL * sR, sR ^ 2),
              ncol = 2, nrow = 2)

n <- 50
x <- seq(from = min('TO-DO'), to = max('TO-DO'), length = n)
y <- seq(from = min('TO-DO'), to = max('TO-DO'), length = n)
M <- matrix(NA, n, n)
for(i in 1:length(x)){
  for(j in 1:length(y)){
    M[i,j] <- mvtnorm::dmvnorm(c(x[i], y[j]), mean = Mu, sigma = Sig) # hustota N_2(Mu, Sigma)
  }
}

# a (teoreticka hustota)
par(mar = c(4, 5, 1, 1))
'TO-DO' # vykresleni bodoveho (teckoveho) grafu
contour(x, y, M, add = T, asp = T, drawlabels = F, 
        levels = seq(0, max(M), length = 7)) 
      # arg. length v levels nastavuje pocet kontur

# b (jadrovy odhad hustoty)
Z <- MASS::kde2d(length.ML, length.MR, n = n, lim = c('TO-DO'))
'TO-DO' # vykresleni bodoveho grafu
contour(Z$x, Z$y, Z$z, add = T, asp = T, drawlabels = F,
        levels = seq(0, max(Z$z), length = 7))

# c (3D teoreticka hustota)
# install.packages("GA")
library(GA)
par(mar = c(0, 4, 1, 4))
persp3D(x, y, M, phi = 30, theta = 5, ticktype = 'simple',
        xlab = 'delka leve klicni kosti', 
        ylab = 'delka prave klicni kosti',
        zlab = 'f(x, y)', col.palette = terrain.colors, border = 'black')

# d (3D jadrovy odhad hustoty)
persp3D(Z$x, Z$y, Z$z, phi = 30, theta = 5, ticktype = 'simple',
        xlab = 'delka leve klicni kosti', 
        ylab = 'delka prave klicni kosti',
        zlab = 'f(x, y)', col.palette = terrain.colors, border = 'black')

