######### 1 #########
# nacteme data
DATA <- read.delim("data_cv_3.txt", header = TRUE)
str(DATA)
attach(DATA)

# nacteme knihovny ktere budeme pouzivat
library(rgl)
library(car)

scatter3d(hmotnost ~ vyska + gonion, data=DATA, groups = sex, fit="smooth",
          axis.col = c(rep("white",3)),
          bg.col = "black",
          point.col = c("palegreen4", "royalblue4"), 
          surface.col = c("palegreen2","royalblue2"))

######### 2 ######### 
DATA3D <- read.delim("07_data3d.txt", header = TRUE)
attach(DATA3D)
str(DATA3D)
dim(DATA3D)

library(rgl)

# vytvorime pomocne objekty pro muze a zeny
DATA3D_f <- DATA3D[sex == "f",]
DATA3D_m <- DATA3D[sex == "m",]

# prumer pro zeny bodu POdx
prumF_POdx_x <- mean(DATA3D_f$x.POdx)
prumF_POdx_y <- mean(DATA3D_f$y.POdx)
prumF_POdx_z <- mean(DATA3D_f$z.POdx)

# prumer pro muze bodu POdx
prumM_POdx_x <- mean(DATA3D_m$x.POdx)
prumM_POdx_y <- mean(DATA3D_m$y.POdx)
prumM_POdx_z <- mean(DATA3D_m$z.POdx)

# prumer pro zeny bodu POsin
prumF_POsin_x <- mean(DATA3D_f$x.POsin)
prumF_POsin_y <- mean(DATA3D_f$y.POsin)
prumF_POsin_z <- mean(DATA3D_f$z.POsin)

# prumer pro muze bodu POsin
prumM_POsin_x <- mean(DATA3D_m$x.POsin)
prumM_POsin_y <- mean(DATA3D_m$y.POsin)
prumM_POsin_z <- mean(DATA3D_m$z.POsin)

rgl.open()
par3d(windowRect=c(20, 30, 800, 800))
rgl.viewpoint(theta = 20, phi = 45, zoom = 1) 
shapelist3d(icosahedron3d(col = "palevioletred4"), DATA3D_f$x.POdx,DATA3D_f$y.POdx,DATA3D_f$z.POdx, data = DATA3D_f, size = 1)
shapelist3d(icosahedron3d(col = "palevioletred4"), DATA3D_f$x.POsin,DATA3D_f$y.POsin,DATA3D_f$z.POsin, data = DATA3D_f, size = 1)
shapelist3d(icosahedron3d(col = "paleturquoise4"), DATA3D_m$x.POdx,DATA3D_m$y.POdx,DATA3D_m$z.POdx, data = DATA3D_m, size = 1)
shapelist3d(icosahedron3d(col = "paleturquoise4"), DATA3D_m$x.POsin,DATA3D_m$y.POsin,DATA3D_m$z.POsin, data = DATA3D_m, size = 1)
shapelist3d(icosahedron3d(col = "palevioletred4"), prumF_POdx_x,prumF_POdx_y,prumF_POdx_z, size = 4, alpha = 0.5)
shapelist3d(icosahedron3d(col = "palevioletred4"), prumF_POsin_x,prumF_POsin_y,prumF_POsin_z, size = 4, alpha = 0.5)
shapelist3d(icosahedron3d(col = "paleturquoise4"), prumM_POdx_x,prumM_POdx_y,prumM_POdx_z, size = 4, alpha = 0.5)
shapelist3d(icosahedron3d(col = "paleturquoise4"), prumM_POsin_x,prumM_POsin_y,prumM_POsin_z, size = 4, alpha = 0.5)
# elipsoidy pro pravou stranu
# zeny
ellips_F_dx <- ellipse3d(cov(cbind(DATA3D_f$x.POdx,DATA3D_f$y.POdx,DATA3D_f$z.POdx)), 
                      centre=c(prumF_POdx_x,prumF_POdx_y,prumF_POdx_z), level = 0.50)
wire3d(ellips_F_dx, col = "palevioletred1",  lit = FALSE)
# muzi
ellips_M_dx <- ellipse3d(cov(cbind(DATA3D_m$x.POdx,DATA3D_m$y.POdx,DATA3D_m$z.POdx)), 
                      centre=c(prumM_POdx_x,prumM_POdx_y,prumM_POdx_z), level = 0.50)
wire3d(ellips_M_dx, col = "paleturquoise3",  lit = FALSE)
# elipsoidy pro levou stranu
# zeny
ellips_F_sin <- ellipse3d(cov(cbind(DATA3D_f$x.POsin,DATA3D_f$y.POsin,DATA3D_f$z.POsin)), 
                         centre=c(prumF_POsin_x,prumF_POsin_y,prumF_POsin_z), level = 0.50)
wire3d(ellips_F_sin, col = "palevioletred1",  lit = FALSE)
# muzi
ellips_M_sin <- ellipse3d(cov(cbind(DATA3D_m$x.POsin,DATA3D_m$y.POsin,DATA3D_m$z.POsin)), 
                         centre=c(prumM_POsin_x,prumM_POsin_y,prumM_POsin_z), level = 0.50)
wire3d(ellips_M_sin, col = "paleturquoise3",  lit = FALSE)
# usecky spojujici prumery
rgl.lines(x = c(prumF_POdx_x, prumF_POsin_x),
          y = c(prumF_POdx_y, prumF_POsin_y),
          z = c(prumF_POdx_z, prumF_POsin_z),
          color="palevioletred4", lwd = 2)
rgl.lines(x = c(prumM_POdx_x, prumM_POsin_x),
          y = c(prumM_POdx_y, prumM_POsin_y),
          z = c(prumM_POdx_z, prumM_POsin_z),
          color="paleturquoise4", lwd = 2)

######### 3 ######### 
data_vzorky <- read.delim("data_density_cv3.txt",header = T)
str(data_vzorky)
attach(data_vzorky)
levels(type)

min(Lat)
max(Lat)
min(Long)
max(Long)

bbox <- c(left = -71.18, bottom = 42.22, right = -71.0, top = 42.4)
library(ggplot2)
library(ggmap)
library(RColorBrewer)

# vytvorime a zobrazime mapu
naleziste <- get_map(bbox, maptype = "toner-lines", source = "stamen", zoom = 12)
ggmap(naleziste)
# zobrazime body
windows()
ggmap(naleziste) + geom_point(data = data_vzorky, aes(x = Long, y = Lat, alpha = type),
                                 shape = 19, col = "darkblue")
# zobrazime body na zaklade ctvercu urcujicich prevahu jednotlivych nalezu
windows()
ggmap(naleziste) +  stat_bin2d(aes(x = Long, y = Lat, colour = type, fill = type),
                                  size = 0.5, bins = 30, alpha = 0.5, data = data_vzorky)
# vytvorime mapu denzity
windows()
ggmap(naleziste) + 
  stat_density2d(aes(x = Long, y = Lat, fill = ..level.., alpha = ..level..),
                                     size = 2, bins = 20, data = data_vzorky,
                                     geom = "polygon")+
  scale_fill_gradientn(colors = brewer.pal(7, "BuPu"))

library(leaflet)
library(leaflet.extras)

leaflet(data_vzorky) %>% 
  addProviderTiles(providers$Esri.WorldImagery)  %>% 
  addHeatmap( lng = ~ Long, lat = ~ Lat, max = 0.6, blur = 60)

######### 4 ######### 

library(Hmisc)
# vypocet korelacni matice a matice p-hodnot
# complete cases
res1 <- rcorr(as.matrix(DATA[complete.cases(DATA),3:9]))
res1
# vse (vysledky se prilis nelisi, ale je dulezite CO pocitame)
res <- rcorr(as.matrix(DATA[,3:9]))
res
library(corrplot)
library(RColorBrewer)
corrplot(res$r, type="lower", p.mat = res$P, order = "hclust", tl.col = "black", 
         sig.level = 0.01, insig = "blank", method = "color", addCoef.col = "gray58",
         col=brewer.pal(n = 11, name="PuOr"))

library(corrplot)
corrplot(res1$r, type="lower", p.mat = res1$P, order = "hclust", tl.col = "black", 
         sig.level = 0.01, insig = "blank", method = "color", addCoef.col = "gray58",
         col=brewer.pal(n = 11, name="PuOr"))

