Assignment 6
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?
A pediatrician wants to study the relationship between a child's height and their head circumferences (both measured in inches). The physician selects a random sample of 13 three year old children, obtaining the following data sets:
heights = 27.75, 24.5, 25.5, 26, 25, 27.75, 26.5, 27, 26.75, 26.75, 27.5, 27.85, 28.0
circ = 17.5, 17.1, 17.1, 17.3, 16.9, 17.6, 17.3, 17.5, 17.3, 17.5, 17.5, 16.9, 18.0
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.
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 both the linear and quadratic models to fit the data, and provide details for both models.
- Generate a plot of the quadratic model comparing with the linear model and 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: 0.2147115 Correlation coefficient: 0.6175882 --------------- Fitting a Linear Model Call: lm(formula = circ ~ heights, data = data) Residuals: Min 1Q Median 3Q Max -0.63868 -0.05173 0.01895 0.10128 0.43662 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 12.95292 1.68832 7.672 9.7e-06 *** heights 0.16466 0.06323 2.604 0.0245 * --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.2501 on 11 degrees of freedom Multiple R-squared: 0.3814, Adjusted R-squared: 0.3252 F-statistic: 6.783 on 1 and 11 DF, p-value: 0.0245 ---------------
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 9th at 11:55pm, with 0.5 point penalty per day for late submission until the cut-off date of March 16th at 11:00am.