# NOT RUN {
################################
#### 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 new interaction variables and incorporate them into a new data frame Switzerland2.
## Switzerland2 will contain the new variables: AV, E, p1_add, p2_add, p3_add, p4_add,
## bfg_G_L, wfg_G and wfg_L.
newlist <- DI_data_prepare(prop = c("p1","p2","p3","p4"), FG = c("G","G","L","L"),
data = Switzerland)
Switzerland2 <- data.frame(newlist$newdata, newlist$FG)
## Create new interaction variables and incorporate them into a new data frame Switzerland3.
## Use theta = 0.5.
newlist <- DI_data_prepare(prop = c("p1","p2","p3","p4"), FG = c("G","G","L","L"),
data = Switzerland, theta = 0.5)
Switzerland3 <- data.frame(newlist$newdata, newlist$FG)
## Add "_theta" to the new interaction variables to differentiate from when theta = 1
names(Switzerland3)[12:20] <- paste0(names(Switzerland3)[12:20], "_theta")
#### 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 Switzerland4.
## Switzerland4 will contain the new variables: AV, E
newlist <- DI_data_E_AV(prop = c("p1","p2","p3","p4"), data = Switzerland)
Switzerland4 <- data.frame(Switzerland, "AV" = newlist$AV, "E" = newlist$E)
## Create the functional group variables and add them to Switzerland4.
## In the FG names vector: G stands for grass, L stands for legume.
## Switzerland4 will contain: bfg_G_L, wfg_G and wfg_L
newlist <- DI_data_FG(prop = 4:7, FG = c("G","G","L","L"), data = Switzerland)
Switzerland4 <- data.frame(Switzerland4, newlist$FG)
## Create the additive species variables and add them to Switzerland4.
## Switzerland4 will contain the new variables: p1_add, p2_add, p3_add and p4_add.
newlist <- DI_data_ADD(prop = c("p1","p2","p3","p4"), data = Switzerland)
Switzerland4 <- data.frame(Switzerland4, newlist$ADD)
## Create all pairwise interaction variables and add them to Switzerland4.
## Switzerland5 will contain the new variables: p1.p2, p1.p3, p1.p4, p2.p3, p2.p4, p3.p4.
newlist <- DI_data_fullpairwise(prop = c("p1","p2","p3","p4"), data = Switzerland)
Switzerland4 <- data.frame(Switzerland4, newlist)
################################
################################
#### 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 interaction variables, assume two functional groups and theta = 1
newlist <- DI_data_prepare(prop = c("p1","p2","p3","p4"), FG = c("G","G","L","L"),
data = minidataset1)
minidataset2 <- data.frame(newlist$newdata, newlist$FG)
## Create the interaction variables, assume two functional groups and theta = 0.5
newlist <- DI_data_prepare(prop = c("p1","p2","p3","p4"), FG = c("G","G","L","L"),
y = "response", data = minidataset1, theta = 0.5)
minidataset3 <- data.frame(newlist$newdata, newlist$FG, check.names = FALSE)
## Add "_theta" to the new interaction variables to differentiate from when theta = 1
names(minidataset3)[8:16] <- paste0(names(minidataset3)[8:16], "_theta")
################################
# }
Run the code above in your browser using DataLab