## Load the Switzerland data
data(Switzerland)
## Summarise the Switzerland data
summary(Switzerland)
## Fit a DI model
m1 <- DI(y = "yield", prop = 4:7, treat = 'nitrogen', DImodel = 'AV',
density = 'density', estimate_theta = FALSE, data = Switzerland)
summary(m1)
## Create contrasts
## We test the following five sample contrasts
## 1. p1 monoculture against p2 monoculture
## 2. p1 and p2 binary (50-50) mixture against the p4 monoculture
## 3. p1 and p2 binary (50-50) mixture against p3 and p4 (50-50) binary mixture
## 4. Three species equi-proportional mixture (1/3 each) of p2, p3, and p4 against p1 monoculture
## 5. p4 monoculture against a 70-30 mixture of p2 and p3 respectively.
contrast_vars <- data.frame("p1" = c(1, 0.5, 0.5, -1, 0),
"p2" = c(-1, 0.5, 0.5, 1/3, -0.7),
"p3" = c(0, 0, -0.5, 1/3, -0.3),
"p4" = c(0, -1, -0.5, 1/3, 1))
con1 <- contrasts_DI(object = m1,
contrast_vars = contrast_vars)
## Calling summary on the returned object would show a significane table for the tests
summary(con1)
## In the previous example, note how the species ID effects and interaction term
## was added to the contrast matrix. Additionally, the nitrogen50 and densityhigh
## variables were added with a value 0
## We could also calculate the contrast at a specific value for these additional variables
## Calculate contrast at nitrogen = 50 for the p1 monoculture against p3 monoculture
contrast_vars2 <- data.frame("p1" = 1, "p3" = -1, "nitrogen50" = 1)
# Name contrast to track it
rownames(contrast_vars2) <- "p1 mono vs p3 mono at 50 N"
con2 <- contrasts_DI(object = m1,
contrast_vars = contrast_vars2)
## Notice that p2 and p4 were added to the contrast with a value 0
summary(con2)
## It is also possible to specify the ID or interactions effects to have a set value
contrast_vars3 <- data.frame("p2_ID" = 0.5,
"p3_ID" = -0.5,
"AV" = 0.375)
con3 <- contrasts_DI(object = m1,
contrast_vars = contrast_vars3)
## Notice the values for terms specified are preserved while others are calculated or set to 0
summary(con3)
#############################################################################
## Using the `contrast` agrument for creating contrasts
## Contrasts for difference between monocultures of p1 and p2, p3 and p4, p2 and p3
con4 <- contrasts_DI(object = m1,
contrast = data.frame('p1vp2 Mono' = c(1, -1, 0, 0, 0, 0, 0),
'p3vp4 Mono' = c(0, 0, 1, -1, 0, 0, 0),
'p2vp3 Mono' = c(0, -1, 1, 0, 0, 0, 0)))
summary(con4)
## Contrasts for 50:50 mixture of p1 and p2 vs 50:50 mixture of p3 and p4
con5 <- contrasts_DI(object = m1,
contrast = data.frame('p1p2 vs p3p4' = c(0.5, 0.5, -0.5, -0.5, 0, 0, 0)))
summary(con5)
## There is also a helper function called `contrast_matrix` to create the
## full contrast matrix with interaction terms and any additional variables
## in the model which can be modified further by the user and passed
## into the contrast parameter
## Compare the three species mixture of p1, p2 and p3 to the centroid mixture
mix1 <- contrast_matrix(object = m1,
contrast_vars = data.frame("p1" = 1/3, "p2" = 1/3,
"p3" = 1/3))
mix2 <- contrast_matrix(object = m1,
contrast_vars = data.frame("p1" = 1/4, "p2" = 1/4,
"p3" = 1/4, "p4" = 1/4))
## The interaction terms and nitrogen and density terms are all added
## Subtract the two vectors to get a contrast
contr <- mix1 - mix2
con6 <- contrasts_DI(object = m1,
contrast = contr)
summary(con6)
## Could also modify a variable the returned output to test another contrast
## Suppose we wish to compare the three species mixture at 150 kg N vs the
## centroid at 50 kg N
mix1[, "nitrogen50"] <- 0
mix2[, "nitrogen50"] <- 1
contr <- mix1 - mix2
con7 <- contrasts_DI(object = m1,
contrast = contr)
summary(con7)
Run the code above in your browser using DataLab