---
title: 'R for Researchers: Regression inference solutions'
date: "April 2015"
---

This article contains solutions to exercises for an article in the
series R for Researchers.
For a list of topics covered by this series, see the 
[Introduction](RFR_Introduction.html) article.
If you\'re new to R we highly recommend reading the articles in order.


There is often more than one approach to the exercises.
Do not be concerned if your approach is different than
the solution provided.



These solutions require the solutions from the prior lesson be
run in your R session.

```{r, echo=FALSE, results="hide", message=FALSE, warning=FALSE, fig.show='hide'}
source("Scripts/RFR_alfalfa_DP.R")
source("Scripts/RFR_alfalfa_DE.R")
source("Scripts/RFR_alfalfa_Reg.R")
source("Scripts/RFR_alfalfa_Diag.R")
```



#### Exercise solutions 


These exercises use the alfalfa dataset and the work
you started on the alfAnalysis script.
Open the script and run all the commands in the script
to prepare your session for these problems.


Note, we will use the shade and irrig variable as continuous
variables for these exercise.
They could also be considered as factor variables.
Since both represent increasing levels we first try to
use them as scale.

1. Find the confidence interval for the model coefficients.

    ```{r, comment=NA }
    confint(out5)
    ```

2. Test if inoculant A equals inoculant D.

    ```{r, comment=NA }
    linearHypothesis(out5,
        c("inocA-inocD") )
    ```
    
    This data set does not provide evidence that inoculant
    A and D are different, when considered at the 
    same level of irrigation and shade.


3. Predict the confidence interval for the mean yield for
a plot which has irrigation level 3, shade level 5, and
inoculant C.

    ```{r, comment=NA }
    newAlfObs <- data.frame(irrig=c(3),
                         inoc=c("C"),
                         shade=c(5)
    )
    predict(out5, newAlfObs, interval="confidence")
    ```


4. Plot the observered verse fitted values for your model

    ```{r, comment=NA }
    ggplot(out5Diag, aes(x=fit)) +
      geom_point(aes(y=yield, col=inoc)) +
      geom_line(aes(y=fit)) +
      theme_bw() +
      ggtitle("Alfalfa Yields Vs Predicted") +
      theme( plot.title=element_text(vjust=1.0) ) +
      xlab("Predicted Yield") +
      theme( axis.title.x = element_text(vjust=-.5) ) +
      ylab("Observed yield") +
      theme( axis.title.y = element_text(vjust=1.0) ) +
      theme(legend.position = "bottom")
    ```



5. Commit your changes to AlfAnalysis.




Return to the [Regression inference](RFR_RegInference.html)  article.


Last Revised: 3/9/2015

