library(data.table)
library(FactoMineR)
library(factoextra)
## Загрузка требуемого пакета: ggplot2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
#открываем таблицу
table = as.data.frame(fread(
"https://esapubs.org/archive/ecol/E096/102/HeteropteraMorphometricTraitsRAW.txt"))
#Задание 3
one.way = aov(Body_length ~ Source, data = table)
summary(one.way)
## Df Sum Sq Mean Sq F value Pr(>F)
## Source 2 89.8 44.89 7.856 0.000447 ***
## Residuals 422 2411.4 5.71
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
boxplot(Body_length ~ Source, data = table)

TukeyHSD(one.way, conf.level=0.95)
## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = Body_length ~ Source, data = table)
##
## $Source
## diff
## Research Collection of Martin M. Gossner-Biodiversity Exploratories 4.6459887
## Zoological State Collection Munich-Biodiversity Exploratories -0.7865214
## Zoological State Collection Munich-Research Collection of Martin M. Gossner -5.4325101
## lwr
## Research Collection of Martin M. Gossner-Biodiversity Exploratories -0.9922081
## Zoological State Collection Munich-Biodiversity Exploratories -1.3402089
## Zoological State Collection Munich-Research Collection of Martin M. Gossner -11.0662167
## upr
## Research Collection of Martin M. Gossner-Biodiversity Exploratories 10.2841855
## Zoological State Collection Munich-Biodiversity Exploratories -0.2328340
## Zoological State Collection Munich-Research Collection of Martin M. Gossner 0.2011965
## p adj
## Research Collection of Martin M. Gossner-Biodiversity Exploratories 0.1293275
## Zoological State Collection Munich-Biodiversity Exploratories 0.0026113
## Zoological State Collection Munich-Research Collection of Martin M. Gossner 0.0614890
#Задание 4
#проводим PCA, на рисунке насекомые из разных локаций не разделяются,
#если учитывать все количественные признаки
table.pca = PCA(table[,12:ncol(table)], graph = FALSE)
## Warning in PCA(table[, 12:ncol(table)], graph = FALSE): Missing values are
## imputed by the mean of the variable: you should use the imputePCA function of
## the missMDA package
fviz_pca_ind(table.pca,
geom.ind = "point",
col.ind = table[,9],
palette = c("#00AFBB", "#E7B800", "#FC4E07"),
addEllipses = TRUE,
legend.title = "Geogr.loc.")
## Too few points to calculate an ellipse

#Задание 5б
#проводим иерархическую кластеризацию, учитывая все количественные признаки
#красим ветви по семействам
#на риcунке хорошо выделяются кластеры, обогащенные определенными семействами
#(например, слева кластер "Pentatomidae",
#правее - кластер семейств "Anthocoridae" и "Tingidae",
#ближе к середине - кластер "Berytidae",
#а ближе к правому краю - кластер "Nabidae")
library(dendextend)
##
## ---------------------
## Welcome to dendextend version 1.17.1
## Type citation('dendextend') for how to cite the package.
##
## Type browseVignettes(package = 'dendextend') for the package vignette.
## The github page is: https://github.com/talgalili/dendextend/
##
## Suggestions and bug-reports can be submitted at: https://github.com/talgalili/dendextend/issues
## You may ask questions at stackoverflow, use the r and dendextend tags:
## https://stackoverflow.com/questions/tagged/dendextend
##
## To suppress this message use: suppressPackageStartupMessages(library(dendextend))
## ---------------------
##
## Присоединяю пакет: 'dendextend'
## Следующий объект скрыт от 'package:data.table':
##
## set
## Следующий объект скрыт от 'package:stats':
##
## cutree
clusters = as.dendrogram(hclust(dist(table[,12:ncol(table)]), method = 'complete'))
clusters = set(clusters, "labels_cex", 0.5)
family_colors = gsub("Tingidae","red",table$Family)
family_colors = gsub("Miridae","blue",family_colors)
family_colors = gsub("Pentatomidae","green",family_colors)
family_colors = gsub("Alydidae","yellow",family_colors)
family_colors = gsub("Anthocoridae","orange",family_colors)
family_colors = gsub("Coreidae","pink",family_colors)
family_colors = gsub("Berytidae","violet",family_colors)
family_colors = gsub("Rhopalidae","lightblue",family_colors)
family_colors = gsub("Saldidae","darkred",family_colors)
family_colors = gsub("Lygaeidae","darkblue",family_colors)
family_colors = gsub("Plataspididae","darkgreen",family_colors)
family_colors = gsub("Acanthosomatidae","darkorange",family_colors)
family_colors = gsub("Scutelleridae","darkviolet",family_colors)
family_colors = gsub("Nabidae","cyan",family_colors)
family_colors = gsub("Reduviidae","darkcyan",family_colors)
family_colors = gsub("Piesmatidae","coral",family_colors)
colLab <- function(n) {
if(is.leaf(n)) {
a <- attributes(n)
attr(n, "label") <- table$Family[a$label]
attr(n, "nodePar") <- c(a$nodePar, lab.col = family_colors[a$label])
}
n
}
plot(dendrapply(clusters, colLab))

#Задание 5а
#проводим ординацию (PCA) по тем же семействам, учитывая все количественные признаки
#видно, что хорошо отделяются от других кластеры тех же семейств, что и в прошлом задании
#например, "Berytidae", "Anthocoridae"+"Tingidae", "Pentatomidae" и "Nabidae")
fviz_pca_ind(table.pca,
geom.ind = "point",
col.ind = table$Family,
palette = names(table(family_colors)),
addEllipses = TRUE,
legend.title = "Families")
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Warning: The shape palette can deal with a maximum of 6 discrete values because more
## than 6 becomes difficult to discriminate
## ℹ you have requested 16 values. Consider specifying shapes manually if you need
## that many have them.
## Warning: Removed 318 rows containing missing values (`geom_point()`).
## Warning: Removed 10 rows containing missing values (`geom_point()`).
