Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scientific Computing (w1) R Computing Workshops An Introduction to Scientific Computing workshop 1.

Similar presentations


Presentation on theme: "Scientific Computing (w1) R Computing Workshops An Introduction to Scientific Computing workshop 1."— Presentation transcript:

1 Scientific Computing (w1) R Computing Workshops An Introduction to Scientific Computing workshop 1

2 Scientific Computing (w1) Before we begin… (you only need to do this the first time) Log on Click on Start Click on Program Installer Scroll down, select R for Windows 3.2.0 Click on Install (may take a minute or two at busy times…) Select RStudio Click on Install

3 Scientific Computing (w1) Getting started with R Log on to a UoL computer. Start  All Programs  RStudio [click] Type commands at the ‘prompt’ in the R ‘console’.

4 Scientific Computing (w1) Help files, history, memory manager here graphics – Plots appear here The console – Type new commands here Editor - Write scripts here

5 Scientific Computing (w1) R Programming Workshop 1 An introduction to computer programming using the R computing environment Solving problems using a computer Using computers as a tool for: –Plotting graphs and pictures –Analysis of data –Learning maths and physics…

6 Scientific Computing (w1) The R Working Directory, and Quitting > getwd() get the working directory [1] "Z:/My Documents/Rwork" > setwd(“Z:/My Documents/Rwork") set the working directory > list.files() list files in the working directory > q() quit R – don’t save workspace NOTE: empty brackets

7 Scientific Computing (w1) Getting Help From RStudio – click “Help” menu (top) – select “R Help” From the console – type ?plot for help with the “plot” command From the web - http://www.r-project.org/http://www.r-project.org/ Or http://stackoverflow.com/questions/tagged/rhttp://stackoverflow.com/questions/tagged/r

8 Scientific Computing (w1) Simple calculations

9 Scientific Computing (w1) Making the computer do something > 1 [1] 1 if you type something at the console and hit return, R will print its value onscreen

10 Scientific Computing (w1) Making the computer do something > 1 [1] 1 > 1+2 [1] 3 > 2*3 The * means ‘multiply’

11 Scientific Computing (w1) Making the computer do something > 1 [1] 1 > 1+2 [1] 3 > 2*3 [1] 6 > “dude!” [1] “dude!” The [1] is because R treats all data as an array of values, even if there’s just one number or one string (of characters)

12 Scientific Computing (w1) Making the computer do something > pi [1] 3.141593 Inspect the object called ‘pi’ This is the answer

13 Scientific Computing (w1) Making the computer do something > pi [1] 3.141593 > 3*pi [1] 9.424778 Evaluate ‘3  pi’ and then inspect the result This is the answer

14 Scientific Computing (w1) Making the computer do something > pi [1] 3.141593 > 3*pi [1] 9.424778 > sin(pi/2) [1] 1 Evaluate sin(pi/2), and inspect the result This is the answer Always: function(…)

15 Scientific Computing (w1) Making the computer do something > pi [1] 3.141593 > 3*pi [1] 9.424778 > sin(pi/2) [1] 1 > 1:10 What does this mean?

16 Scientific Computing (w1) Making the computer do something > pi [1] 3.141593 > 3*pi [1] 9.424778 > sin(pi/2) [1] 1 > 1:10 > plot(1:10) Make a simple plot Always: function(…)

17 Scientific Computing (w1) Variables, Objects and Assignment > x <- 3*pi > x [1] 9.424778

18 Scientific Computing (w1) Variables, Objects and Assignment > x <- 3*pi > x [1] 9.424778 Evaluate this bit Assign the result to an object called x. Notice we use two symbols ‘<’ and ‘-’ together (can also use ‘=’) type the name of an object to see its contents

19 Scientific Computing (w1) Variables, Objects and Assignment > x <- 3*pi > x [1] 9.424778 > x <- 1:5 > x [1] 1 2 3 4 5 The object x is a list of numbers (a vector or 1 dimensional array) containing 5 values 1:5 means “1 to 5”

20 Scientific Computing (w1) Variables, Objects and Assignment > x <- 3*pi > x [1] 9.424778 > x <- 1:5 > x [1] 1 2 3 4 5 > y <- sin(x) > y [1] 0.8414710 0.9092974 0.1411200 - 0.7568025 -0.9589243 computes sin(x) for each value of x

21 Scientific Computing (w1) Variables, Objects and Assignment > x <- 3*pi > x [1] 9.424778 > x <- 1:5 > x [1] 1 2 3 4 5 > y <- sin(x) > y [1] 0.8414710 0.9092974 0.1411200 -0.7568025 -0.9589243 > y[2] [1] 0.9092974 the second element of the object y

22 Scientific Computing (w1) Variables, Objects and Assignment > x <- 3*pi > x [1] 9.424778 > x <- 1:5 > x [1] 1 2 3 4 5 > y <- sin(x) > y [1] 0.8414710 0.9092974 0.1411200 -0.7568025 -0.9589243 > y[3] [1] 0.14112 the third element of the object y

23 Scientific Computing (w1) Variables, Objects and Assignment > x <- 1:10 > x [1] 1 2 3 4 5 6 7 8 9 10 > y [1] 0.8414710 0.9092974 0.1411200 -0.7568025 -0.9589243 what happened to y? replace the values stored in x

24 Scientific Computing (w1) Variables, Objects and Assignment > x <- 1:10 > x [1] 1 2 3 4 5 6 7 8 9 10 > y [1] 0.8414710 0.9092974 0.1411200 -0.7568025 -0.9589243 > y <- sin(x) > y what happens now?

25 Scientific Computing (w1) Variables, Objects and Assignment > x <- 1:10 > x [1] 1 2 3 4 5 6 7 8 9 10 > y <- seq(2, 3, length=10) > x + y what happens here?

26 Scientific Computing (w1) Variables, Objects and Assignment > x <- 1:10 > x [1] 1 2 3 4 5 6 7 8 9 10 > y <- seq(2, 3, length=10) > x + y > y <- seq(2, 3, length=7) > x + y what went wrong here?

27 Scientific Computing (w1) Variables, Objects and Assignment > x <- 1:10 > x [1] 1 2 3 4 5 6 7 8 9 10 > y <- seq(2, 3, length=10) > x + y > y <- seq(2, 3, length=7) > x + y > x + 1 but this one works! when the array sizes aren’t compatible (x has 10 elements, y has 7) they cannot be combined… except if one array can be repeated (an integer number of times) to match the size of the other (array of size 1 is scaled into array of size 10 by repeating values)

28 Scientific Computing (w1) Variables, Objects and Assignment > x <- 1.2 [1] 1.2 in R, the things you manipulate and store are objects. There are different classes of object and R has methods for treating an object in a manner appropriate to its class. “1.2” is just a number, a scalar. In fact, it’s a floating point number (not an integer). When we assign an object this value, the object instantly takes the right form. In this case ‘x’ becomes an object storing our single floating point number.

29 Scientific Computing (w1) Variables, Objects and Assignment > x <- c(1.2, 1.3, 1.4, 2.0) [1] 1.2 1.3 1.4 2.0 in R, the things you manipulate and store are objects. There are different classes of object and R has methods for treating an object in a manner appropriate to its class. We use the c(…) function to combine a several numbers into one object, often called a vector (a 1D list of numbers) or an array (which can have more than 1 dimension). The old ‘x’ has been replaced with a new one, which is now an array storing four values.

30 Scientific Computing (w1) 1 2.000000 += Variables, Objects and Assignment 3.000000 > 1 + 2.0

31 Scientific Computing (w1) 123123 2.000000 2.500000 3.000000 += Variables, Objects and Assignment 3.000000 4.500000 6.000000 > c(1,2,3) + c(2.0,2.5,3.0)

32 Scientific Computing (w1) 1 2 3 4 5 6 7 8 9 10 2.000000 2.166667 2.333333 2.500000 2.666667 2.833333 3.000000 += ? when the array sizes aren’t compatible (x has 10 elements, y has 7) they cannot be combined… > x + y

33 Scientific Computing (w1) 1 2 3 4 5 6 7 8 9 10 1 += > x + 1 ? when the array sizes aren’t compatible (x has 10 elements, y has 7) they cannot be combined… except if one array can be repeated (an integer number of times) to match the size of the other (array of size 1 is scaled into array of size 10 by repeating values)

34 Scientific Computing (w1) 1 2 3 4 5 6 7 8 9 10 11111111111111111111 + 2 3 4 5 6 7 8 9 10 11 = > x + 1 when the array sizes aren’t compatible (x has 10 elements, y has 7) they cannot be combined… except if one array can be repeated (an integer number of times) to match the size of the other (array of size 1 is scaled into array of size 10 by repeating values)

35 Scientific Computing (w1) Making simple plots

36 Scientific Computing (w1) Plotting Graphs > x <- 1:100 > y <- sin(x)*exp(-x/100) Notice how we write the function…

37 Scientific Computing (w1) Plotting Graphs > x <- 1:100 > y <- sin(x)*exp(-x/100) > plot(x, y) make a plot, putting points at each pair of coordinates (x[i], y[i]) where i=1,2,…,100

38 Scientific Computing (w1) Plotting Graphs > x <- 1:100 > y <- sin(x)*exp(-x/100) > plot(x, y, type="l") Change the plot for one using a line. Note: this is the letter l – for line – not the number 1

39 Scientific Computing (w1) Plotting Graphs, again… (1) > x <- seq(-10, 10, by=0.1) > y <- sin(x)*exp(-x/100) > plot(x, y, type="l") Use “up” arrow key to bring back previous lines (no need to re-type)

40 Scientific Computing (w1) Plotting Graphs, again… (2) > x <- seq(-10, 10, by=0.1) > y <- 2*x + 1 > plot(x, y, type="l") Try a new function…

41 Scientific Computing (w1) Plotting Graphs, again… (3) > x <- seq(-10, 10, by=0.1) > y <- 2*x^2 – x + 1 > plot(x, y, type="l") Try a new function…

42 Scientific Computing (w1) Plotting Graphs, again… (4) > x <- seq(-10, 10, by=0.1) > y <- (sin(x))^2 + cos(x/2-0.1) > plot(x, y, type="l") If you hit return on an unfinished line, R will wait for you to finish…

43 Scientific Computing (w1) More maths functions > y <- asin(x) Warning message: In asin(x) : NaNs produced asin(x) means arcsin(x) or sin -1 (x) what’s the problem here? Take another look at x

44 Scientific Computing (w1) > x <- seq(-1, 1, length=100) > y <- asin(x) > plot(x, y, type=“l”) asin(x) isn’t defined outside -1 < x < +1 More maths functions

45 Scientific Computing (w1) > y <- acos(x); print(y) > y <- tan(x) > y <- sinh(x) > x <- seq(0.1, 10, length=100) > y <- log(x) > y <- log10(x) > y <- sqrt(x) > y <- x^(-0.5) > y <- 2.0^x try some other functions… note the default is the natural logarithm (‘ log ’) More maths functions two lines in one!

46 Scientific Computing (w1) > x <- c(-0.9, -0.8, 0, 0.8, 0.9) > print(x) > y <- acos(x) > print(y) choose your points the c(…) function combines a list of values into a single object – very useful! at the console you don’t need to explicitly include the print(…) function. But in a script you do (more later…)

47 Scientific Computing (w1)

48 Reading to and writing from files

49 Scientific Computing (w1) > getwd() Loading some data from a file what is the current working directory?

50 Scientific Computing (w1) > getwd() > list.files() Loading some data from a file what files are there?

51 Scientific Computing (w1) > getwd() > list.files() > mydata <-read.table( "http://www.star.le.ac.uk/sav2/waves.dat", header=TRUE) Loading some data from a file use read.table to load a simple text file can load from your disc or off the web – the file name is in quotes (“”) We set header=TRUE inside read.table because the file has a ‘header’ line

52 Scientific Computing (w1) > getwd() > list.files() > mydata <-read.table( "http://www.star.le.ac.uk/sav2/waves.dat", header=TRUE) > plot(mydata$force, type=“l”) Loading some data from a file the object mydata now contains the contents of the file. This is a single column of data. The column is named ‘force’ in the file header. We use “[object]$[column]”

53 Scientific Computing (w1) > length(mydata$force) Loading some data from a file what does this do?

54 Scientific Computing (w1) > length(mydata$force) [1] 320 > mydata$x <- 1:320 Loading some data from a file add a new column called ‘x’ to the object ‘mydata’ (contains values 1,2,…320)

55 Scientific Computing (w1) > length(mydata$force) [1] 320 > mydata$x <- 1:320 > plot(mydata$x, mydata$force, type=“l”) Loading some data from a file plot data from columns ‘x’ and ‘force’ of object ‘mydata’

56 Scientific Computing (w1) > write.table(mydata, “mydata.txt”) Write some data to a file object containing data to be saved (‘mydata’) name of file (in working directory) to be saved Now look at the file, e.g. File | Open File

57 Scientific Computing (w1) > write.table(mydata, “mydata.txt”, row.names=FALSE) Write some data to a file switch off the naming of each row (“1”, “2”, …) Now look (again) at the file, e.g. File | Open File

58 Scientific Computing (w1) > mydata <- read.table(“mydata.txt”, header=TRUE) > x <- mydata$x > y <- mydata$force > plot(x, y, type=“l”) Loading some data from a file this is the file we just wrote (in the working directory)

59 Scientific Computing (w1) > mydata <- read.table(“mydata.txt”, header=TRUE) > x <- mydata$x > y <- mydata$force > plot(x, y, type=“l”) > plot(x, y, type=“l”, bty=“n”) Tweaking the plot this means ‘box type’. Try setting equal to any of n, l, u, o for different boxes

60 Scientific Computing (w1) > mydata <- read.table(“mydata.txt”, header=TRUE) > x <- mydata$x > y <- mydata$force > plot(x, y, type=“l”) > plot(x, y, type=“l”, bty=“n”) > plot(x, y, type=“l”, bty=“n”, col=“blue”) set colour of the points or line to see a list of colours type colours() Tweaking the plot

61 Scientific Computing (w1) > mydata <- read.table(“mydata.txt”, header=TRUE) > x <- mydata$x > y <- mydata$force > plot(x, y, type=“l”) > plot(x, y, type=“l”, bty=“n”) > plot(x, y, type=“l”, bty=“n”, col=“blue”, lwd=2) change line width Tweaking the plot

62 Scientific Computing (w1) > mydata <- read.table(“mydata.txt”, header=TRUE) > x <- mydata$x > y <- mydata$force > plot(x, y, type=“l”) > plot(x, y, type=“l”, bty=“n”) > plot(x, y, type=“l”, bty=“n”, col=“blue”, lwd=2, cex.axis=1.5) there are many ‘cex’ (character expansion) arguments, this one is for the axis numbering Tweaking the plot

63 Scientific Computing (w1) > mydata <- read.table(“mydata.txt”, header=TRUE) > x <- mydata$x > y <- mydata$force > plot(x, y, type=“l”) > plot(x, y, type=“l”, bty=“n”) > plot(x, y, type=“l”, bty=“n”, col=“blue”, lwd=2, cex.axis=1.5, xlab=“time”, ylab=“force”) ylab and xlab arguments are used to set the y and x axis labels Tweaking the plot

64 Scientific Computing (w1) > mydata <- read.table(“mydata.txt”, header=TRUE) > x <- mydata$x > y <- mydata$force > plot(x, y, type=“l”) > plot(x, y, type=“l”, bty=“n”) > plot(x, y, type=“l”, bty=“n”, col=“blue”, lwd=2, cex.axis=1.5, xlab=“time”, ylab=“force”) > ?plot > ?par there are a LOT of settings you can play with Tweaking the plot

65 Scientific Computing (w1) More about plots New plots always begin with a ‘high level’ plot command, usually plot(…) You can add to this using segments(…), abline(…), points(…) and so on. Once you’re happy with a plot use the ‘Export’ menu (over the plot window) to save a PDF, PS, PNG, JPG, or save into clipboard. Use  and  buttons over plot window to move between previous plots

66 Scientific Computing (w1) 1.0 0.47 1.4 0.87 2.3 0.24 3.3 2.00 4.5 1.30 5.7 2.80 6.1 3.40 8.5 4.40 9.8 4.00 Plot data from a lab experiment write data into a file: File | New File | Text File save data into a file: File | Save As… “lab_data.txt”

67 Scientific Computing (w1) Doing some simple analysis

68 Scientific Computing (w1) > mydata <- read.table(“lab_data.txt”) > x <- mydata$V1 > y <- mydata$V2 > plot(x, y) NOTE: no ‘header’ in this file Load and plot the data if the columns in a data table don’t have names specified, they default to V1, V2, V3, …

69 Scientific Computing (w1) > mydata <- read.table(“lab_data.txt”) > x <- mydata$V1 > y <- mydata$V2 > plot(x, y, xlim=c(0,10), ylim=c(0,6)) Load and plot the data xlim is given two values which set the lower and upper limits of the x axis. The two limits are combined into a single object using c(lower, upper)

70 Scientific Computing (w1) > mydata <- read.table(“lab_data.txt”) > x <- mydata$V1 > y <- mydata$V2 > plot(x, y, xlim=c(0,10), ylim=c(0,6))) > y.error <- 1.0 > segments(x, y-y.error, x, y+y.error) Load and plot the data let’s add some error bars draws line segments between coordinates (x, y-y.error) and (x, y+y.error)

71 Scientific Computing (w1) > mydata <- read.table(“lab_data.txt”) > x <- mydata$V1 > y <- mydata$V2 > plot(x, y, xlim=c(0,10), ylim=c(0,6))) > y.error <- 1.0 > segments(x, y-y.error, x, y+y.error) > result <- lm(y ~ x) Load and plot the data lm(…) is a powerful function for fitting linear functions to data

72 Scientific Computing (w1) > result <- lm(y ~ x) Fitting data with a linear model this is a ‘formula’ – a special kind of object in R. It says to ‘fit y as a linear function of x’). Note the special tilde ‘~’ symbol for formulae the formula (and data) are given to the lm(…) function which does the hard work for you… the output of lm(…) is saved to an object called ‘result’

73 Scientific Computing (w1) > result <- lm(y ~ x) > print(result) display the main contents of the ‘result’ object: the best fitting intercept and slope Fitting data with a linear model

74 Scientific Computing (w1) > result <- lm(y ~ x) > print(result) > summary(result) display a more informative summary of the ‘result’ object: the best fitting intercept and slope, with additional error analysis Fitting data with a linear model

75 Scientific Computing (w1) > result <- lm(y ~ x) > print(result) > summary(result) > abline(a=-0.07, b=0.47) abline(…) adds a straight line with intercept a and slope b Fitting data with a linear model

76 Scientific Computing (w1) > result <- lm(y ~ x) > print(result) > summary(result) > abline(a=-0.07, b=0.47) > abline(result) or just get the slope and intercept out of the object ‘result’ Fitting data with a linear model

77 Scientific Computing (w1) What have we learnt? start R / Rstudio (and you can install it at home) use R as a simple calculator compute values for elementary functions evaluate y = f(x) at multiple x values plot (x, y) coordinates as points, or join as a curve load a data table from a file or from the web save a data table to a file make a plot (with error bars) of some data perform and plot a simple linear fit to the data Now you know how to do this, try using these to improve your data analysis and presentation in lab work.

78 Scientific Computing (w1)


Download ppt "Scientific Computing (w1) R Computing Workshops An Introduction to Scientific Computing workshop 1."

Similar presentations


Ads by Google