Homework 3
Homework 3
Instructions
1. Submit your solution on Canvas as a pdf file before October 17 at 11:59pm.
2. Use R for all computation, and include your code.
3. You may find it helpful to start with the template hw3_template.Rmd file on Canvas.
Rubric
• [15 points] We will grade one of the textbook problems and the computation problem for,
– [8 points] Correctness. Are all parts answered completely and correctly?
– [7 points] Clarity. Are all answers justified? For questions with a coding component, is the code
provided, and does it reflect good code style?
• [10 points] The remaining problems below will be graded for completeness.
• Total: 25 points
Textbook Problems
Design and Analysis of Experiments, 10th Edition, problems: 4.8, 4.11, 4.25, 4.27.
In case you don’t have this edition, the problems can be viewed here.
Computation Problem
In the presence of nuisance factors, RCBDs can lead to substantial improvements in power. This problem
will measure the extent of the gains.
The functions below generate datasets from the model,
yij = µ+ τi + βj + �ij
�ij ∼ N
(
0, σ2
)
.
As usual, τi is interpreted as a treatment effect for group i, βj is a block effect for block j, and �ij is random
variation within the given treatment and block. In the function, the argument method controls whether or
not an RCBD was used to assign treatments – method = “block” or method = “random”, respectively.
library(dplyr)
library(ggplot2)
generate_design <- function(taus, betas, method) {
# number of replicates per block is the number of treatment levels
design <- expand.grid(replicate = seq_along(taus), block = seq_along(betas))
# assign each treatment in each block
if (method == "block") {
design <- design %>%
group_by(block) %>%
mutate(treatment = sample(1:n()))
# assign treatments randomly, ignoring blocks
1
https://style.tidyverse.org/
https://uwmadison.box.com/shared/static/9d1nsxhb2wa9sjxcgwig2zgogall6dtk.pdf
} else if (method == “random”) {
random_assignment <- sample(rep(seq_along(taus), length(betas)))
design <- mutate(design, treatment = random_assignment)
}
design
}
generate_data <- function(taus, betas, method) {
generate_design(taus, betas, method) %>%
mutate(y = taus[treatment] + betas[block] + rnorm(n())) %>%
mutate_at(vars(-y), as.factor)
}
The code above is included for completeness. In this problem, you will only need to be able to use the
functions (you do not need to change any part of it). For example, the block below generates two datasets
with and without an RCBD design. The true treatment effect is τ = (−1.5,−0.5, 0.5, 1.5) and the true block
effect is β = (−2, 0, 2). The figure below visualizes these datasets.
taus <- c(-1.5, -0.5, 0.5, 1.5) # effects for 4 treatments
betas <- c(-2, 0, 2) # effects across 3 blocks
experiment1 <- generate_data(taus, betas, "block")
experiment2 <- generate_data(taus, betas, "random")
−4
−2
0
2
1 2 3 4
treatment
y
block 1 2 3
RCBD Design
−2
0
2
1 2 3 4
treatment
y
block 1 2 3
Random Design
1. For both the datasets above, test whether there is any variation in the response y as a function of the
treatment. Specifically, provide a p-value associated with the hypothesis test,
H0 : τ1 = · · · = τa = 0
H1 : τi 6= 0 for at least one i,
using both experiment1 and experiment2. In the blocked experiment (experiment1), make sure
to account for block membership, as in the usual analysis of RCBDs. For the completely random
experiment (experiment2), do not use any information about block membership. For either experiment,
is the treatment effect detected at the 0.05 level?
2. Was the result in part (1) just a coincidence? To check, we can compare results across many generated
datasets. Specifically, generate many (e.g., B = 200) datasets like experiment1 and experiment2
above and extract p-values from both the RCBD and completely random analysis. Design a plot that
summarizes the differences between the designs (if any). What general conclusions can you draw?
3. Imagine a different value of τ and β of your choice. How would the plot you created in part (2) change
for this new value? You do not need to generate this plot, but provide a justification for your answer.
2
Instructions
Rubric
Textbook Problems
Computation Problem