---
title: "Bio_diversity"
author: "jin"
date: "2018年11月18日"
output: word_document
---
```{r}
knitr::opts_chunk$set(warning = F,message = F)
```
```{r setup}
setwd("C:/Users/jack/Desktop/mission/bio diversity")
library(readxl)
library(FD)
sample <- read_excel("sample.xlsx")
feature<-as.data.frame(sample[,-c(1,length(sample))])
abundance<-as.data.frame(sample[,length(sample)])
abundance<-t(abundance)
colnames(abundance)<-sample$种名
rownames(feature)<-sample$种名
feature
```
```{r load the data}
ex1<-dbFD(feature,abundance)
```
#功能性状距离FAD
```{r 功能性状距离FAD}
sum(dist(feature))
```
#功能性状平均距离MFAD
```{r 功能性状平均距离MFAD}
sum(dist(feature))/nrow(feature)
```
#功能树状图指数 FD
```{r 功能树状图指数 FD}
#Xtree
Xtree <- function(h)
{
species.names <- h$labels
H1 <- matrix(0, length(h$order), 2 * length(h$order) - 2)
l <- vector("numeric", 2 * length(h$order) - 2)
for(i in 1:(length(h$order) - 1)) {
# evaluate branch lengths
#
if(h$merge[i, 1] < 0) {
l[2 * i - 1] <- h$height[order(h$height)[i]]
H1[ - h$merge[i, 1], 2 * i - 1] <- 1
}
else {
l[2 * i - 1] <- h$height[order(h$height)[i]] - h$height[order(h$height)[h$merge[i, 1]]]
H1[, 2 * i - 1] <- H1[, 2 * h$merge[i, 1] - 1] + H1[
, 2 * h$merge[i, 1]]
}