################################
  
#### Data manipulation for the Switzerland dataset
  
## Load the Switzerland data
  data(Switzerland)
  
## Check that the proportions sum to 1 (required for DI models)
## p1 to p4 are in the 4th to 7th columns in Switzerland
  Switzerlandsums <- rowSums(Switzerland[4:7])
  summary(Switzerlandsums)
  
  
## Create FG interaction variables and incorporate them into a new data frame Switzerland2.
## Switzerland2 will contain the new variables:  bfg_G_L, wfg_G and wfg_L.
  FG_matrix <- DI_data(prop = c("p1","p2","p3","p4"), FG = c("G","G","L","L"), 
                       data = Switzerland, what = "FG")
  Switzerland2 <- data.frame(Switzerland, FG_matrix)
  
  
## Create FG interaction variables and incorporate them into a new data frame Switzerland3.
## Use theta = 0.5.
  FG_matrix <- DI_data(prop = c("p1","p2","p3","p4"), FG = c("G","G","L","L"), 
                       data = Switzerland, what = "FG", theta = 0.5)
  Switzerland3 <- data.frame(Switzerland, FG_matrix)
## Add "_theta" to the new interaction variables to differentiate from when theta = 1
  names(Switzerland3)[9:11] <- paste0(names(Switzerland3)[9:11], "_theta") 
#### All interactions can be added to a new dataset all together:
## Create all interactions and add them to a new data frame called Switzerland4
  newlist <- DI_data(prop = c("p1","p2","p3","p4"), FG = c("G","G","L","L"), data = Switzerland, 
                     what = c("E", "AV", "FG", "ADD", "FULL"))
  Switzerland4 <- data.frame(Switzerland, "E" = newlist$E, "AV" = newlist$AV, newlist$FG, 
                             newlist$ADD, newlist$FULL)
  
#### Or the various interactions can also be added to a new dataset individually:
  
## Create the average pairwise interaction and evenness variables
##  and store them in a new data frame called Switzerland5.
## Switzerland5 will contain the new variables: AV, E
  E_variable <- DI_data(prop = c("p1","p2","p3","p4"), data = Switzerland, what = "E")
  AV_variable <- DI_data(prop = c("p1","p2","p3","p4"), data = Switzerland, what = "AV")
  Switzerland5 <- data.frame(Switzerland, "AV" = AV_variable, "E" = E_variable)
  
## Create the functional group variables and add them to Switzerland5.
## In the FG names vector: G stands for grass, L stands for legume.
## Switzerland5 will contain: bfg_G_L, wfg_G and wfg_L
  FG_matrix <- DI_data(prop = 4:7, FG = c("G","G","L","L"), data = Switzerland, what = "FG")
  Switzerland5 <- data.frame(Switzerland5, FG_matrix)
  
## Create the additive species variables and add them to Switzerland5.
## Switzerland5 will contain the new variables: p1_add, p2_add, p3_add and p4_add.
  ADD_matrix <- DI_data(prop = c("p1","p2","p3","p4"), data = Switzerland, what = "ADD")
  Switzerland5 <- data.frame(Switzerland5, ADD_matrix)
  
## Create all pairwise interaction variables and add them to Switzerland5.
## Switzerland5 will contain the new variables: p1.p2, p1.p3, p1.p4, p2.p3, p2.p4, p3.p4.
  FULL_matrix <- DI_data(prop = c("p1","p2","p3","p4"), data = Switzerland, what = "FULL")
  Switzerland5 <- data.frame(Switzerland5, FULL_matrix)
  
################################
  
################################ 
  
#### Short worked example (as illustrated the Details section)
  
## Create a dataframe
  p1 <- c(0.1, 0.25)
  p2 <- c(0.2, 0.25)
  p3 <- c(0.3, 0.25)
  p4 <- c(0.4, 0.25)
  minidataset1 <- data.frame(p1,p2,p3,p4)
  
## Check the rows sum to 1
  rowSums(minidataset1[1:4]) 
  
## Create the FG variables, assume two functional groups and theta = 1
  FG_matrix <- DI_data(prop = c("p1","p2","p3","p4"), FG = c("G","G","L","L"),
                       data = minidataset1, what = "FG")
  minidataset2 <- data.frame(minidataset1, FG_matrix)
  
## Create the FG variables, assume two functional groups and theta = 0.5
  FG_matrix <- DI_data(prop = c("p1","p2","p3","p4"), FG = c("G","G","L","L"), 
                       data = minidataset1, what = "FG", theta = 0.5)
  minidataset3 <- data.frame(minidataset1, FG_matrix)
## Create the ADD variables, assume theta = 0.5
  ADD_matrix <- DI_data(prop = c("p1","p2","p3","p4"), 
                        data = minidataset1, what = "ADD", theta = 0.5)
  minidataset3 <- data.frame(minidataset3, ADD_matrix)
## Add "_theta" to the new interaction variables to differentiate from when theta = 1
  names(minidataset3)[5:11] <- paste0(names(minidataset3)[5:11], "_theta")   
  
################################
Run the code above in your browser using DataLab