% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
  \usepackage{unicode-math}
  \defaultfontfeatures{Scale=MatchLowercase}
  \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\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
\usepackage{xcolor}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\hypersetup{
  pdftitle={Homework 06 Solutions},
  hidelinks,
  pdfcreator={LaTeX via pandoc}}
\urlstyle{same} % disable monospaced font for URLs
\usepackage[margin=1in]{geometry}
\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}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\usepackage{longtable,booktabs}
% 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,grffile}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{5}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{array}
\usepackage{multirow}
\usepackage{wrapfig}
\usepackage{float}
\usepackage{colortbl}
\usepackage{pdflscape}
\usepackage{tabu}
\usepackage{threeparttable}
\usepackage{threeparttablex}
\usepackage[normalem]{ulem}
\usepackage{makecell}
\usepackage{xcolor}

\title{Homework 06 Solutions}
\author{}
\date{\vspace{-2.5em}}

\begin{document}
\maketitle

\hypertarget{problem-1}{%
\section{Problem 1}\label{problem-1}}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(tidyverse)}
\KeywordTok{library}\NormalTok{(kableExtra)}
\NormalTok{maskdata <-}\StringTok{ }\KeywordTok{read.csv}\NormalTok{(}\StringTok{"../maskdata.csv"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\hypertarget{part-a}{%
\subsection{Part a}\label{part-a}}

The first two code sections produce summary information and EDA as encouraged in the assignment.

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(knitr)}
\NormalTok{table_data <-}\StringTok{ }\NormalTok{maskdata }\OperatorTok{%>%}\StringTok{ }
\StringTok{  }\KeywordTok{mutate}\NormalTok{(}\DataTypeTok{study =} \KeywordTok{factor}\NormalTok{(study),}
         \DataTypeTok{allo.wm =} \KeywordTok{factor}\NormalTok{(study),}
         \DataTypeTok{mask.class =} \KeywordTok{factor}\NormalTok{(mask.class),}
         \DataTypeTok{virus =} \KeywordTok{factor}\NormalTok{(virus),}
         \DataTypeTok{breath.num =} \KeywordTok{factor}\NormalTok{(breath.num),}
         \DataTypeTok{female =} \KeywordTok{factor}\NormalTok{(female, }
                         \DataTypeTok{labels =} \KeywordTok{c}\NormalTok{(}\StringTok{"male"}\NormalTok{, }\StringTok{"female"}\NormalTok{)),}
         \StringTok{`}\DataTypeTok{log10(nasal)}\StringTok{`}\NormalTok{ =}\StringTok{ }\KeywordTok{log10}\NormalTok{(nasal),}
         \StringTok{`}\DataTypeTok{log10(throat)}\StringTok{`}\NormalTok{ =}\StringTok{ }\KeywordTok{log10}\NormalTok{(throat),}
         \StringTok{`}\DataTypeTok{log10(aerosols)}\StringTok{`}\NormalTok{ =}\StringTok{ }\KeywordTok{log10}\NormalTok{(aerosols),}
         \StringTok{`}\DataTypeTok{log10(droplets)}\StringTok{`}\NormalTok{ =}\StringTok{ }\KeywordTok{log10}\NormalTok{(droplets)}
\NormalTok{         ) }\OperatorTok{%>%}
\StringTok{  }\KeywordTok{select}\NormalTok{(}\OperatorTok{-}\KeywordTok{c}\NormalTok{(nasal, throat, aerosols, droplets))}

\KeywordTok{options}\NormalTok{(}\DataTypeTok{knitr.kable.NA =} \StringTok{''}\NormalTok{)}
\NormalTok{table_data }\OperatorTok{%>%}
\StringTok{  }\KeywordTok{select}\NormalTok{(female, study, allo.wm, mask.class, virus, breath.num,}
\NormalTok{         cough.num, }\StringTok{`}\DataTypeTok{log10(nasal)}\StringTok{`}\NormalTok{, }\StringTok{`}\DataTypeTok{log10(throat)}\StringTok{`}\NormalTok{, }\StringTok{`}\DataTypeTok{log10(aerosols)}\StringTok{`}\NormalTok{,}
         \StringTok{`}\DataTypeTok{log10(droplets)}\StringTok{`}\NormalTok{) }\OperatorTok{%>%}\StringTok{ }
\StringTok{  }\KeywordTok{summary}\NormalTok{() }\OperatorTok{%>%}\StringTok{ }
\StringTok{  }\NormalTok{knitr}\OperatorTok{::}\KeywordTok{kable}\NormalTok{(}\DataTypeTok{format =} \StringTok{"latex"}\NormalTok{,}
               \DataTypeTok{caption =} \StringTok{"Summary of variables in mask experiment."}\NormalTok{) }\OperatorTok{%>%}
\StringTok{  }\NormalTok{kableExtra}\OperatorTok{::}\KeywordTok{row_spec}\NormalTok{(}\DataTypeTok{row =} \DecValTok{0}\NormalTok{, }\DataTypeTok{bold =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{  }\NormalTok{kableExtra}\OperatorTok{::}\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{latex_options =}\KeywordTok{c}\NormalTok{(}\StringTok{"scale_down"}\NormalTok{)) }
\end{Highlighting}
\end{Shaded}

\begin{table}

\caption{\label{tab:unnamed-chunk-3}Summary of variables in mask experiment.}
\centering
\resizebox{\linewidth}{!}{
\begin{tabular}[t]{l|l|l|l|l|l|l|l|l|l|l|l}
\hline
\textbf{ } & \textbf{   female} & \textbf{ study} & \textbf{allo.wm} & \textbf{mask.class} & \textbf{ virus} & \textbf{breath.num} & \textbf{  cough.num} & \textbf{ log10(nasal)} & \textbf{log10(throat)} & \textbf{log10(aerosols)} & \textbf{log10(droplets)}\\
\hline
 & male  :49 & cold:81 & cold:81 & both:26 & cold:54 & 1:88 & Min.   : 0.00 & Min.   : 2.389 & Min.   :1.531 & Min.   :0.301 & Min.   :0.3010\\
\hline
 & female:65 & flu :33 & flu :33 & ctrl:43 & flu :43 & 2:26 & 1st Qu.: 0.00 & 1st Qu.: 5.532 & 1st Qu.:2.878 & 1st Qu.:0.301 & 1st Qu.:0.3010\\
\hline
 &  &  &  & mask:45 & hcov:17 &  & Median : 3.00 & Median : 6.514 & Median :3.854 & Median :0.301 & Median :0.3010\\
\hline
 &  &  &  &  &  &  & Mean   : 9.57 & Mean   : 6.412 & Mean   :4.150 & Mean   :1.150 & Mean   :0.6758\\
\hline
 &  &  &  &  &  &  & 3rd Qu.:11.00 & 3rd Qu.: 7.345 & 3rd Qu.:5.359 & 3rd Qu.:2.245 & 3rd Qu.:0.3010\\
\hline
 &  &  &  &  &  &  & Max.   :99.00 & Max.   :10.097 & Max.   :8.059 & Max.   :5.276 & Max.   :3.5185\\
\hline
 &  &  &  &  &  &  &  & NA's   :6 & NA's   :32 & NA's   :4 & NA's   :8\\
\hline
\end{tabular}}
\end{table}

We observe \texttt{NA} values in each of the viral load variables, the homework and project document mention that all these variables (recorded in copies of the virus per ml) have a value of \texttt{2} if the viral load was measured as 0, and that the \texttt{NA}s are associated with measurements not being taken or being lost. Table \ref{tab:tab-na} shows that most of these variables see very few \texttt{NA} values recorded. There also doesn't seem to be much relationship between when the \texttt{NA}s occur in these variables.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{na_table <-}\StringTok{ }\NormalTok{maskdata }\OperatorTok{%>%}\StringTok{ }
\StringTok{  }\NormalTok{dplyr}\OperatorTok{::}\KeywordTok{select}\NormalTok{(nasal, throat, aerosols, droplets) }\OperatorTok{%>%}
\StringTok{  }\KeywordTok{sapply}\NormalTok{(}\ControlFlowTok{function}\NormalTok{(col) }\KeywordTok{table}\NormalTok{(}\KeywordTok{is.na}\NormalTok{(col))) }

\NormalTok{any_na <-}\StringTok{ }\KeywordTok{sum}\NormalTok{(}\KeywordTok{apply}\NormalTok{(maskdata, }\DecValTok{1}\NormalTok{, }\ControlFlowTok{function}\NormalTok{(row) }\KeywordTok{any}\NormalTok{(}\KeywordTok{is.na}\NormalTok{(row))))}
\NormalTok{na_table <-}\StringTok{ }\KeywordTok{cbind}\NormalTok{(na_table, }\StringTok{`}\DataTypeTok{rows with any NA}\StringTok{`}\NormalTok{ =}\StringTok{ }\KeywordTok{c}\NormalTok{(}\KeywordTok{nrow}\NormalTok{(maskdata) }\OperatorTok{-}\StringTok{ }\NormalTok{any_na, any_na))}
\KeywordTok{rownames}\NormalTok{(na_table) <-}\StringTok{ }\KeywordTok{c}\NormalTok{(}\StringTok{"not NA"}\NormalTok{, }\StringTok{"is NA"}\NormalTok{)}

\NormalTok{na_table }\OperatorTok{%>%}
\StringTok{  }\NormalTok{knitr}\OperatorTok{::}\KeywordTok{kable}\NormalTok{(}\DataTypeTok{caption =} \StringTok{"Number of NAs in each viral load column."}\NormalTok{) }\OperatorTok{%>%}
\StringTok{  }\NormalTok{kableExtra}\OperatorTok{::}\KeywordTok{kable_styling}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

\begin{table}

\caption{\label{tab:tab-na}Number of NAs in each viral load column.}
\centering
\begin{tabular}[t]{l|r|r|r|r|r}
\hline
  & nasal & throat & aerosols & droplets & rows with any NA\\
\hline
not NA & 108 & 82 & 110 & 106 & 75\\
\hline
is NA & 6 & 32 & 4 & 8 & 39\\
\hline
\end{tabular}
\end{table}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{par}\NormalTok{(}\DataTypeTok{mfrow =} \KeywordTok{c}\NormalTok{(}\DecValTok{4}\NormalTok{,}\DecValTok{3}\NormalTok{))}
\ControlFlowTok{for}\NormalTok{ (c_name }\ControlFlowTok{in} \KeywordTok{colnames}\NormalTok{(maskdata))\{}
  \ControlFlowTok{if}\NormalTok{(}\KeywordTok{length}\NormalTok{(}\KeywordTok{unique}\NormalTok{(maskdata[,c_name])) }\OperatorTok{<=}\StringTok{ }\DecValTok{3}\NormalTok{)\{}
\NormalTok{     c_vals <-}\StringTok{ }\KeywordTok{names}\NormalTok{(}\KeywordTok{table}\NormalTok{(maskdata[,c_name]))}
\NormalTok{     c_counts <-}\StringTok{ }\KeywordTok{table}\NormalTok{(maskdata[,c_name])}
     \KeywordTok{barplot}\NormalTok{(}\DataTypeTok{names.arg =}\NormalTok{ c_vals,}\DataTypeTok{height=}\NormalTok{ c_counts, }\DataTypeTok{main =}\NormalTok{ c_name)}
\NormalTok{  \}}\ControlFlowTok{else}\NormalTok{ \{}
    \KeywordTok{hist}\NormalTok{(maskdata[,c_name], }\DataTypeTok{main =}\NormalTok{ c_name, }\DataTypeTok{xlab =}\NormalTok{ c_name)}
\NormalTok{  \}}
\NormalTok{\}}
\end{Highlighting}
\end{Shaded}

\includegraphics{hw06_solutions_files/figure-latex/unnamed-chunk-4-1.pdf}

It's highly encouraged to examine the data more deeply and understand how the variables relate. In the following EDA, we examine the interaction between variables, this helps us prepare for answering the questions of interesting in the later subsections.

The relationship between \texttt{allo.wm} and \texttt{mask.class}, as presented in Table \ref{tab:tab-all-vs-mask}, is very interesting, and digging into the data a bit more, it appears that even though individuals were allocated to a ``wear mask'' or ``don't wear mask'' class, but also had the opportunity to breath both with a mask and without a mask (in 2 seperate 30 minute breathing sessions). It's hard to rectify how an individual with \texttt{allo.wm\ =\ 1} (which means they were assigned to wear the mask), is listed as \texttt{mask.class\ =\ "ctrl"}. It's also unclear what the viral load \texttt{aerosol} and \texttt{droplets} data actually contains for individuals with \texttt{mask.class\ =\ "both"}. As such, we're going to use the \texttt{mask.class} feature to split of the groups (and ignore individuals that gave samples for both types as we are unclear what the data associated with that individual means). We also explored potentials for individuals with \texttt{mask.class\ =\ "both"} to have 2 rows (for both options), but that is not the case.

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# table(maskdata$allo.wm, maskdata$mask.class) }
\NormalTok{maskdata }\OperatorTok{%>%}\StringTok{ }
\StringTok{  }\KeywordTok{group_by}\NormalTok{(allo.wm, mask.class) }\OperatorTok{%>%}\StringTok{ }
\StringTok{  }\KeywordTok{summarize}\NormalTok{(}\DataTypeTok{total =} \KeywordTok{n}\NormalTok{()) }\OperatorTok{%>%}
\StringTok{  }\KeywordTok{pivot_wider}\NormalTok{(}\DataTypeTok{names_from =}\NormalTok{ mask.class, }\DataTypeTok{values_from =}\NormalTok{ total, }\DataTypeTok{values_fill =} \DecValTok{0}\NormalTok{) }\OperatorTok{%>%}
\StringTok{  }\NormalTok{knitr}\OperatorTok{::}\KeywordTok{kable}\NormalTok{(}\DataTypeTok{caption =} \StringTok{"Split of studies (year) and types of virus"}\NormalTok{) }\OperatorTok{%>%}
\StringTok{  }\KeywordTok{kable_styling}\NormalTok{() }\OperatorTok{%>%}\StringTok{ }
\StringTok{  }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DataTypeTok{bold =}\NormalTok{ T, }\DataTypeTok{border_right =}\NormalTok{ T)}
\end{Highlighting}
\end{Shaded}

\begin{table}

\caption{\label{tab:tab-all-vs-mask}Split of studies (year) and types of virus}
\centering
\begin{tabular}[t]{>{}r||r|r|r}
\hline
allo.wm & both & ctrl & mask\\
\hline
\textbf{0} & 13 & 42 & 0\\
\hline
\textbf{1} & 13 & 1 & 45\\
\hline
\end{tabular}
\end{table}

Table \ref{tab:tab-study-vs-virus} helps us see that that the study year and the type of virus isn't independent, we that only 1 individual in our data set in the \texttt{flu} study year had coronavirus.

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# table(maskdata$study, maskdata$virus) }
\NormalTok{maskdata }\OperatorTok{%>%}\StringTok{ }
\StringTok{  }\KeywordTok{filter}\NormalTok{(mask.class }\OperatorTok{!=}\StringTok{ "both"}\NormalTok{) }\OperatorTok{%>%}\StringTok{ }
\StringTok{  }\KeywordTok{group_by}\NormalTok{(study, virus) }\OperatorTok{%>%}\StringTok{ }
\StringTok{  }\KeywordTok{summarize}\NormalTok{(}\DataTypeTok{total =} \KeywordTok{n}\NormalTok{()) }\OperatorTok{%>%}
\StringTok{  }\KeywordTok{pivot_wider}\NormalTok{(}\DataTypeTok{names_from =}\NormalTok{ virus, }\DataTypeTok{values_from =}\NormalTok{ total, }\DataTypeTok{values_fill =} \DecValTok{0}\NormalTok{) }\OperatorTok{%>%}
\StringTok{  }\NormalTok{knitr}\OperatorTok{::}\KeywordTok{kable}\NormalTok{(}\DataTypeTok{caption =} \StringTok{"Allocated to wear a mask vs mask class recorded"}\NormalTok{) }\OperatorTok{%>%}
\StringTok{  }\KeywordTok{kable_styling}\NormalTok{() }\OperatorTok{%>%}\StringTok{ }
\StringTok{  }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DataTypeTok{bold =}\NormalTok{ T, }\DataTypeTok{border_right =}\NormalTok{ T)}
\end{Highlighting}
\end{Shaded}

\begin{table}

\caption{\label{tab:tab-study-vs-virus}Allocated to wear a mask vs mask class recorded}
\centering
\begin{tabular}[t]{>{}l||r|r|r}
\hline
study & cold & flu & hcov\\
\hline
\textbf{cold} & 36 & 17 & 12\\
\hline
\textbf{flu} & 4 & 18 & 1\\
\hline
\end{tabular}
\end{table}

Finally, we observed that the variable \texttt{breath.num} equals 2 if \texttt{mask.class\ =\ "both"} and otherwise it equals 1. Given this observation, we won't be using this variable due to the above discussion of \texttt{mask.class} vs \texttt{allo.wm}.

\hypertarget{part-b}{%
\subsection{Part b}\label{part-b}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{aerosols_base_lm <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(}\KeywordTok{log10}\NormalTok{(aerosols}\DecValTok{-1}\NormalTok{) }\OperatorTok{~}\StringTok{ }\NormalTok{mask.class, }
                       \DataTypeTok{data =}\NormalTok{ maskdata[maskdata}\OperatorTok{$}\NormalTok{mask.class }\OperatorTok{!=}\StringTok{ "both"}\NormalTok{,])}
\NormalTok{droplets_base_lm <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(}\KeywordTok{log10}\NormalTok{(droplets}\DecValTok{-1}\NormalTok{) }\OperatorTok{~}\StringTok{ }\NormalTok{mask.class, }
                       \DataTypeTok{data =}\NormalTok{ maskdata[maskdata}\OperatorTok{$}\NormalTok{mask.class }\OperatorTok{!=}\StringTok{ "both"}\NormalTok{,])}
\end{Highlighting}
\end{Shaded}

\hypertarget{aerosol-model}{%
\subsubsection{aerosol model}\label{aerosol-model}}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{summary}\NormalTok{(aerosols_base_lm)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = log10(aerosols - 1) ~ mask.class, data = maskdata[maskdata$mask.class != 
##     "both", ])
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.3540 -0.7275 -0.5311  0.8657  3.9225 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      1.3540     0.2218   6.104 3.28e-08 ***
## mask.classmask  -0.8229     0.3065  -2.685  0.00878 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.403 on 82 degrees of freedom
##   (4 observations deleted due to missingness)
## Multiple R-squared:  0.0808, Adjusted R-squared:  0.06959 
## F-statistic: 7.208 on 1 and 82 DF,  p-value: 0.008781
\end{verbatim}

The above model's coefficients suggest that wearing a mask reduces the percentage of viral load in aerosol released in 30 minutes of breathing by 82.29\%. This statistically significant result. For both of the models in this section, we are basically doing a 1 way ANOVA (with a single binary covariate), as such the diagnostic plots won't make that much sense. We decided to use \(log_{10}(\text{viral load}-1)\) as the viral load variables are highly skewed, and the associated paper we are comparing against also examined these loads in the log scale\footnote{Given that the minimum value (when no viral load was actually recorded) was 2, we did a \(log(x-1)\) transform so those values would be transformed to \(0\).}.

\hypertarget{droplets-model}{%
\subsubsection{droplets model}\label{droplets-model}}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{summary}\NormalTok{(droplets_base_lm)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = log10(droplets - 1) ~ mask.class, data = maskdata[maskdata$mask.class != 
##     "both", ])
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.6749 -0.6749 -0.2218 -0.2218  2.8435 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      0.6749     0.1449   4.659 1.23e-05 ***
## mask.classmask  -0.4531     0.2036  -2.225   0.0289 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9276 on 81 degrees of freedom
##   (5 observations deleted due to missingness)
## Multiple R-squared:  0.05761,    Adjusted R-squared:  0.04597 
## F-statistic: 4.951 on 1 and 81 DF,  p-value: 0.02885
\end{verbatim}

The above model's coefficients suggest that wearing a mask reduces the percentage of viral load in droplets released in 30 minutes of breathing buy 45.31\%. This statistically significant result.

\hypertarget{part-c}{%
\subsection{Part c}\label{part-c}}

In order to only make 2 models, but still be able to answer the question of the efficiency of masks conditional on virus type we build models of the type

\[
\log_{10}(\text{viral load}) \sim \text{virus} + \text{mask.class}:\text{virus} - 1 \;.
\]

This allows us to examine the efficiency of the masks per virus type using the interaction term.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{aerosols_interact_lm <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(}\KeywordTok{log10}\NormalTok{(aerosols }\OperatorTok{-}\StringTok{ }\DecValTok{1}\NormalTok{) }\OperatorTok{~}\StringTok{ }\NormalTok{virus }\OperatorTok{+}\StringTok{ }\NormalTok{mask.class}\OperatorTok{:}\NormalTok{virus }\OperatorTok{-}\StringTok{ }\DecValTok{1}\NormalTok{, }
                       \DataTypeTok{data =}\NormalTok{ maskdata[maskdata}\OperatorTok{$}\NormalTok{mask.class }\OperatorTok{!=}\StringTok{ "both"}\NormalTok{,])}

\NormalTok{droplets_interact_lm <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(}\KeywordTok{log10}\NormalTok{(droplets }\OperatorTok{-}\StringTok{ }\DecValTok{1}\NormalTok{) }\OperatorTok{~}\StringTok{ }\NormalTok{virus }\OperatorTok{+}\StringTok{ }\NormalTok{mask.class}\OperatorTok{:}\NormalTok{virus }\OperatorTok{-}\StringTok{ }\DecValTok{1}\NormalTok{, }
                       \DataTypeTok{data =}\NormalTok{ maskdata[maskdata}\OperatorTok{$}\NormalTok{mask.class }\OperatorTok{!=}\StringTok{ "both"}\NormalTok{,])}
\end{Highlighting}
\end{Shaded}

\hypertarget{aerosol-model-1}{%
\subsubsection{aerosol model}\label{aerosol-model-1}}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{summary}\NormalTok{(aerosols_interact_lm)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = log10(aerosols - 1) ~ virus + mask.class:virus - 
##     1, data = maskdata[maskdata$mask.class != "both", ])
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.6559 -0.8063 -0.5214  0.7607  4.2576 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)    
## viruscold                  1.5232     0.3242   4.698 1.11e-05 ***
## virusflu                   1.0188     0.3649   2.792  0.00658 ** 
## virushcov                  1.6559     0.5770   2.870  0.00528 ** 
## viruscold:mask.classmask  -0.7753     0.4649  -1.668  0.09936 .  
## virusflu:mask.classmask   -0.4974     0.4881  -1.019  0.31133    
## virushcov:mask.classmask  -1.6559     0.7863  -2.106  0.03843 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.413 on 78 degrees of freedom
##   (4 observations deleted due to missingness)
## Multiple R-squared:  0.3696, Adjusted R-squared:  0.3211 
## F-statistic: 7.622 on 6 and 78 DF,  p-value: 1.868e-06
\end{verbatim}

Interestingly, when we start focusing on the efficacy of masks conditional on the virus type, the evidence is a bit more murky. The above model's coefficients suggest that wearing a mask when you have a \textbf{cold} reduces the percentage of viral load in aerosol released in 30 minutes of breathing by 77.53\% (this is \textbf{not} statistically significant result). Similarly when you have the \textbf{flu}, wearning a mask reduces the precentage of viral load in aerosol released in 30 minutes of breathing by 49.74\% (this is also \textbf{not} statistically significant result). If you have a coronavirus, mask wearing is associated with a reduction of your viral load in aerosol released in 30 minutes by 165.59\% (this \textbf{is} a statistically significant result).

If we interpret the question as \emph{``Is the efficacy of masks different between the different virus types''}, then we'd be looking to see if \(\beta_\text{mask:cold} = \beta_\text{mask:flu} = \beta_\text{mask:coronavirus}\). To do this type of test we'd actually want to look at a slightly different model, namely

\[
\log_{10}(\text{viral load}) \sim \text{virus} +\text{mask.class} +\text{mask.class}:\text{virus} - 1 \;.
\]

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{aerosols_interact_lm2 <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(}\KeywordTok{log10}\NormalTok{(aerosols }\OperatorTok{-}\StringTok{ }\DecValTok{1}\NormalTok{) }\OperatorTok{~}\StringTok{ }\NormalTok{virus }\OperatorTok{+}\StringTok{ }\NormalTok{mask.class }\OperatorTok{+}\StringTok{ }
\StringTok{                              }\NormalTok{mask.class}\OperatorTok{:}\NormalTok{virus }\OperatorTok{-}\StringTok{ }\DecValTok{1}\NormalTok{,}
   \DataTypeTok{data =}\NormalTok{ maskdata[maskdata}\OperatorTok{$}\NormalTok{mask.class }\OperatorTok{!=}\StringTok{ "both"}\NormalTok{,])}
\end{Highlighting}
\end{Shaded}

We can then examine amount of variability explained by the interaction term to determine if the efficacy for masks is different across the virus types. In the following summary, we see that the associated p-value for this question is not significant (0.459), suggesting that we cannot conclude that efficacy of masks is different across virus strands.

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{anova}\NormalTok{(aerosols_interact_lm2)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Analysis of Variance Table
## 
## Response: log10(aerosols - 1)
##                  Df  Sum Sq Mean Sq F value    Pr(>F)    
## virus             3  74.851 24.9503 12.4917 9.404e-07 ***
## mask.class        1  13.349 13.3489  6.6833    0.0116 *  
## virus:mask.class  2   3.140  1.5698  0.7859    0.4593    
## Residuals        78 155.794  1.9974                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

\hypertarget{droplets-model-1}{%
\subsubsection{droplets model}\label{droplets-model-1}}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{summary}\NormalTok{(droplets_interact_lm)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = log10(droplets - 1) ~ virus + mask.class:virus - 
##     1, data = maskdata[maskdata$mask.class != "both", ])
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.7419 -0.5850  0.0000  0.0000  2.9334 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)    
## viruscold                  0.7223     0.2072   3.485 0.000815 ***
## virusflu                   0.5850     0.2393   2.445 0.016789 *  
## virushcov                  0.7419     0.3784   1.961 0.053529 .  
## viruscold:mask.classmask  -0.1401     0.3109  -0.451 0.653467    
## virusflu:mask.classmask   -0.5850     0.3201  -1.827 0.071510 .  
## virushcov:mask.classmask  -0.7419     0.5156  -1.439 0.154269    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9268 on 77 degrees of freedom
##   (5 observations deleted due to missingness)
## Multiple R-squared:  0.2686, Adjusted R-squared:  0.2116 
## F-statistic: 4.714 on 6 and 77 DF,  p-value: 0.0003893
\end{verbatim}

A similar translation of this summary table can be made. We leave this to the reader.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{droplets_interact_lm2 <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(}\KeywordTok{log10}\NormalTok{(droplets }\OperatorTok{-}\StringTok{ }\DecValTok{1}\NormalTok{) }\OperatorTok{~}\StringTok{ }\NormalTok{virus }\OperatorTok{+}\StringTok{ }\NormalTok{mask.class }\OperatorTok{+}\StringTok{ }
\StringTok{                              }\NormalTok{mask.class}\OperatorTok{:}\NormalTok{virus }\OperatorTok{-}\StringTok{ }\DecValTok{1}\NormalTok{,}
   \DataTypeTok{data =}\NormalTok{ maskdata[maskdata}\OperatorTok{$}\NormalTok{mask.class }\OperatorTok{!=}\StringTok{ "both"}\NormalTok{,])}
\KeywordTok{anova}\NormalTok{(droplets_interact_lm2)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Analysis of Variance Table
## 
## Response: log10(droplets - 1)
##                  Df Sum Sq Mean Sq F value    Pr(>F)    
## virus             3 19.472  6.4906  7.5563 0.0001701 ***
## mask.class        1  3.562  3.5620  4.1469 0.0451483 *  
## virus:mask.class  2  1.259  0.6295  0.7329 0.4838491    
## Residuals        77 66.140  0.8590                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

\hypertarget{part-d}{%
\subsection{Part d}\label{part-d}}

As mentioned in the EDA in part a, we won't include \# of breaths as it is only 2 when with \texttt{mask.class\ ==\ "both"} (else it is 1), and we're not looking at the data where \texttt{mask.class\ ==\ "both"}.

Our model focuses on inference (focusing on potential collaborators' interest in the best estimate of the impact of masks on reducing the viral load across these populations). We include \texttt{age} and \texttt{female} features to account for demographic covariates that might impact an individual's production viral load production (which interestingly seems to have significant coefficients). We include also include nasal and throat recorded viral load, as it is natural to assume that an individual's viral load produced by breathing is associated with the amount of viral load found in the nose and throat. And finally, we include \texttt{study} to account for any measurement / experiment differences across the studies.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{aerosols_interact_lm <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(}\KeywordTok{log10}\NormalTok{(aerosols) }\OperatorTok{~}\StringTok{ }\NormalTok{virus }\OperatorTok{+}\StringTok{ }\NormalTok{mask.class}\OperatorTok{:}\NormalTok{virus }\OperatorTok{+}\StringTok{ }\NormalTok{age }\OperatorTok{+}\StringTok{ }
\StringTok{                             }\NormalTok{female }\OperatorTok{+}\StringTok{ }\KeywordTok{log10}\NormalTok{(nasal) }\OperatorTok{+}\StringTok{ }\KeywordTok{log10}\NormalTok{(throat) }\OperatorTok{+}\StringTok{ }\NormalTok{study }\OperatorTok{-}\StringTok{ }\DecValTok{1}\NormalTok{, }
                       \DataTypeTok{data =}\NormalTok{ maskdata[maskdata}\OperatorTok{$}\NormalTok{mask.class }\OperatorTok{!=}\StringTok{ "both"}\NormalTok{,])}

\KeywordTok{summary}\NormalTok{(aerosols_interact_lm)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = log10(aerosols) ~ virus + mask.class:virus + age + 
##     female + log10(nasal) + log10(throat) + study - 1, data = maskdata[maskdata$mask.class != 
##     "both", ])
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.0577 -0.8504 -0.1688  0.6667  2.7485 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)   
## viruscold                -0.05285    0.99063  -0.053  0.95767   
## virusflu                 -0.83462    1.11837  -0.746  0.45892   
## virushcov                -0.14371    1.34366  -0.107  0.91524   
## age                       0.02424    0.01130   2.146  0.03666 * 
## female                   -1.13966    0.33913  -3.361  0.00148 **
## log10(nasal)              0.16240    0.15278   1.063  0.29283   
## log10(throat)             0.19730    0.13021   1.515  0.13589   
## studyflu                 -0.07756    0.40246  -0.193  0.84794   
## viruscold:mask.classmask -1.18717    0.51245  -2.317  0.02458 * 
## virusflu:mask.classmask  -0.36244    0.50164  -0.723  0.47327   
## virushcov:mask.classmask -2.11400    0.85098  -2.484  0.01631 * 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.249 on 51 degrees of freedom
##   (26 observations deleted due to missingness)
## Multiple R-squared:  0.6384, Adjusted R-squared:  0.5604 
## F-statistic: 8.185 on 11 and 51 DF,  p-value: 5.121e-08
\end{verbatim}

Under this model, it appears that we can reject hypothesis that masks don't reduce the amount of viral load for those with a cold or a flu (and the estimated impact is 118.72 and 211.4\% respectively - aka pretty high.

Our data has a lot of left censured variables (basically all the viral load recordings), and the diagnostic plots in Figures \ref{fig:fig-left-censured-aerosol} and \ref{fig:fig-diagostics-aerosol} still highlight such things. We also examined added variable plots to understanding if additional patterns between variables and residuals where present but didn't find any. VIF analysis (when accounting for subspace structure in the data for the categorical variables) the generalized VIF values are not too worrisome related to the inflation of standard errors for the \(\beta\) values. Additionally, given the small size of the data set, and the nature of the variables there wasn't too much imagined could be done / variable that could be removed in the situation.

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{plot}\NormalTok{(}\DataTypeTok{x =}\NormalTok{ aerosols_interact_lm}\OperatorTok{$}\NormalTok{fitted.values,}
     \DataTypeTok{y =}\NormalTok{ (aerosols_interact_lm}\OperatorTok{$}\NormalTok{residuals }\OperatorTok{+}\StringTok{ }\NormalTok{aerosols_interact_lm}\OperatorTok{$}\NormalTok{fitted.values),}
     \DataTypeTok{ylab =} \StringTok{"log10(aerosol viral load)"}\NormalTok{,}
     \DataTypeTok{xlab =} \StringTok{"estimated log10(aerosol viral load)"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{hw06_solutions_files/figure-latex/fig-left-censured-aerosol-1.pdf}
\caption{\label{fig:fig-left-censured-aerosol}True vs Estimated log10(aerosol viral load).}
\end{figure}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{par}\NormalTok{(}\DataTypeTok{mfrow =} \KeywordTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{,}\DecValTok{2}\NormalTok{))}
\KeywordTok{plot}\NormalTok{(aerosols_interact_lm)}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{hw06_solutions_files/figure-latex/fig-diagostics-aerosol-1.pdf}
\caption{\label{fig:fig-diagostics-aerosol}Diagnostics for final aerosol viral load model.}
\end{figure}

\hypertarget{problem-2}{%
\section{Problem 2}\label{problem-2}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{beauty <-}\StringTok{ }\KeywordTok{read_csv}\NormalTok{(}\StringTok{"../ProfEvaltnsBeautyPublic.csv"}\NormalTok{) }\OperatorTok{%>%}
\StringTok{  }\NormalTok{dplyr}\OperatorTok{::}\KeywordTok{select}\NormalTok{(}\OperatorTok{-}\NormalTok{profevaluation, }\OperatorTok{-}\NormalTok{profnumber, }\OperatorTok{-}\NormalTok{multipleclass, }
                \OperatorTok{-}\KeywordTok{starts_with}\NormalTok{(}\StringTok{"class"}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

\hypertarget{part-a-1}{%
\subsection{Part a}\label{part-a-1}}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(leaps)}
\NormalTok{all_subsets <-}\StringTok{ }\KeywordTok{regsubsets}\NormalTok{(courseevaluation }\OperatorTok{~}\NormalTok{.,}
                          \DataTypeTok{data =}\NormalTok{ beauty,}
                          \DataTypeTok{nvmax =} \DecValTok{10}\NormalTok{, }\DataTypeTok{intercept =}\NormalTok{ T,}
                          \DataTypeTok{method =} \StringTok{"exhaustive"}\NormalTok{)}

\NormalTok{summ <-}\StringTok{ }\KeywordTok{summary}\NormalTok{(all_subsets)}

\NormalTok{best_model_index <-}\StringTok{ }\KeywordTok{which.min}\NormalTok{(summ}\OperatorTok{$}\NormalTok{bic)}

\NormalTok{dropped <-}\StringTok{ }\NormalTok{summ}\OperatorTok{$}\NormalTok{outmat }\OperatorTok{%>%}\StringTok{ }\KeywordTok{apply}\NormalTok{(}\DecValTok{2}\NormalTok{, }\ControlFlowTok{function}\NormalTok{(col) \{}\KeywordTok{mean}\NormalTok{(col }\OperatorTok{==}\StringTok{ " "}\NormalTok{) }\OperatorTok{==}\StringTok{ }\DecValTok{1}\NormalTok{\})}
\NormalTok{all_subset_info <-}\StringTok{ }\KeywordTok{cbind}\NormalTok{(summ}\OperatorTok{$}\NormalTok{outmat[,}\OperatorTok{!}\NormalTok{dropped], }\DataTypeTok{BIC =}\NormalTok{ summ}\OperatorTok{$}\NormalTok{bic) }
\KeywordTok{rownames}\NormalTok{(all_subset_info) <-}\StringTok{ }\DecValTok{1}\OperatorTok{:}\KeywordTok{nrow}\NormalTok{(all_subset_info)}
\NormalTok{all_subset_info }\OperatorTok{%>%}\StringTok{ }\NormalTok{data.frame }\OperatorTok{%>%}
\StringTok{  }\KeywordTok{rownames_to_column}\NormalTok{(}\DataTypeTok{var =} \StringTok{"num variables"}\NormalTok{) }\OperatorTok{%>%}
\StringTok{  }\NormalTok{knitr}\OperatorTok{::}\KeywordTok{kable}\NormalTok{() }\OperatorTok{%>%}\StringTok{ }
\StringTok{  }\NormalTok{kableExtra}\OperatorTok{::}\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{latex_options =} \StringTok{"scale_down"}\NormalTok{) }\OperatorTok{%>%}
\StringTok{  }\KeywordTok{row_spec}\NormalTok{(best_model_index, }\DataTypeTok{background =} \StringTok{"gray"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{table}[H]
\centering
\resizebox{\linewidth}{!}{
\begin{tabular}{l|l|l|l|l|l|l|l|l|l|l|l|l}
\hline
num variables & minority & beautyf2upper & beautyfupperdiv & btystdave & btystdf2u & btystdfu & female & nonenglish & onecredit & percentevaluating & blkandwhite & BIC\\
\hline
1 &  &  &  &  &  &  &  &  & * &  &  & -14.0820700135937\\
\hline
2 &  &  &  & * &  &  &  &  & * &  &  & -30.0187608382119\\
\hline
3 &  &  & * &  &  &  & * &  & * &  &  & -42.0422726206511\\
\hline
4 &  &  & * &  &  &  & * &  & * & * &  & -52.6352707419912\\
\hline
5 & * &  & * &  &  &  & * &  & * & * &  & -61.8604866501093\\
\hline
\cellcolor{gray}{6} & \cellcolor{gray}{} & \cellcolor{gray}{} & \cellcolor{gray}{*} & \cellcolor{gray}{} & \cellcolor{gray}{} & \cellcolor{gray}{} & \cellcolor{gray}{*} & \cellcolor{gray}{*} & \cellcolor{gray}{*} & \cellcolor{gray}{*} & \cellcolor{gray}{*} & \cellcolor{gray}{-68.7273809819797}\\
\hline
7 & * &  & * &  &  &  & * & * & * & * & * & -68.168278525645\\
\hline
8 &  & * & * &  & * &  & * & * & * & * & * & -67.592998887792\\
\hline
9 & * & * & * &  & * &  & * & * & * & * & * & -67.4303455606005\\
\hline
10 & * & * & * &  & * & * & * & * & * & * & * & -64.9400133420853\\
\hline
\end{tabular}}
\end{table}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{var_names <-}\StringTok{ }\KeywordTok{colnames}\NormalTok{(all_subset_info)[all_subset_info[best_model_index,] }\OperatorTok{==}\StringTok{ "*"}\NormalTok{]}

\NormalTok{all_subsets_model <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(}\KeywordTok{as.formula}\NormalTok{(}\KeywordTok{paste}\NormalTok{(}\StringTok{"courseevaluation ~"}\NormalTok{,}
                                         \KeywordTok{paste0}\NormalTok{(var_names, }\DataTypeTok{collapse =} \StringTok{" + "}\NormalTok{))),}
                        \DataTypeTok{data =}\NormalTok{ beauty)}

\KeywordTok{summary}\NormalTok{(all_subsets_model)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = as.formula(paste("courseevaluation ~", paste0(var_names, 
##     collapse = " + "))), data = beauty)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.97607 -0.30533  0.04791  0.37959  1.04118 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        3.355273   0.113922  29.452  < 2e-16 ***
## beautyfupperdiv    0.058282   0.012559   4.641 4.54e-06 ***
## female            -0.254260   0.048290  -5.265 2.16e-07 ***
## nonenglish        -0.388186   0.097125  -3.997 7.49e-05 ***
## onecredit          0.500846   0.099146   5.052 6.35e-07 ***
## percentevaluating  0.005502   0.001415   3.889 0.000115 ***
## blkandwhite        0.251170   0.063629   3.947 9.15e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.495 on 456 degrees of freedom
## Multiple R-squared:  0.2143, Adjusted R-squared:  0.204 
## F-statistic: 20.73 on 6 and 456 DF,  p-value: < 2.2e-16
\end{verbatim}

\hypertarget{part-b-1}{%
\subsection{Part b}\label{part-b-1}}

We'll show forward regression.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{forward_step <-}\StringTok{ }\KeywordTok{step}\NormalTok{(}\KeywordTok{lm}\NormalTok{(courseevaluation }\OperatorTok{~}\DecValTok{1}\NormalTok{, }\DataTypeTok{data =}\NormalTok{ beauty),}
  \DataTypeTok{scope  =} \KeywordTok{as.formula}\NormalTok{(}\KeywordTok{paste}\NormalTok{(}\StringTok{"courseevaluation ~"}\NormalTok{,}
                      \KeywordTok{paste0}\NormalTok{(}\KeywordTok{names}\NormalTok{(beauty)[}\KeywordTok{names}\NormalTok{(beauty) }\OperatorTok{!=}\StringTok{ "courseevaluation"}\NormalTok{], }
                             \DataTypeTok{collapse =} \StringTok{" + "}\NormalTok{))),}
                          \DataTypeTok{direction =} \StringTok{"forward"}\NormalTok{, }\DataTypeTok{trace =} \DecValTok{0}\NormalTok{)}

\KeywordTok{summary}\NormalTok{(forward_step)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = courseevaluation ~ onecredit + btystdave + percentevaluating + 
##     female + minority + blkandwhite + nonenglish + beautyfupperdiv + 
##     btystdfu + btystdf2u + tenuretrack + formal + age, data = beauty)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.86588 -0.29589  0.04495  0.33963  1.06133 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       -0.836838   1.639295  -0.510 0.609962    
## onecredit          0.503749   0.107838   4.671 3.96e-06 ***
## btystdave         -0.167839   0.075372  -2.227 0.026456 *  
## percentevaluating  0.004850   0.001451   3.342 0.000902 ***
## female            -0.302259   0.052308  -5.778 1.41e-08 ***
## minority          -0.155301   0.074788  -2.077 0.038412 *  
## blkandwhite        0.285158   0.069381   4.110 4.70e-05 ***
## nonenglish        -0.381223   0.104535  -3.647 0.000297 ***
## beautyfupperdiv    0.939194   0.323894   2.900 0.003918 ** 
## btystdfu          -1.770340   0.656800  -2.695 0.007294 ** 
## btystdf2u          0.092159   0.042790   2.154 0.031790 *  
## tenuretrack       -0.132773   0.062020  -2.141 0.032828 *  
## formal             0.143150   0.069643   2.055 0.040410 *  
## age               -0.004442   0.002837  -1.566 0.118145    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4867 on 449 degrees of freedom
## Multiple R-squared:  0.2523, Adjusted R-squared:  0.2306 
## F-statistic: 11.65 on 13 and 449 DF,  p-value: < 2.2e-16
\end{verbatim}

\hypertarget{part-c-1}{%
\subsection{Part c}\label{part-c-1}}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(glmnet)}
\NormalTok{cv_lasso_selection <-}\StringTok{ }\KeywordTok{cv.glmnet}\NormalTok{(}\DataTypeTok{x =} \KeywordTok{as.matrix}\NormalTok{(beauty[,}\KeywordTok{names}\NormalTok{(beauty) }\OperatorTok{!=}\StringTok{ "courseevaluation"}\NormalTok{]),}
          \DataTypeTok{y =}\NormalTok{ beauty}\OperatorTok{$}\NormalTok{courseevaluation, }\DataTypeTok{alpha =} \DecValTok{1}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{lasso <-}\StringTok{ }\KeywordTok{glmnet}\NormalTok{(}\DataTypeTok{x =} \KeywordTok{as.matrix}\NormalTok{(beauty[,}\KeywordTok{names}\NormalTok{(beauty) }\OperatorTok{!=}\StringTok{ "courseevaluation"}\NormalTok{]),}
                \DataTypeTok{y =}\NormalTok{ beauty}\OperatorTok{$}\NormalTok{courseevaluation,}
                \DataTypeTok{alpha =} \DecValTok{1}\NormalTok{, }\DataTypeTok{lambda =}\NormalTok{ cv_lasso_selection}\OperatorTok{$}\NormalTok{lambda}\FloatTok{.1}\NormalTok{se)}


\NormalTok{coef_lasso <-}\StringTok{ }\KeywordTok{as.matrix}\NormalTok{(}\KeywordTok{coef}\NormalTok{(lasso))}
\NormalTok{coef_lasso_df <-}\StringTok{ }\KeywordTok{data.frame}\NormalTok{(}\DataTypeTok{columns =} \KeywordTok{names}\NormalTok{(coef_lasso[coef_lasso[,}\DecValTok{1}\NormalTok{] }\OperatorTok{!=}\StringTok{ }\DecValTok{0}\NormalTok{,]),}
         \DataTypeTok{beta =}\NormalTok{ coef_lasso[coef_lasso[,}\DecValTok{1}\NormalTok{] }\OperatorTok{!=}\StringTok{ }\DecValTok{0}\NormalTok{,]) }
\KeywordTok{rownames}\NormalTok{(coef_lasso_df) <-}\StringTok{ }\OtherTok{NULL}

\NormalTok{coef_lasso_df}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##              columns          beta
## 1        (Intercept)  3.558843e+00
## 2           minority -3.562730e-02
## 3      beautyf2upper  3.663638e-03
## 4    beautyfupperdiv  2.828790e-02
## 5          btystdf2u  8.129368e-06
## 6             female -1.061187e-01
## 7           fulldept  7.330144e-02
## 8         nonenglish -1.531938e-01
## 9          onecredit  3.355938e-01
## 10 percentevaluating  3.137356e-03
## 11       blkandwhite  1.104157e-01
\end{verbatim}

\hypertarget{part-d-1}{%
\subsection{Part d}\label{part-d-1}}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{#basically coding up a document term matrix function for some reason}

\NormalTok{dtm_coef <-}\StringTok{ }\ControlFlowTok{function}\NormalTok{(names_list)\{}
\NormalTok{  all_words <-}\StringTok{ }\KeywordTok{unique}\NormalTok{(}\KeywordTok{unlist}\NormalTok{(names_list))}
\NormalTok{  matrix_info <-}\StringTok{ }\KeywordTok{matrix}\NormalTok{(}\DataTypeTok{nrow =} \KeywordTok{length}\NormalTok{(names_list), }\DataTypeTok{ncol =} \KeywordTok{length}\NormalTok{(all_words))}
  
\NormalTok{  r_idx <-}\StringTok{ }\DecValTok{1}
  \ControlFlowTok{for}\NormalTok{ (col_names }\ControlFlowTok{in}\NormalTok{ names_list)\{}
\NormalTok{    matrix_info[r_idx,] <-}\StringTok{ }\NormalTok{all_words }\OperatorTok{%in%}\StringTok{ }\NormalTok{col_names}
\NormalTok{    r_idx <-}\StringTok{ }\NormalTok{r_idx }\OperatorTok{+}\StringTok{ }\DecValTok{1}
\NormalTok{  \}}
  
  \KeywordTok{colnames}\NormalTok{(matrix_info) <-}\StringTok{ }\NormalTok{all_words}
  \KeywordTok{rownames}\NormalTok{(matrix_info) <-}\StringTok{ }\KeywordTok{names}\NormalTok{(names_list)}
  
  \KeywordTok{return}\NormalTok{(matrix_info)}
\NormalTok{\}}

\NormalTok{variables_in_model <-}\StringTok{ }\KeywordTok{list}\NormalTok{(}
  \StringTok{"best subset"}\NormalTok{ =}\StringTok{ }\KeywordTok{names}\NormalTok{(}\KeywordTok{coef}\NormalTok{(all_subsets_model)),}
  \StringTok{"lasso"}\NormalTok{ =}\StringTok{ }\NormalTok{coef_lasso_df}\OperatorTok{$}\NormalTok{columns,}
  \StringTok{"forward stepwise"}\NormalTok{ =}\StringTok{ }\KeywordTok{names}\NormalTok{(forward_step}\OperatorTok{$}\NormalTok{coefficients),}
  \StringTok{"homework 05, 2c"}\NormalTok{ =}\StringTok{ }\KeywordTok{c}\NormalTok{(}\KeywordTok{names}\NormalTok{(beauty[,}\KeywordTok{names}\NormalTok{(beauty) }\OperatorTok{!=}\StringTok{ "courseevaluation"}\NormalTok{]), }
                        \StringTok{"profevaluation"}\NormalTok{, }\StringTok{"(Intercept)"}\NormalTok{)}
\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{t}\NormalTok{(}\KeywordTok{dtm_coef}\NormalTok{(variables_in_model)) }\OperatorTok{%>%}
\StringTok{  }\KeywordTok{data.frame}\NormalTok{(}\DataTypeTok{check.names =}\NormalTok{ F) }\OperatorTok{%>%}\StringTok{ }
\StringTok{  }\KeywordTok{rownames_to_column}\NormalTok{(}\DataTypeTok{var =} \StringTok{"features"}\NormalTok{) }\OperatorTok{%>%}
\StringTok{  }\KeywordTok{mutate}\NormalTok{(}\StringTok{`}\DataTypeTok{best subset}\StringTok{`}\NormalTok{ =}\StringTok{ }\KeywordTok{ifelse}\NormalTok{(}\StringTok{`}\DataTypeTok{best subset}\StringTok{`}\NormalTok{,  }
                                \KeywordTok{cell_spec}\NormalTok{(}\StringTok{`}\DataTypeTok{best subset}\StringTok{`}\NormalTok{, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{color =} \StringTok{"black"}\NormalTok{),}
                                \KeywordTok{cell_spec}\NormalTok{(}\StringTok{`}\DataTypeTok{best subset}\StringTok{`}\NormalTok{, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{color =} \StringTok{"red"}\NormalTok{)),}
         \StringTok{`}\DataTypeTok{lasso}\StringTok{`}\NormalTok{ =}\StringTok{ }\KeywordTok{ifelse}\NormalTok{(}\StringTok{`}\DataTypeTok{lasso}\StringTok{`}\NormalTok{,  }
                                \KeywordTok{cell_spec}\NormalTok{(}\StringTok{`}\DataTypeTok{lasso}\StringTok{`}\NormalTok{, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{color =} \StringTok{"black"}\NormalTok{),}
                                \KeywordTok{cell_spec}\NormalTok{(}\StringTok{`}\DataTypeTok{lasso}\StringTok{`}\NormalTok{, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{color =} \StringTok{"red"}\NormalTok{)),}
         \StringTok{`}\DataTypeTok{forward stepwise}\StringTok{`}\NormalTok{ =}\StringTok{ }\KeywordTok{ifelse}\NormalTok{(}\StringTok{`}\DataTypeTok{forward stepwise}\StringTok{`}\NormalTok{,  }
                                \KeywordTok{cell_spec}\NormalTok{(}\StringTok{`}\DataTypeTok{forward stepwise}\StringTok{`}\NormalTok{, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{color =} \StringTok{"black"}\NormalTok{),}
                                \KeywordTok{cell_spec}\NormalTok{(}\StringTok{`}\DataTypeTok{forward stepwise}\StringTok{`}\NormalTok{, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{color =} \StringTok{"red"}\NormalTok{)),}
         \StringTok{`}\DataTypeTok{homework 05, 2c}\StringTok{`}\NormalTok{ =}\StringTok{ }\KeywordTok{ifelse}\NormalTok{(}\StringTok{`}\DataTypeTok{homework 05, 2c}\StringTok{`}\NormalTok{,  }
                                \KeywordTok{cell_spec}\NormalTok{(}\StringTok{`}\DataTypeTok{homework 05, 2c}\StringTok{`}\NormalTok{, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{color =} \StringTok{"black"}\NormalTok{),}
                                \KeywordTok{cell_spec}\NormalTok{(}\StringTok{`}\DataTypeTok{homework 05, 2c}\StringTok{`}\NormalTok{, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{color =} \StringTok{"red"}\NormalTok{))) }\OperatorTok{%>%}
\StringTok{  }\NormalTok{knitr}\OperatorTok{::}\KeywordTok{kable}\NormalTok{(}\DataTypeTok{format =} \StringTok{"latex"}\NormalTok{, }\DataTypeTok{caption =} \StringTok{"Features include in each model."}\NormalTok{,}
               \DataTypeTok{escape =}\NormalTok{ F) }\OperatorTok{%>%}
\StringTok{  }\NormalTok{kableExtra}\OperatorTok{::}\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{latex_options =} \KeywordTok{c}\NormalTok{(}\StringTok{"scale_down"}\NormalTok{),)}
\end{Highlighting}
\end{Shaded}

\begin{table}

\caption{\label{tab:tab-feature}Features include in each model.}
\centering
\resizebox{\linewidth}{!}{
\begin{tabular}[t]{l|l|l|l|l}
\hline
features & best subset & lasso & forward stepwise & homework 05, 2c\\
\hline
(Intercept) & \textcolor{black}{TRUE} & \textcolor{black}{TRUE} & \textcolor{black}{TRUE} & \textcolor{black}{TRUE}\\
\hline
beautyfupperdiv & \textcolor{black}{TRUE} & \textcolor{black}{TRUE} & \textcolor{black}{TRUE} & \textcolor{black}{TRUE}\\
\hline
female & \textcolor{black}{TRUE} & \textcolor{black}{TRUE} & \textcolor{black}{TRUE} & \textcolor{black}{TRUE}\\
\hline
nonenglish & \textcolor{black}{TRUE} & \textcolor{black}{TRUE} & \textcolor{black}{TRUE} & \textcolor{black}{TRUE}\\
\hline
onecredit & \textcolor{black}{TRUE} & \textcolor{black}{TRUE} & \textcolor{black}{TRUE} & \textcolor{black}{TRUE}\\
\hline
percentevaluating & \textcolor{black}{TRUE} & \textcolor{black}{TRUE} & \textcolor{black}{TRUE} & \textcolor{black}{TRUE}\\
\hline
blkandwhite & \textcolor{black}{TRUE} & \textcolor{black}{TRUE} & \textcolor{black}{TRUE} & \textcolor{black}{TRUE}\\
\hline
minority & \textcolor{red}{FALSE} & \textcolor{black}{TRUE} & \textcolor{black}{TRUE} & \textcolor{black}{TRUE}\\
\hline
beautyf2upper & \textcolor{red}{FALSE} & \textcolor{black}{TRUE} & \textcolor{red}{FALSE} & \textcolor{black}{TRUE}\\
\hline
btystdf2u & \textcolor{red}{FALSE} & \textcolor{black}{TRUE} & \textcolor{black}{TRUE} & \textcolor{black}{TRUE}\\
\hline
fulldept & \textcolor{red}{FALSE} & \textcolor{black}{TRUE} & \textcolor{red}{FALSE} & \textcolor{black}{TRUE}\\
\hline
btystdave & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{black}{TRUE} & \textcolor{black}{TRUE}\\
\hline
btystdfu & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{black}{TRUE} & \textcolor{black}{TRUE}\\
\hline
tenuretrack & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{black}{TRUE} & \textcolor{black}{TRUE}\\
\hline
formal & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{black}{TRUE} & \textcolor{black}{TRUE}\\
\hline
age & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{black}{TRUE} & \textcolor{black}{TRUE}\\
\hline
tenured & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{black}{TRUE}\\
\hline
beautyflowerdiv & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{black}{TRUE}\\
\hline
beautym2upper & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{black}{TRUE}\\
\hline
beautymlowerdiv & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{black}{TRUE}\\
\hline
beautymupperdiv & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{black}{TRUE}\\
\hline
btystdfl & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{black}{TRUE}\\
\hline
btystdm2u & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{black}{TRUE}\\
\hline
btystdml & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{black}{TRUE}\\
\hline
btystdmu & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{black}{TRUE}\\
\hline
didevaluation & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{black}{TRUE}\\
\hline
lower & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{black}{TRUE}\\
\hline
students & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{black}{TRUE}\\
\hline
btystdvariance & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{black}{TRUE}\\
\hline
btystdavepos & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{black}{TRUE}\\
\hline
btystdaveneg & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{black}{TRUE}\\
\hline
profevaluation & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{red}{FALSE} & \textcolor{black}{TRUE}\\
\hline
\end{tabular}}
\end{table}

The number of features (excluding the intercept) in our models increase from 6 in the best subset model, lasso with 10, forward stepwise regression selecting 14 and our full model with 30 features in homework 05. If we take out the lasso model these models are nested inside each other. \textbf{It's hard to evaluate prediction models as we left nothing to evaluate them on.} We observe that the lasso had actually higher training mse than the best subset model - but that's not to crazy because the penalty means that we're get more training error allowed.

\end{document}
