Upload
raymond-ford
View
229
Download
1
Embed Size (px)
Citation preview
Scientific Computing (w1)R Computing Workshops
An Introduction to Scientific Computing
workshop 1
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
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’.
Scientific Computing (w1)
Help files, history, memory manager here
graphics –Plots appear hereThe console –
Type new commands here
Editor -Write scripts here
Scientific Computing (w1)
R Programming Workshop 1An introduction to computer programming using the R computing environmentSolving problems using a computerUsing computers as a tool for:
– Plotting graphs and pictures– Analysis of data– Learning maths and physics…
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 workspaceNOTE: empty brackets
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/ Or http://stackoverflow.com/questions/tagged/r
Scientific Computing (w1)
Simple calculations
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
Scientific Computing (w1)Making the computer do
something> 1[1] 1> 1+2[1] 3> 2*3
The * means ‘multiply’
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)
Scientific Computing (w1)Making the computer do
something> pi[1] 3.141593
Inspect the object called ‘pi’
This is the answer
Scientific Computing (w1)Making the computer do
something> pi[1] 3.141593> 3*pi[1] 9.424778
Evaluate ‘3pi’ and then inspect the result
This is the answer
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(…)
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?
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(…)
Scientific Computing (w1)Variables, Objects and
Assignment> x <- 3*pi> x [1] 9.424778
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 objectto see its contents
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”
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) foreach value of x
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 elementof the object y
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 elementof the object y
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
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?
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?
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?
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)
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.
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.
Scientific Computing (w1)
1 2.000000 + =
Variables, Objects and Assignment
3.000000
> 1 + 2.0
Scientific Computing (w1)
123
2.0000002.5000003.000000
+ =
Variables, Objects and Assignment
3.0000004.5000006.000000
> c(1,2,3) + c(2.0,2.5,3.0)
Scientific Computing (w1)
12345678910
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
Scientific Computing (w1)
12345678910
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)
Scientific Computing (w1)
12345678910
1111111111
+
234567891011
=
> x + 1when 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)
Scientific Computing (w1)
Making simple plots
Scientific Computing (w1)Plotting Graphs
> x <- 1:100> y <- sin(x)*exp(-x/100)
Notice how we write the function…100/)sin( xexy
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
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
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)
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…
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…
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…
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
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
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!
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…)
Scientific Computing (w1)
Scientific Computing (w1)
Reading to and writing from files
Scientific Computing (w1)
> getwd()
Loading some data from a file
what is the current working
directory?
Scientific Computing (w1)
> getwd()> list.files()
Loading some data from a file
what files are there?
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
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]”
Scientific Computing (w1)
> length(mydata$force)
Loading some data from a file
what does this do?
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)
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’
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
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
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)
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
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 lineto see a list of colours type
colours()
Tweaking the plot
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
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
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
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
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
Scientific Computing (w1)
1.0 0.471.4 0.872.3 0.243.3 2.004.5 1.305.7 2.806.1 3.408.5 4.409.8 4.00
Plot data from a lab experimentwrite data into a file: File | New File | Text File
save data into a file: File | Save As… “lab_data.txt”
Scientific Computing (w1)
Doing some simple analysis
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, …
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)
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)
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
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’
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
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
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
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
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 dataNow you know how to do this, try using these to improve your data analysis and presentation in lab work.
Scientific Computing (w1)