####################################
## Code to simulate the sim5 dataset
# \donttest{
## Simulate dataset sim5 with 9 species and three functional groups.
## The species 1-5 are FG1, species 6-7 are FG2 and species 8-9 are FG3.
## Assume ID effects and the FG interactions model, with theta = 0.7.
## Set up proportions
data("design_a")
sim5 <- design_a
## Create the functional group interaction variables, with theta = 0.7.
FG_matrix <- DI_data(prop = 3:11, FG = c("FG1","FG1","FG1","FG1","FG1","FG2","FG2","FG3","FG3"),
data = sim5, theta = 0.7, what = "FG")
sim5 <- data.frame(sim5, FG_matrix)
names(sim5)[12:17] <- paste0(names(sim5)[12:17], "_theta")
## To simulate the response, first create a matrix of predictors that includes p1-p9, the
## treatment and the interaction variables.
X <- model.matrix(~ p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9
+ bfg_FG1_FG2_theta + bfg_FG1_FG3_theta + bfg_FG2_FG3_theta
+ wfg_FG1_theta + wfg_FG2_theta + wfg_FG3_theta -1, data = sim5)
## Create a vector of 'known' parameter values for simulating the response.
## The first nine are the p1-p9 parameters, and the second set of six are the interaction
## parameters.
sim5_coeff <- c(10,9,8,7,11, 6,5, 8,9, 8,3,6, 6,4,5)
##Create response and add normally distributed error
sim5$response <- as.numeric(X %*% sim5_coeff)
set.seed(35748)
r <- rnorm(n = 206, mean = 0, sd = 1.2)
sim5$response <- round(sim5$response + r, digits = 3)
sim5[,12:17] <- NULL
# }
###########################
## Analyse the sim5 dataset
## Load the sim5 data
data(sim5)
## View the first few entries
head(sim5)
## Explore the variables in sim5
str(sim5)
## Check characteristics of sim5
hist(sim5$response)
summary(sim5$response)
plot(sim5$richness, sim5$response)
plot(sim5$p1, sim5$response)
plot(sim5$p2, sim5$response)
plot(sim5$p3, sim5$response)
plot(sim5$p4, sim5$response)
plot(sim5$p5, sim5$response)
plot(sim5$p6, sim5$response)
plot(sim5$p7, sim5$response)
plot(sim5$p8, sim5$response)
plot(sim5$p9, sim5$response)
## What model fits best? Selection using F-test in autoDI
auto1 <- autoDI(y = "response", prop = 3:11,
FG = c("FG1","FG1","FG1","FG1","FG1","FG2","FG2","FG3","FG3"),
data = sim5, selection = "Ftest")
summary(auto1)
## Fit the functional group model, with theta, using DI and the FG tag
m1 <- DI(y = "response", prop = 3:11,
FG = c("FG1","FG1","FG1","FG1","FG1","FG2","FG2","FG3","FG3"), DImodel = "FG",
estimate_theta = TRUE, data = sim5)
summary(m1)
CI_95 <- theta_CI(m1, conf = .95)
CI_95
plot(m1)
# \donttest{
## Check goodness-of-fit using a half-normal plot with a simulated envelope
library(hnp)
hnp(m1)
# }
## Graph the profile likelihood
library(ggplot2)
ggplot(m1$profile_loglik, aes(x = grid, y = prof)) +
theme_bw() +
geom_line() +
xlim(0,1.5) +
xlab(expression(theta)) +
ylab("Log-likelihood") +
geom_vline(xintercept = CI_95, lty = 3) +
labs(title = " Log-likelihood versus theta",
caption = "dotted vertical lines are upper and lower bounds of 95% CI for theta")
## Fit the functional group model, with theta set equal to the estimate from m1, and custom_formula.
## Note, it is not possible to estimate theta with custom_formula (only select a 'known' value).
## First, create the functional group interactions (theta value as estimated from m1),
## store them in a new dataset and rename them with a theta indicator.
FG_matrix <- DI_data(prop = 3:11, FG=c("FG1","FG1","FG1","FG1","FG1","FG2","FG2","FG3","FG3"),
theta = 0.7296887, data = sim5, what = "FG")
sim5new <- data.frame(sim5, FG_matrix)
names(sim5new)[13:18] <- paste0(names(sim5new)[13:18], "_theta")
m2 <- DI(custom_formula = response ~ 0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 +
bfg_FG1_FG2_theta + bfg_FG1_FG3_theta + bfg_FG2_FG3_theta
+ wfg_FG1_theta + wfg_FG2_theta + wfg_FG3_theta, data = sim5new)
## This will adjust the standard errors in m2 for the 'estimation' of theta
m2$df.residual <- m2$df.residual - 1
## This will adjust the AIC in m2 for the 'estimation' of theta
m2$aic <- m2$aic + 2
summary(m2)
Run the code above in your browser using DataLab