\documentclass{article}
\usepackage[czech]{babel} %[english]
\usepackage[cp1250]{inputenc} % pro iso8859-2
%\usepackage[utf8]{inputenc}   % pro unicode UTF-8
\usepackage{geometry}
\geometry{verbose, tmargin=2cm, bmargin=3cm, lmargin=2cm, rmargin=2cm}

\usepackage{multirow}
\usepackage{animate}
\usepackage{hyperref}
\usepackage{float}
\usepackage{subfig}
%\usepackage[T1]{fontenc}
%\usepackage{multicol}
%\usepackage{amsthm}
%\usepackage{amsmath}
%\usepackage{amssymb}
%\usepackage{mathtools}
%\usepackage{graphicx}
%\usepackage{comment}
%\usepackage{fancyhdr}
%\usepackage{url}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
<<setup, include=F>>=
# set global chunk options
#options(formatR.arrow=TRUE, width=90)

opts_chunk$set(fig.path='figure-', fig.align='center',  size='small', background='white', comment='', fig.show='hold')
library(knitr)
library(xtable)
library(car)
library(dplyr)
@

\section{Exploratory Data Analysis}
Vice informaci je k dispozici na webove adrese \href{https://yihui.name/knitr/options/}{https://yihui.name/knitr/options/}.

\subsection{Nacteni dat}
<<echo=T>>=
#setwd('/home/pgs/xbendovav/cox')
setwd('C:/Disk D/ND-Skola/02-Vyuka/06-Statistická inference II 2017/17-05-16')
data            <- read.delim('15-anova-means-skull.txt', stringsAsFactors=F)
data            <- na.omit(data)
data$upface_cat <- car::recode(data$upface.H, " lo:70=1; 70.001:hi=2")

head(data)

dim(data)
@


\subsection{Tabulky}
<<echo=F, results='asis'>>=
upface <- data$upface.H
sex    <- data$sex
pop    <- data$pop

mal.m <- upface[pop=='mal' & sex=='m']
per.m <- upface[pop=='per' & sex=='m']

tab <- data.frame(m=c(mean(mal.m), mean(per.m)),
                  s=c(sd(mal.m)  , sd(per.m)  ),   
                  med=c(quantile(mal.m,0.50), quantile(per.m,0.50)),
                  q1= c(quantile(mal.m,0.25), quantile(per.m,0.25)),
                  q3= c(quantile(mal.m,0.75), quantile(per.m,0.75)),
                  row.names=c('malajska', 'peruanska'))

tab <- round(tab, 4)

print(xtable(tab, align='l||rrrrr', digits=rep(4,6), 
               caption=paste('Tabulka základních charakteristik'), label='Tab_zakl_char'),  hline.after = c( 0,0),
      include.rownames = T,  include.colnames=T) # add.to.row=addtorow, 
@
\noindent
Z tabulky zakladnich charakteristik \ref{Tab_zakl_char} vidime, ze vyska horni casti tvare malajske populace je mirne nizsi nez vyska horni casti tvare peruanske populace.

<<echo=F, warning=F,  include=T, results='asis'>>=
tab <- round(tab, 4)

addtorow <- list()
addtorow$pos <- list(0)
addtorow$command <- paste0(' & $\\bar{x}$ & $\\mathrm{s}$ & $x_{0.5}$ & $x_{0.25}$ & $x_{0.75}$ \\\\')

print(xtable(tab, align='l||rrrrr', digits=rep(4,6), caption=paste('Tabulka základních charakteristik'), 
             label='Tab_char'),  
      hline.after = c( 0,0), include.rownames = T,  include.colnames=F, add.to.row=addtorow) 
@
\noindent
Z tabulky \ref{Tab_char} vidime, ze smerodatna odchylka vysky horni casti tvare malajske populace je mirne vyssi nez smerodatna odchylka vysky horni casti tvare peruanske populace.

<<echo=F, warning=F,  include=T, results='asis'>>=
tab <- table(data[data$pop %in% c('mal', 'per'),]$pop, data[data$pop %in% c('mal', 'per'),]$upface_cat)
tab <- cbind(tab, suma=apply(tab, 1, sum))
tab <- rbind(tab, suma=apply(tab, 2, sum))
row.names(tab) <- c('malajska', 'peruanska', '$\\sum$')

  
addtorow <- list()
addtorow$pos <- list(0)

addtorow$command <- paste0('\\multirow{2}*{populace} & \\multicolumn{2}{c|}{kategorie} & \\\\ & $\\leq 70$ & $>70$ & $\\sum$ \\\\' )
print(xtable(tab, align='l|rr|r', digits=rep(0,4), 
               caption=paste('Kontingencni tabulka absolutnich cetnosti'), label='Tab_charakteristik'),
        sanitize.text.function = function(x) x, hline.after = c( 0,  2),include.rownames = T, add.to.row=addtorow,
        include.colnames=F)
@

\newpage
\subsection{Grafy, obrázky}

<<echo=F,  fig.height=5, fig.width=5, out.width='.44\\linewidth', fig.cap="\\label{Malajsie} Vyska horni casti tvare -- malajska populace", fig.pos='h'>>=
hist(mal.m, prob=T, col='darkolivegreen1', main='Malajska populace', xlab='Vyska horni casti tvare')
boxplot(mal.m, col='darkolivegreen1', main='Peruanska populace', ylab='vyska horni casti tvare')
@

<<echo=F,  fig.height=5, fig.width=10, out.width='.88\\linewidth', fig.cap="\\label{Peruan} Vyska horni casti tvare -- peruanska populace", fig.pos='h'>>=
par(mfrow=c(1,2))
hist(per.m, prob=T, col='darkolivegreen1', main='Peruanska populace', xlab='Vyska horni casti tvare')
boxplot(per.m, col='darkolivegreen1', main='Peruanska populace', ylab='vyska horni casti tvare')
@
Z obrazku \ref{Malajsie} a \ref{Peruan} je videt, ze rozdeleni horni vysky tvare malajske populace je blize normalnimu rozdeleni nez rozdeleni horni vysky tvare peruanske populace.

\clearpage
\newpage
\subsection{Animace}
<<echo=T, fig.width=5, fig.height=5, out.width='.6\\linewidth', fig.show='animate', fig.path='figure/figure-', fig.cap="\\label{animace} Monotonni pomer verohodnosti", fig.pos='h'>>=
like <- function(x,N,p){
  like <- p^x*(1-p)^N
  return(like)
}

monotonnost <- function(N, p1, p2){
x <- seq(0:N)
L <- like(x, N, p2)/like(x, N, p1)

plot(x, L, type='l', ylab='', main='', col='red', lwd=2) 
mtext(expression(L(p[1],x)/L(p[2],x)), side=2, line=2.5)
mtext('Monotonni pomer verohodnosti', side=3, line=1.5, font = 2)
mtext(paste('N=', N, ';  p1=', p1, ';  p2=', p2, sep=''), line=0.4, cex=0.8)
}

p2 <- 0.5
p1 <- seq(p2-0.01, 0.01, by=-0.01)

for (j in 1:length(p1)) {
   monotonnost(20, p1[j],p2)
}
@

Na obrazku \ref{animace} je zachycena monotonnost pomeru verohodnosti pro $X\sim \mathrm{Bin}(N, p)$.

\clearpage
\newpage
\subsection{Mnohonasobne generovani tabulek:}
<<echo=F, warning=F, include=T, results='asis', tab.pos="h">>=
loop.tab <- function(pop1, pop2, lab1, lab2){
tab <- table(data[data$pop %in% c(pop1, pop2),]$pop, data[data$pop %in% c(pop1, pop2),]$upface_cat)
tab <- cbind(tab, suma=apply(tab, 1, sum))
tab <- rbind(tab, suma=apply(tab, 2, sum))
row.names(tab) <- c(lab1, lab2, '$\\sum$')

  
addtorow <- list()
addtorow$pos <- list(0)
addtorow$command <- paste0('\\multirow{2}*{populace} & \\multicolumn{2}{c|}{kategorie} & \\\\ & $\\leq 70$ & $>70$ & $\\sum$ \\\\' )
print(xtable(tab, align='l|rr|r', digits=rep(0,4), 
               caption=paste('Kontingencni tabulka absolutnich cetnosti -- ', lab1, 'a', lab2, 'populace'), label='Tab_charakteristik'),
        sanitize.text.function = function(x) x, hline.after = c( 0,  2),include.rownames = T, add.to.row=addtorow,
        include.colnames=F, table.placement="H")
}

pop1 <- c('mal', 'per')
nam1 <- c('malajska', 'peruanska')
pop2 <- c('nem', 'ban', 'cin')
nam2 <- c('nemecka', 'cinska', 'bantuska')

for(i in 1:2){
  for(j in 1:3){
loop.tab(pop1[i], pop2[j], nam1[i], nam2[j])
  }
}
@

\cleardoublepage
\newpage
\subsection{Mnohonasobne generovani grafu:}
<<echo=F, include=T, result=text, fig.width=10, fig.height=5, out.width='.8\\linewidth', fig.pos='h',  fig.show='asis'>>=
loop.plot <- function(pop, lab){
par(mfrow=c(1,2))
hist(data$upface.H[data$pop==pop], prob=T, col='darkolivegreen1', main=paste(lab,'populace'), 
     xlab=paste('vyska horni casti tvare'), ylab='hustota')
boxplot(data$upface.H[data$pop==pop], col='darkolivegreen1', main=paste(lab,'populace'),
        ylab=paste('vyska horni casti tvare'))}

pop <- c('mal', 'per', 'nem', 'ban', 'cin')
nam <- c('malajska', 'peruanska', 'nemecka', 'cinska', 'bantuska')
for(i in 1:5){
loop.plot(pop[i],nam[i])
  }

@

\end{document}