library(DImodels)
library(dplyr)
## Load data
data(sim2)
## Fit model
mod <- glm(response ~ 0 + (p1 + p2 + p3 + p4)^2, data = sim2)
## Create data
## By default response would be averaged on the basis of richness
head(gradient_change_data(data = sim2,
prop = c("p1", "p2", "p3", "p4"),
model = mod))
## Average response with respect to evenness
head(gradient_change_data(data = sim2,
prop = c("p1", "p2", "p3", "p4"),
model = mod,
gradient = "evenness"))
## Additional variables can also be added to the data by either specifying
## them directly in the `data` or by using the `add_var` argument
## Refit model
sim2$block <- as.numeric(sim2$block)
new_mod <- update(mod, ~. + block, data = sim2)
## This model has block so we can either specify block in the data
subset_data <- sim2[c(1,5,9,11), 2:6]
subset_data
head(gradient_change_data(data = subset_data,
prop = c("p1", "p2", "p3", "p4"),
model = mod,
gradient = "evenness"))
## Or we could add the variable using `add_var`
subset_data <- sim2[c(1,5,9,11), 3:6]
subset_data
head(gradient_change_data(data = subset_data,
prop = c("p1", "p2", "p3", "p4"),
model = new_mod,
gradient = "evenness",
add_var = list(block = c(1, 2))))
## The benefit of specifying the variable this way is we have an ID
## columns now called `.add_str_ID` which could be used to create a
## separate plot for each value of the additional variable
## Model coefficients can also be used, but then user would have
## to specify the data with all columns corresponding to each coefficient
coef_data <- sim2 %>%
mutate(`p1:p2` = p1*p2, `p1:p3` = p1*p2, `p1:p4` = p1*p4,
`p2:p3` = p2*p3, `p2:p4` = p2*p4, `p3:p4` = p3*p4) %>%
select(p1, p2, p3, p4,
`p1:p2`, `p1:p3`, `p1:p4`,
`p2:p3`, `p2:p4`, `p3:p4`) %>%
slice(1,5,9,11)
print(coef_data)
print(mod$coefficients)
gradient_change_data(data = coef_data,
prop = c("p1", "p2", "p3", "p4"),
gradient = "evenness",
coefficients = mod$coefficients,
interval = "none")
Run the code above in your browser using DataLab