There are 8 labs in this course. Each one will build on the last one, with support provided in a series of tutorials (see the tutorials tab). You should NOT submit the tutorial work. We will work through the tutorials together and then your job is to submit the requirements on this page.
By the end of this week’s lab, you will be able to:
If the labs are causing major problems with your computer or your computer hardware is struggling (or you have any other software issue), Talk to Dr Greatrex. We can fix this and there are other options for “online R” that you can use.
THERE IS A TEAMS DISCUSSION FOR ANY LAB HELP (see canvas)
Assignment 1 is due by midnight the night before your next lab on Canvas. See here for assignment guidelines & we will cover them in detail here.
These were covered in homework 2 and are here for reference. Your R and R studio should be up to date.
Work through Tutorial1.3 to open R-studio and change your settings.
Use Tutorial 2.1 to make your first R-project called STAT462-Lab1-Project
[OPTIONAL BUT RECOMMENDED] Now work through Tutorial 3 to get used to the basics
Now go back to Tutorial 2B to understand what an R-Package is and to install the code packages/libraries we will need for this course.
Everything above was for your own learning - you do not need to submit any of it. Now we will start the actual lab you will submit. Consider printing this out and checking off each step one by one.
First, see if you remember how to close and re-open R studio.
Save everything then close down R studio.To re-open, do not reopen R from the programmes folder or icon!
INSTEAD, GO TO THE LAB 1 FOLDER ON YOUR COMPUTER AND DOUBLE CLICK THE .RPROJ FILE CALLED STAT462-Lab1-Project!.
This will get you straight back into your lab folder for you to carry on.
Create a new RMarkdown document called STAT462-Lab1-PSUID.Rmd
. e.g. for me STAT462-Lab1-hlg5155.Rmd.
Delete all the text/code below line 11 (e.g. everything from “R Markdown onward”)
You might find it easier to use the markdown visual text editor.
Click the “A” symbol at the top right to switch (instructions here:Tutorial 4.6)
In the white text area, create a level 1 heading called “Introduction to STAT-462”. For a cheat sheet to help with this, move your mouse to the top of the page, click the help menu, then click Markdown Quick reference, or see Tutorial 4.6.
Leave a blank line, then use the STAT-462 syllabus to describe the course late policy in your own words (e.g. you’re writing about this in the white space).
Press “knit” at the top of the screen. If you haven’t made a mistake a pop up should appear with a html file and your edits. If you have a made a mistake, stop and fix before continuing.
<br<
Leave another blank line and add a new Level 1 heading called “Code Showcase”. Add a blank line afterwards too. Create a code chunk (). Inside use R code to calculate the following
103*103*103
, or 103^3
) _ The co-sine of your age (google is your friend, google R function for cosine)
Press “knit” at the top of the screen. If you haven’t made a mistake a pop up should appear with a html file and your edits. If you have a made a mistake, stop and fix before continuing.
If you have run through the tutorials, you should have installed all the packages you need for this course.
IF NOT GO AND DO THE BULK INSTALL IN Tutorial 2.2]
You only need to download the packages once, but we still need to load them every time we need a command in that package (in the same way you only download your banking app once from the app store but need to press the icon every time you want to use it).
Today we will be using commands from the skimr package, the ggplot2 package and the plotly package.
Somewhere near the topc of your script, make a new code chunk and add this code. Remember to run the code chunk! (pressing the green arrow, or go to the run button on the top right and press Run All)
library(tidyverse)
library(skimr)
library(ggplot2)
library(plotly)
library(ggpubr)
Leave a blank line, and create a new level 1 heading called Car Analysis. Leave a blank line afterwards too. We’re going to work with a table of data that’s already pre-loaded into R inside the ggplot2 package.
Make sure you have run the library code chunk above without error, or it won’t work.
Load the data using this command
data(mpg)
?mpg
command in the console. This will bring up the help file. Now look at the data itself. If you look in the environment tab, you will see a new variable called mpg. Click on it’s NAME to see the spreadsheet/table itself and familiarise yourself with the data.
We could have also looked at the raw data by either by typing its name into the console or a code chunk, or by using commands like head(mpg)
to show the first few lines
Let’s look at the summary statistics. Leave a blank line and create a new code chunk containing the following code
# mpg comes from the ggplot2 package
# skim comes from the skimr package
skim(mpg)
This command compiles the summary statistics for mpg. You can also use the summary() command to achieve a similar result
summary(mpg)
Now below your code, answer the following questions in full sentences.
What is the mean manufacturing year of the car models in the dataset?
What is the maximum value of the number of cylinders of the car models?
What type of data is the manufacturer column of the dataset? (nominal, ordinal, interval, continous) State why you answered that.
Suppose I want to compare the miles per gallon of the car models being driven in cities for different classes of cars. We can make the comparison by making a side-by-side boxplot. Create a new code chunk. Use the code below to create the plot.
To see the full plot, press the tiny “show in new window” button at the top right of the plot itself.
# boxplot is the command. You can have a command on several lines.
boxplot(cty~class, # the miles per gallon column vs each type
data=mpg, # from the mpg table
xlab="Class",ylab="mpg in a city",
col=c("deepskyblue4","deepskyblue")) # and colours
As we discussed above, the mpg table is loaded into R. But what if we want to access a specific column? This is where the $ symbol is useful. It means, “select the column named..”
For example if you type mpg$model
into the console, you will see that it just prints the data from the cyl column (number of cylinders) onto the screen. We could then apply a command to those numbers. For example unique(mpg$model)
will find all the unique values in that column
median()
and IQR()
commands to find the actual median and inter quartile range of the column containing highway miles per gallon. Summarise your results in full sentencesI want to see if the hwy variable in our data is normally distributed. We can do this using the histogram and normal-qq plot. The code below makes a histogram and QQNorm plot for the car miles per gallon on a highway. It also runs a Shapiro-Wilks test for the horsepower.
hist(mpg$hwy,br=10)
# Instead we are going to use a nicer format from the `ggpubr` library
ggqqplot(mpg$hwy,col="red")
# Shapiro Wilks test
shapiro.test(mpg$hwy)
If you need help, see this tutorial (https://r4ds.had.co.nz/data-visualisation.html).
Now we’re going to move onto some commands that allow us to calculate normal probabilities using R, rather than slowly by hand. Using the R functions pnorm()
and qnorm()
, we can find the cumulative probabilities and normal quantiles for any normal distribution.
For example, Cat lifespans have a normal distribution with a mean of 15.4 and a standard deviation of 2.3. What’s the probability of a cat lifespan less than 14 years?
?pnorm # use the help file of ?pnorm to see what lower.tail=TRUE/FALSE does
pnorm(14,mean=15.4,sd=2.4,lower.tail = TRUE)
## [1] 0.2798345
So, it’s 0.2798 - or ~28%
What about the probability of a cat being older than 20?
pnorm(20,mean=15.4,sd=2.4,lower.tail = FALSE)
## [1] 0.02764015
Normal quantiles go the other way around - they tell you the z score for whatever probability you require. For example, 80% of cats live to be what age?
qnorm(.80,mean=15.4,sd=2.4)
## [1] 17.41989
So the the answer is 17.42 years (remember the units!)
Use R to find the probability below and write your conclusions in the text
If X ∼ N(5,2), then find P(X ≤ 4)
If X ∼ N(5,2), then find the value a such that P(X < a) = 0.025
Remember that an A is 94%, so you can ignore this section and still easily get an A.
But here is your time to shine. Also, if you are struggling in another part of the lab, you can use this to gain back points.
To get the final 4 marks in the lab, you need to show me something new, e.g. you need to go above and beyond the lab questions in some way.
Please tell us in your R script what you did!
Here in lab 1, maybe you added in different text formats to make your lab script more clear (bold/italic etc). Maybe you worked out how to add axis labels to ggplot (note, it’s a pain), maybe you used nested headings or sub-headings, or worked out how to add a table of contents.
OR……..
Alternatively, you get 4/4 for successfully answering this question fully in your script, using R to help:
Records maintained by the your admin office indicate that amount of time elapsed between the submission of travel receipts and the final reimbursement of funds has approximately a normal distribution with a mean of 39 days and a standard deviation of 6 days. If you submitted your travel claim 55 days ago, what is the probability that it should have been returned by now? What might you conclude?
Remember to save your work throughout and to spell check your writing (next to the save button). Now, press the knit button again. If you have not made any mistakes in the code then R should create a html file in your lab 1 folder which includes your answers. If you look at your lab 1 folder, you should see this there - complete with a very recent time-stamp.
In that folder, double click on the html file. This will open it in your browser. CHECK THAT THIS IS WHAT YOU WANT TO SUBMIT.
If you are on R studio cloud, see Tutorial 1 for how to download your files
Now go to Canvas and submit BOTH your html and your .Rmd file in Lab 1.
See the table below for what this means - 100% is hard to get!
HTML FILE SUBMISSION - 8 marks
RMD CODE SUBMISSION - 8 marks
MARKDOWN/CODE STYLE - 10 MARKS
Your code and document is neat and easy to read. LOOK AT YOUR HTML FILE IN YOUR WEB-BROWSER BEFORE YOU SUBMIT. There is also a spell check next to the save button. You have written your answers below the relevant code chunk in full sentences in a way that is easy to find and grade. For example, you have written in full sentences, it is clear what your answers are referring to.
Introduction to STAT-462: 10 MARKS
You have described the lab late policy clearly in a way I could share with a new student.
Code Showcase: 20 MARKS
You have managed to successfully complete all the code challenges
Car/Normal analysis: 40 MARKS
You included all the code and successfully answered the questions, providing reasoning where appropriate
Above and beyond: 4 MARKS
Here you need to go above and beyond the lab questions in some way. Here in lab 1, maybe you added in different text formats to make your lab script more clear (bold/italic etc). Maybe you worked out how to add axis labels to ggplot (note, it’s a pain), maybe you used nested headings or sub-headings, or worked out how to add a table of contents.
You get 2/4 for doing something new in any way and 4/4 for something really impressive or multiple small things.
[100 marks total]
Overall, here is what your lab should correspond to:
Grade | % Mark | Rubric |
---|---|---|
A* | 98-100 | Exceptional. Not only was it near perfect, but the graders learned something. THIS IS HARD TO GET. |
NA | 96+ | You went above and beyond |
A | 94+: | Everything asked for with high quality. Class example |
A- | 90+ | The odd minor mistake, All code done but not written up in full sentences etc. A little less care |
B+ | 87+ | More minor mistakes. Things like missing units, getting the odd question wrong, no workings shown |
B | 84+ | Solid work but the odd larger mistake or missing answer. Completely misinterpreted something, that type of thing |
B- | 80+ | Starting to miss entire/questions sections, or multiple larger mistakes. Still a solid attempt. |
C+ | 77+ | You made a good effort and did some things well, but there were a lot of problems. (e.g. you wrote up the text well, but messed up the code) |
C | 70+ | It’s clear you tried and learned something. Just attending labs will get you this much as we can help you get to this stage |
D | 60+ | You attempt the lab and submit something. Not clear you put in much effort or you had real issues |
F | 0+ | Didn’t submit, or incredibly limited attempt. |