% Options for packages loaded elsewhere
% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
\PassOptionsToPackage{dvipsnames,svgnames,x11names}{xcolor}
%
\documentclass[
  letterpaper,
  DIV=11,
  numbers=noendperiod]{scrartcl}
\usepackage{xcolor}
\usepackage{amsmath,amssymb}
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering
\usepackage{iftex}
\ifPDFTeX
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
  \usepackage{unicode-math} % this also loads fontspec
  \defaultfontfeatures{Scale=MatchLowercase}
  \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
\usepackage{lmodern}
\ifPDFTeX\else
  % xetex/luatex font selection
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
  \usepackage[]{microtype}
  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
  \IfFileExists{parskip.sty}{%
    \usepackage{parskip}
  }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
  \KOMAoptions{parskip=half}}
\makeatother
% Make \paragraph and \subparagraph free-standing
\makeatletter
\ifx\paragraph\undefined\else
  \let\oldparagraph\paragraph
  \renewcommand{\paragraph}{
    \@ifstar
      \xxxParagraphStar
      \xxxParagraphNoStar
  }
  \newcommand{\xxxParagraphStar}[1]{\oldparagraph*{#1}\mbox{}}
  \newcommand{\xxxParagraphNoStar}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
  \let\oldsubparagraph\subparagraph
  \renewcommand{\subparagraph}{
    \@ifstar
      \xxxSubParagraphStar
      \xxxSubParagraphNoStar
  }
  \newcommand{\xxxSubParagraphStar}[1]{\oldsubparagraph*{#1}\mbox{}}
  \newcommand{\xxxSubParagraphNoStar}[1]{\oldsubparagraph{#1}\mbox{}}
\fi
\makeatother

\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{241,243,245}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.40,0.45,0.13}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\BuiltInTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.13,0.47,0.30}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textit{#1}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textit{#1}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\ExtensionTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.28,0.35,0.67}{#1}}
\newcommand{\ImportTok}[1]{\textcolor[rgb]{0.00,0.46,0.62}{#1}}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\RegionMarkerTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.13,0.47,0.30}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.13,0.47,0.30}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.07,0.07,0.07}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.13,0.47,0.30}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textit{#1}}}

\usepackage{longtable,booktabs,array}
\newcounter{none} % for unnumbered tables
\usepackage{calc} % for calculating minipage widths
% Correct order of tables after \paragraph or \subparagraph
\usepackage{etoolbox}
\makeatletter
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
\makeatother
% Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}
\usepackage{graphicx}
\makeatletter
\newsavebox\pandoc@box
\newcommand*\pandocbounded[1]{% scales image to fit in text height/width
  \sbox\pandoc@box{#1}%
  \Gscale@div\@tempa{\textheight}{\dimexpr\ht\pandoc@box+\dp\pandoc@box\relax}%
  \Gscale@div\@tempb{\linewidth}{\wd\pandoc@box}%
  \ifdim\@tempb\p@<\@tempa\p@\let\@tempa\@tempb\fi% select the smaller of both
  \ifdim\@tempa\p@<\p@\scalebox{\@tempa}{\usebox\pandoc@box}%
  \else\usebox{\pandoc@box}%
  \fi%
}
% Set default figure placement to htbp
\def\fps@figure{htbp}
\makeatother





\setlength{\emergencystretch}{3em} % prevent overfull lines

\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}



 


\KOMAoption{captions}{tableheading}
\makeatletter
\@ifpackageloaded{caption}{}{\usepackage{caption}}
\AtBeginDocument{%
\ifdefined\contentsname
  \renewcommand*\contentsname{Table of contents}
\else
  \newcommand\contentsname{Table of contents}
\fi
\ifdefined\listfigurename
  \renewcommand*\listfigurename{List of Figures}
\else
  \newcommand\listfigurename{List of Figures}
\fi
\ifdefined\listtablename
  \renewcommand*\listtablename{List of Tables}
\else
  \newcommand\listtablename{List of Tables}
\fi
\ifdefined\figurename
  \renewcommand*\figurename{Figure}
\else
  \newcommand\figurename{Figure}
\fi
\ifdefined\tablename
  \renewcommand*\tablename{Table}
\else
  \newcommand\tablename{Table}
\fi
}
\@ifpackageloaded{float}{}{\usepackage{float}}
\floatstyle{ruled}
\@ifundefined{c@chapter}{\newfloat{codelisting}{h}{lop}}{\newfloat{codelisting}{h}{lop}[chapter]}
\floatname{codelisting}{Listing}
\newcommand*\listoflistings{\listof{codelisting}{List of Listings}}
\makeatother
\makeatletter
\makeatother
\makeatletter
\@ifpackageloaded{caption}{}{\usepackage{caption}}
\@ifpackageloaded{subcaption}{}{\usepackage{subcaption}}
\makeatother
\usepackage{bookmark}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\urlstyle{same}
\hypersetup{
  pdftitle={Problem Set 3},
  colorlinks=true,
  linkcolor={blue},
  filecolor={Maroon},
  citecolor={Blue},
  urlcolor={Blue},
  pdfcreator={LaTeX via pandoc}}


\title{Problem Set 3}
\author{}
\date{}
\begin{document}
\maketitle


\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
  \textbf{The Secret to Longevity}. Scientists are interested in whether
  the energy costs involved in reproduction affect longevity\footnote{Question
    from Problem 3.2 in Oehlert textbook, original data from Hanley and
    Shapiro (1994), ``Sexual activity and the lifespan of male
    fruitflies: a dataset that gets attention,'' \emph{Journal of
    Statistics Education 2}.}. In this experiment, 125 male fruit flies
  were divided at random into five sets of 25. In group 1, the males
  were kept by themselves. In groups 3 and 5, the males were supplied
  with one or eight receptive virgin female fruit flies per day,
  respectively. In groups 2 and 4, the males were supplied with one or
  eight unreceptive (pregnant) female fruit flies per day, respectively.
  Other than the number and type of companions, the males were treated
  identically. The longevity of the flies was observed.

  You can access the data as a csv file at:
  \url{https://stat158.berkeley.edu/spring-2026/data/fruit-flies/fruit-flies.csv}

  \begin{enumerate}
  \def\labelenumii{\alph{enumii}.}
  \item
    Visualize the data and interpret what you see. Be sure to comment
    both on the center of the distributions and their spread.
  \item
    \emph{ANOVA ``by hand''}. Without using any of the built-in commands
    (\texttt{aov()}, \texttt{anova()}) create a one-way ANOVA table
    minus the p-values. You're welcome to use either a base R or
    \texttt{dplyr} approach. It is a bit tedious, however it's useful to
    cobble together each of the statistics in an ANOVA table just once
    in your lives. You can check your answers with the output of
    \texttt{aov()} shown below.
  \end{enumerate}

\begin{verbatim}
             Df Sum Sq Mean Sq F value Pr(>F)
factor(trt)   4  11939  2984.8   13.61       
Residuals   120  26314   219.3               
\end{verbatim}

  \begin{enumerate}
  \def\labelenumii{\alph{enumii}.}
  \setcounter{enumii}{2}
  \item
    State your conclusion about the scientific question using
    Model-based inference. Be clear about the data generation model your
    approach relies upon and the null hypothesis that you're evaluating.
    To calculate the p-value, use the \texttt{pf()} function and then
    verify it using the \texttt{aov()} function\footnote{Recall all
      named probability distributions have a CDF in R with the prefix
      \texttt{p}. The F-distribution is no exception, so you can use
      \texttt{pf()} to calculate the p-value for your ANOVA table. The
      arguments to \texttt{pf()} are the F-statistic, the degrees of
      freedom for the numerator, and the degrees of freedom for the
      denominator. Recall a test using the F statistic is a right-tailed
      test. The standard anova output is available with
      \texttt{summary(aov(y\ \textasciitilde{}\ x,\ data))} (be sure
      your \texttt{x} is a factor vector).}.
  \item
    State your conclusion about the scientific question using
    Randomization-based Inference. Be clear about the data generation
    model your approach relies upon and the null hypothesis that you're
    evaluating. For your test statistic, use the F-statistic that you
    built the code for from part a above.
  \item
    How do the results of the model-based and randomization-based
    inference compare? Do they lead to the same conclusion? Do they rely
    on the same assumptions? Which do you think is more appropriate for
    this problem?
  \end{enumerate}
\end{enumerate}

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\setcounter{enumi}{1}
\item
  \textbf{More Comparisons for Longevity}. In the experiment regarding
  the fruit flies, the ANOVA analysis only tests whether there were any
  differences between the group means. Suppose there was further
  interest in whether the 1 pregnant group was different from the 1
  virgin group (for questions a and b, restrict the data set to these
  two groups).

  \begin{enumerate}
  \def\labelenumii{\alph{enumii}.}
  \item
    Perform a randomization test to compare these two groups using the
    t-statistic. Assume the two groups have the same variance (revisit
    your notes on the t-test and the pooled estimate of the SE).
  \item
    Repeat the randomization test but this time use an F-statistic. How
    do the results of this analysis this compare to the results in part
    (a)?
  \item
    Calculate an ANOVA table for this comparison and report the value of
    \(\sqrt{MS_{residuals}}\), which is an estimate of \(\sigma\). How
    does that compare to the estimate of the standard deviation that you
    used for the t-statistic (\(s_{p}\))?
  \end{enumerate}

  You are welcome to use built-in R commands for this question, if
  appropriate.
\end{enumerate}

\newpage{}

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\setcounter{enumi}{2}
\item
  \textbf{Poison}. Medical researchers investigated how to combat the
  effects of certain toxic agents (indeed poison, not Poisson!). For
  this, they randomly assigned 48 to a group where they are treated with
  a poison and a drug to counteract the poison. The survival time of the
  animal was recorded as the response of interest{[}\^{}death{]}. Three
  poisons and four drugs were considered, and four mice were assigned to
  each poison/drug combination. The responses are recorded in hours.

  You can access the data as a csv file at:
  \url{https://stat158.berkeley.edu/spring-2026/data/poison/poison.csv}

  \begin{enumerate}
  \def\labelenumii{\alph{enumii}.}
  \tightlist
  \item
    Create an interaction plot of the data and interpret it.
  \item
    Fit a two factor model with interactions and

    \begin{itemize}
    \tightlist
    \item
      \begin{enumerate}
      \def\labelenumiii{\roman{enumiii}.}
      \tightlist
      \item
        for each component of the model (main effect of poisons, main
        effect of drugs, interaction effects), calculate the value of
        observed F statistics and the corresponding p-values. You may
        calculate the p-values using either randomization-based
        inference or model-based inference (or both)\footnote{Note the
          interpretation of p-values associated with a test on
          interaction terms is different for RBI than MBI due to
          different null hypotheses. To test a null hypothesis analogous
          to the MBI that \(\gamma_{j,k} = 0 \forall j,k\) using RBI
          requires a more sophisticated shuffling method such as the
          Freedman-Lane Residual Permutation Test.}.
      \end{enumerate}
    \item
      \begin{enumerate}
      \def\labelenumiii{\roman{enumiii}.}
      \setcounter{enumiii}{1}
      \tightlist
      \item
        draw conclusions about differences among poisons, differences
        among treatments, and interactions between them. Is this a
        setting where it is important to estimate the interaction
        effects?
      \end{enumerate}
    \end{itemize}
  \end{enumerate}
\end{enumerate}

\newpage{}

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\setcounter{enumi}{3}
\item
  \textbf{Growing Herbs}. Botanists studying optimal greenhouse
  conditions for a fast-growing herb ran a fully-crossed three-way
  factorial experiment. They manipulated light intensity (low, medium,
  or high), watering frequency (low, medium, or high), and soil type
  (sandy or loam). Each of the \(3 \times 3 \times 2 = 18\) treatment
  combinations was replicated twice, yielding \(n = 36\) plants. The
  response is shoot dry weight (grams) measured after four weeks.

  You can load the data directly in R with the following code chunk:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{plants }\OtherTok{\textless{}{-}} \FunctionTok{data.frame}\NormalTok{(}
  \AttributeTok{weight =} \FunctionTok{c}\NormalTok{(}\FloatTok{12.7}\NormalTok{,}\FloatTok{14.2}\NormalTok{,}\FloatTok{15.7}\NormalTok{,}\FloatTok{12.6}\NormalTok{,}\FloatTok{16.4}\NormalTok{,}\FloatTok{17.2}\NormalTok{,}\FloatTok{15.0}\NormalTok{,}\FloatTok{17.4}\NormalTok{,}\FloatTok{20.5}\NormalTok{,}
             \FloatTok{11.9}\NormalTok{,}\FloatTok{15.2}\NormalTok{,}\FloatTok{16.4}\NormalTok{,}\FloatTok{12.9}\NormalTok{,}\FloatTok{17.9}\NormalTok{,}\FloatTok{18.7}\NormalTok{,}\FloatTok{15.9}\NormalTok{,}\FloatTok{18.1}\NormalTok{,}\FloatTok{25.7}\NormalTok{,}
             \FloatTok{11.3}\NormalTok{,}\FloatTok{13.4}\NormalTok{,}\FloatTok{15.5}\NormalTok{,}\FloatTok{12.0}\NormalTok{,}\FloatTok{16.6}\NormalTok{,}\FloatTok{18.0}\NormalTok{,}\FloatTok{15.6}\NormalTok{,}\FloatTok{17.6}\NormalTok{,}\FloatTok{22.1}\NormalTok{,}
             \FloatTok{14.8}\NormalTok{,}\FloatTok{13.9}\NormalTok{,}\FloatTok{18.3}\NormalTok{,}\FloatTok{14.8}\NormalTok{,}\FloatTok{17.8}\NormalTok{,}\FloatTok{19.2}\NormalTok{,}\FloatTok{16.7}\NormalTok{,}\FloatTok{17.9}\NormalTok{,}\FloatTok{26.4}\NormalTok{),}
  \AttributeTok{light  =} \FunctionTok{gl}\NormalTok{(}\DecValTok{3}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{36}\NormalTok{, }\AttributeTok{labels =} \FunctionTok{c}\NormalTok{(}\StringTok{"low"}\NormalTok{, }\StringTok{"medium"}\NormalTok{, }\StringTok{"high"}\NormalTok{)),}
  \AttributeTok{water  =} \FunctionTok{gl}\NormalTok{(}\DecValTok{3}\NormalTok{, }\DecValTok{3}\NormalTok{, }\DecValTok{36}\NormalTok{, }\AttributeTok{labels =} \FunctionTok{c}\NormalTok{(}\StringTok{"low"}\NormalTok{, }\StringTok{"medium"}\NormalTok{, }\StringTok{"high"}\NormalTok{)),}
  \AttributeTok{soil   =} \FunctionTok{gl}\NormalTok{(}\DecValTok{2}\NormalTok{, }\DecValTok{9}\NormalTok{, }\DecValTok{36}\NormalTok{, }\AttributeTok{labels =} \FunctionTok{c}\NormalTok{(}\StringTok{"sandy"}\NormalTok{, }\StringTok{"loam"}\NormalTok{))}
\NormalTok{)}
\end{Highlighting}
\end{Shaded}

  \begin{enumerate}
  \def\labelenumii{\alph{enumii}.}
  \item
    Create a \emph{main effects plot} for each of the three factors ---
    light intensity, watering frequency, and soil type --- and interpret
    each one. Which factor appears to have the largest main effect on
    shoot dry weight?
  \item
    Construct \emph{three two-way interaction plots} (one for each pair
    of factors: light × water, light × soil, water × soil) and interpret
    them. Do any of the pairs show evidence of an interaction?
  \item
    Construct a \emph{three-way interaction plot} by plotting mean shoot
    dry weight against one factor on the x-axis, a second factor mapped
    to color and line type, and using \texttt{facet\_wrap()} on the
    third. Interpret what you see: is there evidence that a two-way
    interaction changes in character across levels of the third factor?
  \item
    Fit a full three-way ANOVA model using
    \texttt{aov(weight\ \textasciitilde{}\ light\ *\ water\ *\ soil,\ data\ =\ plants)}.
    Report the ANOVA table and, for each term in the model, state
    whether you would conclude the effect is present.
  \end{enumerate}
\item
  \textbf{Estimation Three Ways}. We laid out the additive model for a
  two-way factorial with no interactions as

  \[
  Y_{i} = \mu + \alpha_{j(i)} + \beta_{k(i)} + \epsilon_{i}.
  \]

  The most commonly used estimators for these parameters are as follows.

  \[
  \begin{aligned}
  \hat{\mu} &= \hat{\bar{Y}} \\
  \hat{\alpha}_j &= \hat{\bar{Y}}_j - \hat{\bar{Y}} \\
  \hat{\beta}_k &= \hat{\bar{Y}}_k - \hat{\bar{Y}}
  \end{aligned}
  \]

  There are three different reasons that these estimates are sensible:

  \begin{enumerate}
  \def\labelenumii{\alph{enumii}.}
  \item
    They provide unbiased estimates of the corresponding causal
    parameters (shown in class).
  \item
    These estimates are the least squares estimates. That is, they are
    the values that minimize
  \end{enumerate}

  \[
  \sum_{i = 1}^n \left(Y_i - \left( \mu + \alpha_{j(i)} + \beta_{k(i)} \right) \right) ^2
  \]

  \begin{enumerate}
  \def\labelenumii{\alph{enumii}.}
  \setcounter{enumii}{2}
  \tightlist
  \item
    Under a generative model assumption that
    \(\epsilon_{i} \sim N(0, \sigma^2)\), these are the maximum
    likelihood estimates.
  \end{enumerate}

  Pick either b. or c.~and brush off your calculus skills to demonstrate
  that it is true. If you haven't seen maximum likelihood estimation
  before, stick to b.
\end{enumerate}




\end{document}
