\documentstyle[bj,txfonts]{article}

\frenchspacing
\def\vtrim{\vspace*{-0.8em}}

\begin{document}

\begin{center}
\Large
36-617: Applied Linear Models\\
Fall 2020\\
HW08 -- Due Mon Nov 2, 11:59pm
\end{center}

\begin{itemize}

\item Please turn the homework in to Gradescope using the appropriate link in our course webspace at canvas.cmu.edu, 
under Assignments. 

\item Next week we will discuss causal inference (causal-inference.pdf) and hopefully begin discussing generalized least squares (Sheather Ch 9).  There will be a quiz on Monday, but it will only cover material on causal inference. 

% \item Please read Sheather Ch 9 for next Monday (there will be a reading quiz!).  The method of generalized least squares in Ch 9 contains the method of weighted least squares (Ch 4, which we did not read) as a special case.

\end{itemize}


\section*{Exercises}

\begin{enumerate}% \itemsep=0pt\parsep=0pt\parskip=3pt

% \item List the people you worked with on this assignment.

\item The {\tt warpbreaks} data set included in R gives the results of an experiment to determine the the effect of wool type (A or B) and tension (low, medium or high) on the number of warp breaks per loom. Data was collected for nine looms for each combination of settings. You can get more information on this data set from {\tt help(warpbreaks)}.  Use {\tt View(warpbreaks)} and \verb|xtabs(breaks ~ wool + tension, data=warpbreaks)| to familiarize yourself with the data.  (You don't have to turn in any of this initial exploration.)
\begin{enumerate}
\item Fit the two Poisson regression models \verb|breaks ~ wool + tension| and \verb|breaks ~ wool * tension| (dont't forget {\tt family=poisson}!).  Consider {\tt summary()}'s, {\tt plot()}'s of the fitted glm's, {\tt mmplot()}'s and also {\tt binnedplot()}'s (from {\tt library(arm)}) and/or plots from {\tt DHARMa}, and any other plots you think are useful.  Comment on the fits of each model, using the graphical evidence you have obtained {\em (if you think some plot(s) are \ul{\bf not} useful for this, please tell which ones \& why)}.

\item We would like to know whether the interaction between {\tt wool} and {\tt tension} is needed.  
\begin{enumerate}
\item Compare AIC's for the two models, and then compare BIC's for the two models.  Do AIC and BIC choose the same model? 
\item Conduct a likelihood ratio test for including the interaction. Does the LR test choose the same model as either AIC or BIC? {\em (Hint/recipe: For the likelihood ratio test, use the {\tt logLik()} function to obtain the log-likelihood of the two models,  compute $-2$ times the difference between the null and alternative model log-liklihoods, and obtain a $p$-value from the tail of an approporiate $\chi^2$ distribution.)} 
\end{enumerate}
\item For the best model from part (b), we would like to know if overdispersion is a problem. Use the Pearson residuals to conduct a test of overdispersion (hint: follow the recipe from lecture \#17.1).  Does this agree with the conclusion of {\tt testDispersion()} from {\tt library(DHARMa)}?   
\item Refit the model from part (c) using {\tt family=quasipoisson}, and note any differences in overdispersion parameter, coefficient estimates, SE's, etc.  Are there any differences in which predictors are significant?

\end{enumerate}

\vspace*{\fill}

\begin{center}
{\sl [More exercises on the next page]}
\end{center}

\clearpage

\item{} [Based on some exercises in Gelman \& Hill.] The data set {\tt nes5200\_{}processed\_{}voters\_{}realideo.dta} has data from the National Election Survey in every congressional election year in the US from 1948 to 2002.  You can read it into R using the function {\tt read.dta()} from {\tt library(foreign)}.  We are interested in learning whether some of the survey data is useful in predicting respondents' vote for president.

\begin{enumerate}

\item\label{extract} Extract from the full NES data set above a data frame with just data from the presidential election year 1976, and the following variables: {\tt ideo7, black, female, rep\_{}pres\_{}intent, presapprov, age, educ1, urban, income, union} and {\tt perfin1}.  The variable {\tt rep\_{}pres\_{}intent} is 1 if the respondent intended to vote for the Republican candidate (Gerald Ford, who assumed the presidency when Richard Nixon resigned amid an impeachment investigation), and 0 if the respondent intended to vote for the Democratic candidate (Jimmy Carter).  Familiarize yourself with all of the variables by using {\tt summary()} on your 1976 data frame\footnote{A command like {\tt attr(nes\_{}data,"var.labels")} on the full data set will give you brief definitions of all of the variables.} to look at the categories. 
You don't have to turn anything in for this, but you should do it.

\item\label{reg1} Fit a logistic regression predicting {\tt rep\_{}pres\_{}intent} from the other variables, for 1976.  Assess the fit using appropriate graphical methods.  Interpret the results: which variables seem to matter for predicting voters' preference for president?  How do they affect the odds of voting for the Republican candidate?

\item\label{reg2} Make a new data frame by converting all of the variables in the 1976 data frame to numeric variables, using {\tt as.numeric()}.  Repeat part (\ref{reg1}), using this new data frame.

\item\label{compare} Compare the results from parts (\ref{reg1}) and (\ref{reg2}).  How are they consistent? Inconsistent?  In this case, would you recommend converting all of the variables to numeric?

\item Repeat parts (\ref{extract}) through (\ref{compare}) for the presidential election year 2000.  In this year, the democratic candidate was Al Gore, who served as vice-president to Bill Clinton, who was impeached but not removed from office.  The Republican candidate was George Bush.

\item Finally, compare your results from 1976 and 2000.  Interpret any similarities or differences in terms of voter preferences.  

\end{enumerate}
Of course there are very many other variables, as well as transformations and interactions, to explore in this data set, and the models you fitted above are not ``optimal'' in any sense.  However I hope you see that you can still get interesting signal out of the data, even with non-optimal models.

% it would be nice to return to the nes data, have the students 
% add ``presvote_intent'' to their 1976 and 2000 data sets, 
% and for each data set have them:
%
% library(nnet)
% summary(mu.3 <- multinom(presvote_intent ~ black + female + age,data=nes1976c))
% 
% then
% (a) interpret this model
% (b) try to add more variables from the 1976 or 2000 data sets, respectively, 
%     to make a better model 
%
% polr from library(MASS) is also an option but it's not clear the categories
% in presvote_intent are plausibly ordered...


\comment{

\item (Based on Gelman \& Hill, Chapter 9, \#6).  You are consulting for a researcher who has performed a randomized trial where the treatment was a series of 26 weekly therapy sessions, the control was no therapy, and the outcome was self-report of emotional state one year later. However, most people in the treatment group did not attend every therapy session. In fact there was a good deal of variation in the number of therapy sessions actually attended. The researcher is concerned that her results represent ``watered down'' estimates because of this variation and suggests adding in another predictor to  he model: number of therapy sessions attended. 
\begin{enumerate}
\item What would you advise her about her suggestion?  Carefully justify your answer in terms of our discussion about controlled experiments, observational studies, confounders, and so forth.
\item Can you suggest another kind of analysis that might give her a better estimate of the treatment effect?  Carefully explain why this might work.  {\em (Hint: Think about the more sophisticated analyses presented in the second half of lecture 18 in class.)}
\end{enumerate}

}

% \item Maybe a problem with log(X) percent change thing

\end{enumerate}

\end{document}




\end{enumerate}


\end{document}
