download.file(url = "http://biogeo.ucdavis.edu/data/climate/worldclim/1_4/grid/cur/bio_10m_esri.zip",
              destfile = "WorldClim_data/current_bioclim_10min.zip",
              method = "auto")
unzip( zipfile = "WorldClim_data/current_bioclim_10min.zip",
       exdir = "WorldClim_data/current",
       overwrite = T)
list.files("WorldClim_data/current/bio/")
bioclim_world <- stack(list.files("WorldClim_data/current/bio", pattern ="bio_", full.names=T), RAT = FALSE)
plot(raster("bio_1.tif"))

plot(raster("bio_12.tif"))

bio_data<-read.table("biodata.csv",header=TRUE,sep=";")
head(bio_data)
##   ID bio_1 bio_10 bio_11 bio_12 bio_13 bio_14 bio_15 bio_16 bio_17 bio_18
## 1  1   115    202     40    483     58     13     34    153     61     62
## 2  2   110    159     59   1394    146     71     18    419    250    250
## 3  3   144    186    104    969    141     25     43    385    110    136
## 4  4   109    173     45    801     82     55     12    225    170    195
## 5  5   102    154     49   1522    173     68     24    478    243    243
## 6  6   133    179     92   1144    161     31     42    451    127    151
##   bio_19 bio_2 bio_3 bio_4 bio_5 bio_6 bio_7 bio_8 bio_9 decimalLatitude
## 1    133   113    37  6357   293    -8   301    99   200        40.22432
## 2    396   114    50  3895   238    10   228    84   159       -37.86285
## 3    323    60    39  3176   226    75   151   113   182        42.95462
## 4    183   130    47  4980   255   -18   273   105    52       -35.53403
## 5    469   114    49  4087   235     3   232    56   154       -37.81271
## 6    397    62    38  3361   223    63   160   101   175        42.97655
##   decimalLongitude   country
## 1        -5.148742     Spain
## 2       146.379080 Australia
## 3        -9.193672     Spain
## 4       149.240386 Australia
## 5       146.338105 Australia
## 6        -9.058642     Spain
summary(bio_data)
##        ID            bio_1           bio_10          bio_11  
##  Min.   :  1.0   Min.   : 71.0   Min.   :113.0   Min.   : 15.00
##  1st Qu.:108.2   1st Qu.:112.0   1st Qu.:173.0   1st Qu.: 51.25
##  Median :215.5   Median :124.0   Median :184.0   Median : 60.00
##  Mean   :215.5   Mean   :123.9   Mean   :183.1   Mean   : 63.87
##  3rd Qu.:322.8   3rd Qu.:131.0   3rd Qu.:191.0   3rd Qu.: 78.75
##  Max.   :430.0   Max.   :181.0   Max.   :251.0   Max.   :128.00
##      bio_12           bio_13          bio_14          bio_15
##  Min.   : 442.0   Min.   : 47.0   Min.   : 7.00   Min.   : 9.00
##  1st Qu.: 766.5   1st Qu.: 85.0   1st Qu.:42.00   1st Qu.:14.00
##  Median : 873.5   Median :100.0   Median :48.50   Median :22.00
##  Mean   : 925.7   Mean   :108.3   Mean   :49.13   Mean   :22.31
##  3rd Qu.:1042.2   3rd Qu.:125.0   3rd Qu.:57.00   3rd Qu.:29.00
##  Max.   :1781.0   Max.   :228.0   Max.   :71.00   Max.   :79.00
##      bio_16          bio_17          bio_18          bio_19
##  Min.   :130.0   Min.   : 47.0   Min.   : 47.0   Min.   :105.0
##  1st Qu.:227.0   1st Qu.:151.5   1st Qu.:174.0   1st Qu.:169.2
##  Median :276.0   Median :170.0   Median :210.0   Median :213.5
##  Mean   :295.8   Mean   :168.3   Mean   :211.8   Mean   :248.0
##  3rd Qu.:352.0   3rd Qu.:190.0   3rd Qu.:248.0   3rd Qu.:310.0
##  Max.   :633.0   Max.   :263.0   Max.   :419.0   Max.   :633.0
##      bio_2           bio_3           bio_4          bio_5
##  Min.   : 60.0   Min.   :34.00   Min.   :3032   Min.   :180.0
##  1st Qu.:109.0   1st Qu.:46.00   1st Qu.:4253   1st Qu.:243.0
##  Median :122.0   Median :47.00   Median :4720   Median :255.0
##  Mean   :115.8   Mean   :46.07   Mean   :4656   Mean   :257.5
##  3rd Qu.:129.0   3rd Qu.:48.00   3rd Qu.:5068   3rd Qu.:274.0
##  Max.   :148.0   Max.   :51.00   Max.   :6694   Max.   :331.0
##      bio_6            bio_7           bio_8           bio_9
##  Min.   :-34.00   Min.   :151.0   Min.   : 34.0   Min.   : 39.0
##  1st Qu.: -6.00   1st Qu.:232.0   1st Qu.: 84.0   1st Qu.: 72.0
##  Median :  4.00   Median :254.0   Median :106.0   Median :123.0
##  Mean   : 10.04   Mean   :247.4   Mean   :120.2   Mean   :126.8
##  3rd Qu.: 25.00   3rd Qu.:269.0   3rd Qu.:171.0   3rd Qu.:181.0
##  Max.   : 75.00   Max.   :301.0   Max.   :251.0   Max.   :229.0
##  decimalLatitude  decimalLongitude       country
##  Min.   :-42.99   Min.   : -9.264   Australia:378
##  1st Qu.:-36.64   1st Qu.:145.714   Spain    : 52
##  Median :-35.34   Median :149.041
##  Mean   :-25.68   Mean   :130.063
##  3rd Qu.:-32.76   3rd Qu.:149.561
##  Max.   : 43.42   Max.   :151.863
aggregate(bio_data,by=list(bio_data$country),FUN=mean)#sin usar fórmula
##     Group.1       ID    bio_1   bio_10   bio_11    bio_12   bio_13
## 1 Australia 217.7698 122.6614 181.9868 61.72222  906.8968 104.7804
## 2     Spain 199.0000 133.0962 191.5000 79.46154 1062.5385 134.1154
##     bio_14   bio_15   bio_16   bio_17   bio_18   bio_19     bio_2    bio_3
## 1 50.05026 21.27513 286.8333 168.5079 215.0979 235.7566 121.26720 47.23280
## 2 42.42308 29.84615 360.9808 166.3846 188.2115 337.2885  75.98077 37.61538
##      bio_4    bio_5     bio_6    bio_7     bio_8    bio_9 decimalLatitude
## 1 4690.029 259.2831  5.145503 254.1376 123.74603 118.3201       -35.08138
## 2 4405.942 244.2500 45.596154 198.6538  94.30769 188.5962        42.65719
##   decimalLongitude country
## 1       148.391126      NA
## 2        -3.170822      NA
boxplot(bio_1~country,data=bio_data)

t.test(bio_1~country,data=bio_data)
##
##  Welch Two Sample t-test
##
## data:  bio_1 by country
## t = -6.2393, df = 115.6, p-value = 7.461e-09
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -13.747369  -7.122188
## sample estimates:
## mean in group Australia     mean in group Spain
##                122.6614                133.0962
hist(bio_data$bio_1,main="Histograma")

aggregate(bio_data,by=list(bio_data$country),FUN=mean)#sin usar fórmula
##     Group.1       ID    bio_1   bio_10   bio_11    bio_12   bio_13
## 1 Australia 217.7698 122.6614 181.9868 61.72222  906.8968 104.7804
## 2     Spain 199.0000 133.0962 191.5000 79.46154 1062.5385 134.1154
##     bio_14   bio_15   bio_16   bio_17   bio_18   bio_19     bio_2    bio_3
## 1 50.05026 21.27513 286.8333 168.5079 215.0979 235.7566 121.26720 47.23280
## 2 42.42308 29.84615 360.9808 166.3846 188.2115 337.2885  75.98077 37.61538
##      bio_4    bio_5     bio_6    bio_7     bio_8    bio_9 decimalLatitude
## 1 4690.029 259.2831  5.145503 254.1376 123.74603 118.3201       -35.08138
## 2 4405.942 244.2500 45.596154 198.6538  94.30769 188.5962        42.65719
##   decimalLongitude country
## 1       148.391126      NA
## 2        -3.170822      NA
boxplot(bio_12~country,data=bio_data)

t.test(bio_12~country,data=bio_data)
##
##  Welch Two Sample t-test
##
## data:  bio_12 by country
## t = -4.0668, df = 61.063, p-value = 0.0001388
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -232.16813  -79.11514
## sample estimates:
## mean in group Australia     mean in group Spain
##                906.8968               1062.5385
hist(bio_data$bio_1,main="Histograma")

ggplot(bio_data,aes(x=bio_12,fill=country))+geom_histogram()

ggplot(bio_data,aes(x=bio_12,fill=country))+geom_density()

ggplot(bio_data,aes(x=bio_12))+geom_density()+facet_grid(.~country)

library(factoextra)
## Welcome! Related Books: `Practical Guide To Cluster Analysis in R` at https://goo.gl/13EFCZ
library(FactoMineR)
pca_adealbata<-PCA(bio_data[2:19])

fviz_pca(pca_adealbata,col.ind=bio_data$country)