lecture-07-slides-mm206-v2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

lOMoARcPSD|36552487

Lecture 01 slides MM206 V2

Mathematical And Statistical Computing (University of Strathclyde)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)
lOMoARcPSD|36552487

MM206 Mathematical and Statistical


Computing

Part I: Statistical Computing

Lecture 1: Getting Started

Prof. D. Greenhalgh

Department of Mathematics and Statistics


david.greenhalgh@strath.ac.uk

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

The R project for statistical computing

• R is a software environment for statistical computing and


graphics

• Widely used professionally, including for academic research

• Powerful graphics

• Wide range of built-in functions and data analysis tools

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

• Easy to write your own functions and programs

• Free for you to download for UNIX, Windows, and MacOS


from https://www.r-project.org

• Website contains lots of information, including manuals and


introductory texts

• The R project URL and an introductory manual are on My-


place

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

Getting started

You need to be organised!

• Your personal file storage space is on the networked H: drive


available anywhere on the campus.

• Keep work for different classes separate, create a directory


for each class.

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

• Create a new directory, say, MM206 in your H: drive:

Start > All Programs > Accessories > Windows Explorer

and select your H: drive. Right click inside the window, and
select

New > Folder

and then type MM206 underneath the folder icon.

• You need to do this only once. Each time you come to work
on the class, just save the relevant files into that directory.

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

Running R

• To run R, click on the R icon on the desktop. If there is no


icon, go to

Start > All Programs > R

A window named RGui appears. This is the main R window.

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

• You will also see the R console window. This is where you
input commands. To make sure that any work is saved in
the MM206 directory that you have created, from the RGui
toolbar, select

File > Change dir...

A new window appears and you can browse to find the


MM206 directory.

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

Working with Microsoft Word

• sometimes useful to keep a word processed version of your


working, e.g. a written report.

• in this class the exercises and the exam will involve putting
R code and the results in a MS Word document.

• open a Word document (from e.g. Start > All Programs)

• copy and paste R code and/or graphical outputs from the


RGui into the Word document.

• save your document regularly

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

Command line versus scripts

• It is best to store your R code in script

• Select File > New Script.

• R will open a window entitled Untitled - R Editor.

• Type and edit all your commands in this editor

• On any one line, anything after a hash symbol # will not be


executed - this is used to comment your script

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

Running commands from the script

• Highlight the selection of commands that you want to exe-


cute or if just one line, place the cursor on that line.

• In the RGui toolbar, select Edit > Run line or selection.

• The commands will appear and execute in the console win-


dow.

• Make sure you save your script regularly by pressing Ctrl +S.

• Give the saved file suitable name and make sure it has a .R
file extension.

10

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

Using R as a simple calculator

The simplest way in which you can use R is as a calculator, with


the following operators

• + for addition

• - for subtraction

• * for multiplication

• / for division

• ∧ for powers

11

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

Using R as a simple calculator

• grouping using round brackets ( ) gives the order of opera-


tions

• the letter e denotes powers of 10 as in scientific notation

0.00678 = 6.78 × 10−3


can be written as 6.78e-3 in R.

12

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

Example: Use R to calculate


 
3.4 × 102 × 5 + 72/3
42 − 2.65

Answer:

> (3.4e2 * 5 + 7 ∧ (2/3) ) / (42 -2.65)

[1] 43.29503

13

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

Constants

R has a small number of built-in constants which can come in


handy

Examples

> pi

[1] 3.141593

> month.name

[1] "January" "February" "March" "April" "May"

[6] "June" "July" "August" "September" "October"

[11] "November" "December"


14

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

The assignment operator < −

• Everything in R is stored as an object

• Objects obtain values by assignment

Example: create a object called x with the value 53

> x <- 53

> x

[1] 53
15

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

Note on assignments

The above assignment can also be written as

53 -> x

(note the reversal of the arrow), or

x = 53

Note that 53=x does not make sense and is invalid.

16

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

Data types

Objects do not necessarily contain numeric data types.

Commonly-used data types in R objects include

• numeric e.g. twoK <- 2e3

• string e.g. university <- "Strathclyde"

• logical (Boolean) values e.g. Interesting <- TRUE

There are others!


17

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

Vectors

The simplest type of R object is the vector (the examples above


of single values are special case of vectors of length 1. You
can concatenate single values of the same basic type to create
vectors. This is done with the function c()

Example: Create a vector called “MyNumbers”, containing the


following (numeric) values 3, 9, 23, 1, -78

Answer:

> MyNumbers <- c(3,9,23,1,-78)

> MyNumbers

[1] 3 9 23 1 -78
18

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

Accessing Vector Elements

It is easy to extract individual values from vectors.

Example: What is the third value of the vector MyNumbers?

Answer:

> MyNumbers[3]

[1] 23

Note the use of square brackets [ ].

19

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

The index can itself be an (integer) variable

Example:

What is the nth value of the vector MyNumbers when n = 5?

Answer:

> n <- 5

> MyNumbers[n]

[1] -78

20

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

Missing Values

Sometimes it is important to record a missing value — this is


denoted by NA

Example:

> x <- c(2,8,3,NA,1) # vector x with 4th element missing

> x[4]

[1] NA

21

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

In-built functions R has a huge range of “off the shelf” func-


tions. Some basic ones are

• sqrt() square root of a number/vector

• sum() sum of the elements of a vector

• mean() the arithmetic mean of a vector

• var() the variance of a vector

• log() the natural logarithm of a number/vector

22

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

The arguments to a function are passed to it within the round


brackets ( ).

Example: Calculate the mean of the vector MyNumbers

Answer

> mean(MyNumbers)

[1] -8.4

23

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

Controlling the precision of outputs

Typically R outputs answers to 7 significant figures. Occasionally


you may want to change this using the in-built function options()

Example: What is 7/24 to 9 significant figures?

Answer

> options(digits=9) # report output to 9 significant figures

> 7/24

[1] 0.291666667

> options(digits=7) # restore default value


24

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

Simple Scatter Plots

When values are paired it is often useful to be able to plot one


against the other. Simple scatter plots are very easy in R using
the plot() function.

Example: A scientist recorded the mean number of eggs laid by


parasitoids in their host for a given egg complement. Plot the
relationship between the two.

Egg complement Mean clutch


8 2.1
9 2.5
10 2.7
11 3
25

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

Answer:

> egg.complement <- c(8,9,10,11) # abscissa (x) vector

> mean.clutch <- c(2.1,2.5,2.7,3) # ordinate (y) vector

> plot(egg.complement,mean.clutch) # note order of arguments

26

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

A graphics window will appear with the following

27

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

3.0
l

2.8

l
mean.clutch

2.6

l
2.4
2.2

8.0 8.5 9.0 9.5 10.0 10.5 11.0

egg.complement

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)


lOMoARcPSD|36552487

Finishing an R Session

• Make sure you have saved your R script and (for this class)
your Word document

• On exiting R you will be prompted to save your workspace.


We recommend you decline

28

Downloaded by Prof. Madya Dr. Umar Yusuf Madaki (uymadaki84@gmail.com)

You might also like