# Assignment 6

**Ouvert le :**jeudi 17 février 2022, 11:30

**À remettre :**jeudi 3 mars 2022, 23:59

#### Due date: March 3, 2022 at 11:55 pm.

Be sure to use version control `git`

, as you develop your script. Do `git add`

and `git commit`

repeatedly as you add to your script. You will hand in the output of `git log`

for your assignment repository as part of the assignment.

### Introduction

We are often interested in studying the relationship among variables to determine whether there is any underlying association among them. When we think that changes in a variable X explain, or maybe even cause, changes in a second variable Y, we call X an independent (or explanatory) variable and Y a dependent (or response) variable. Moreover, if we plot these variables (X,Y), and the form of the plot resembles a straight line, this may indicate that there may be a linear relationship between the two variables. The relationship is strong if all the data points are close to the line or weak if the points are widely scattered about the line. The covariance and correlation are measures of the strength and direction of a linear relationship between two quantitative variables. A regression line can be defined as a mathematical model describing a relationship between an explanatory variable X, and a response variable Y.

The following are some steps that you should initially follow when analyzing data, and that you should also perform for this assignment:

- Inspect the data graphically, to check for possibles insights underlying their relation.
- Quantify this relationship by computing the appropriate statistical estimators (e.g. covariance and correlation between the variables). What can you conclude from these values?

Consider the average retail prices of various foods in Ontario, which can be found here (the original source of the data is here). Download this data set and place it in your assignment directory. For the rest of this assignment, use the "1810024501-eng.csv" file, which contains monthly data.

### Problem

For answering the following questions, create an R script, named `generateModels.R`

, that will receive an argument from the Linux command line and, depending on its value, perform one of the actions mentioned in parts 1), 2) or 3) below. The script should be modular, as much as you think is necessary. For instance, *at least each part in this assignment could be a function*, such as loading the data, computing correlations, executing the fits, etc. Put your functions in an auxiliary file called `Utilities.R`

.

We also want you to implement defensive programming, so that if the arguments are not a 1, 2 or 3, the script sends a message to the screen letting the user know that only these options are possible, and then stops. It should also check to make sure that there is only one command linear argument given.

In addition to the commands in your script, include **additional comments** explaining your observations.

**0)** Create a function which loads the data above and extracts the "bacon" row and puts it into a data frame. The function should also create a column which contains the months. The months should be indicated by month number, starting at 1, but not repeating (1 through the maximum number of months). You may hard-code the name of the file. Note that this file is a bit of a mess, you will need to add additional flags to your read.csv call to get the data into memory. Also note that the `as.numeric`

function can be used to convert a data frame row into a vector.

Your script should perform the following actions:

- If the command line argument is
**1**:

- Print the correlation estimators for the dataset.
- Implement a linear model to fit the data, and print out the details of the fitted model.
- Generate a graphical representation of the model in the presence of the original data.

- The following actions should be performed if the command line argument is
**2**:- Print the correlation estimators for the dataset.
- Implement a quadratic model to fit the data, and print out the details of the model.
- Generate a graphical representation of the model in the presence of the original data.

- The following actions should be performed if the command line argument is a
**3**:- Print the correlation estimators for the dataset.
- Implement a generalized linear model to fit to the data, using a noise model and link function that you think is appropriate for the data, and print out the details of the model.
- Generate a graphical representation of the model in the presence of the original data.

Some notes to follow when implementing your script:

**OBSERVATION #1**: Do **not** use global variables, i.e. pass arguments to the functions you created otherwise you will lose marks!

**OBSERVATION #2**: You will notice that when running the R script from the command line, the plots will **not be shown**, but instead saved on a file named `Rplots.pdf`

in the same directory as the script is located. This is the default way in which R deals with plots when running in *batch mode*, and totally acceptable for this assignment.

Examples:

```
```$ Rscript generateModels.R
Error: This scripts requires only one argument: 1, 2 or 3
$ Rscript generateModels.R 0
Error: This scripts requires only one argument: 1, 2 or 3
$ Rscript generateModels.R 1 2
Error: This scripts requires only one argument: 1, 2 or 3
$ Rscript generateModels.R 1
---------------
Computing correlation indicators...
Covariance: 7.523983
Correlation coefficient: 0.7611613
---------------
Fitting a Linear Model
Call:
lm(formula = bacon ~ month, data = my.data)
Residuals:
Min 1Q Median 3Q Max
-0.75351 -0.25499 -0.01583 0.23455 0.77719
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 5.18477 0.09680 53.560 < 2e-16 ***
month 0.02467 0.00276 8.938 1.68e-12 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.3702 on 58 degrees of freedom
Multiple R-squared: 0.5794, Adjusted R-squared: 0.5721
F-statistic: 79.89 on 1 and 58 DF, p-value: < 1.679e-16
---------------

Submit your `generateModels.R`

script file and `Utiltites.R`

file, and the output of `git log`

from your assignment repository.

To capture the output of `git log`

use redirection, `git log > git.log`

, and hand in the `git.log`

file.

Assignments will be graded on a 10 point basis. **Due date is March 3rd, 2022 at 11:55pm, with 0.5 point penalty per day for late submission until the cut-off date of March 10, 2022 at 11:00am.**