### Bruslarky
bru<-read.delim("clipboard")

summary(bru)
dim(bru)
pca.1<-rda(bru[,4:11], scale=T)# PCA se standardizaci dat
pca.1
# Call: rda(X = bru[, 4:11], scale = T)
# 
#               Inertia Rank
# Total               8     
# Unconstrained       8    8
# Inertia is correlations 
# 
# Eigenvalues for unconstrained axes:
#   PC1   PC2   PC3   PC4   PC5   PC6   PC7   PC8 
# 7.639 0.251 0.056 0.021 0.014 0.008 0.007 0.005 
screeplot(pca.1, bstick = T)# Screeplot s BS modelem

aa<-ordiplot(pca.1, type='n') # prazdny ordinacni diagram
text(aa, "sp", arrows = T, length=0.05) # pridani promennych ("druhu") do diagramu
points(aa, "si", pch=".")# Zobrazeni vzorku teckami
# pridani obalek
ordihull(pca.1, groups=bru$instar, # definice skupin
         draw="poly",# co chci namalovat - zde polygony 
         col=2:7,# specifikace barev - defaultne jsou polopruhledne
         lty=0 #nechci malovat okolo polygonu hranicni cary 
         )
legend("bottomright", fill=2:7, legend=unique(bru$instar), 
       ncol=3, title="Instar") # pridani legendy. Pro podrobnosti mrknete na ?legend


## Pakomari

pako.env<-read.delim("clipboard", row.names = 1)
pako.spe<-read.delim("clipboard",row.names = 1)
summary(pako.spe)

pako.spe[is.na(pako.spe)]<-0 # Nahrazeni NA hodnot (pradznych bunek v Excelu) nulami
pako.spe<-pako.spe[, colSums(pako.spe>0)>1] # odstraneni druhu s jednotlivymi 
# vyskyty. Zkuste tomu skriptu porozumet

dca.1<-decorana(log(pako.spe+1))
# Detrended correspondence analysis with 26 segments.
# Rescaling of axes with 4 iterations.
# 
#                   DCA1    DCA2    DCA3    DCA4
# Eigenvalues     0.2515 0.11365 0.05767 0.03509
# Decorana values 0.2620 0.08353 0.04597 0.02029
# Axis lengths    1.9168 1.38527 1.07346 0.79291

pako.sh.names<-make.cepnames(names(pako.spe))#Kratka jmena druhu
substr(pako.sh.names,5,5)<-toupper(substr(pako.sh.names, 5,5))#Vymena 5. pismene za VELKE

sel<-weights(dca.1, "sp")>5 #Vyber druhu pro zobrazeni - druhy s vetsi vahou v analyze
# nez 5
# Doporucuju mrknout na 
hist(weights(dca.1, "sp")) # aby si clovek udelal predstavu o rozlozeni vah

aa<-ordiplot(dca.1, type='n')
points(aa, "si")
text(aa, "sp", 
     labels=pako.sh.names, # specifikace pouziti kratkych jmen jako labelu
     cex=0.6, 
     col=rgb(0,0,0.8,0.5), # modra polopruhledna barva
     font=3, # kurziva
     select=sel # vyber druhu k zobrazeni
     )


nmds.1<-metaMDS(log(pako.spe+1), k=2)
nmds.1$stress
# [1] 0.1475333
nmds.2<-metaMDS(log(pako.spe+1), k=3)
nmds.2$stress
# [1] 0.08849311
nmds.3<-metaMDS(log(pako.spe+1), k=4)
nmds.3$stress
# [1] 0.06019171
nmds.4<-metaMDS(log(pako.spe+1), k=5)
nmds.4$stress
# [1] 0.04685683

stressplot(nmds.2) #Zobrazeni stressplotu pro nmds.2
aa<-ordiplot(nmds.2, type="n")
points(aa, "sites")

ef<-envfit(nmds.2, pako.spe, permutations = 0)# Fitovani korelaci druhu s
#ordinacnimi osami, tady me nezajima signifikance, ale jen r2

text(aa, "sp", 
     labels=pako.sh.names, # specifikace pouziti kratkych jmen jako labelu
     cex=0.7, 
     col=rgb(0,0,0.8,0.5), # modra polopruhledna barva
     font=3, # kurziva
     select=ef$vectors$r>0.2# vyber zobrazeni jen tech druhu jejichz r2 s osami
     #je vetsi nez 0.2
     )

ef.2<-envfit(nmds.2, pako.env, permutations = 999)# Testovani korelaci promennych 
#prostedi s osami
plot(ef.2, col=2,p.max=0.05) #Pasivni promitnuti promennych prostredi do ordinacniho diagramu-
# pouze tech, ktere jsou prukazne korelovane s ordiacnimi osami

