# Assignment 7

**Opened:**Friday, 22 October 2021, 12:00 AM

**Due:**Thursday, 4 November 2021, 11:55 PM

#### Due date: November 4, 2021 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 Bike Sharing data set, a description of which can be found here. Download this data set and place it in your assignment directory. For the rest of this assignment, use the "day.csv" file, which contains daily data. Use the "temp" (the temperature of that day) as the independent variable, and "cnt" (the number of bikes used) as the dependent variable. Note that the dependent variable is not continuous.

### 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 observations above, and puts them into an appropriate data structure, and then returns the data structure. Do not hard-code the name of the file into the function.

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 provide details of the fitted model (we will ignore the fact that the dependent variable is not continuous).
- 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 provide details of the model (we will ignore the fact that the dependent variable is not continuous).
- 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 provide 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: 222.5147
Correlation coefficient: 0.627494
---------------
Fitting a Linear Model
Call:
lm(formula = cnt ~ temp, data = my.data)
Residuals:
Min 1Q Median 3Q Max
-4615.3 -1134.9 -104.4 1044.3 3737.8
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1214.6 161.2 7.537 1.43e-13 ***
temp 6640.7 305.2 21.759 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1509 on 729 degrees of freedom
Multiple R-squared: 0.3937, Adjusted R-squared: 0.3929
F-statistic: 473.5 on 1 and 729 DF, p-value: < 2.2e-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 November 4th, 2021 at 11:55pm, with 0.5 point penalty per day for late submission until the cut-off date of November 11, 2021 at 12:00pm.**