Upcoming SlideShare
×

# An introduction to Mapping and Spatial Modelling in R (preview version)

10,925 views
10,759 views

Published on

An introduction to Mapping and Spatial Modelling in R - draft version (not well proofed for typos, etc.). The full and most up-to-date version is available to download from http://www.researchgate.net/publication/258151270_An_Introduction_to_Mapping_and_Spatial_Modelling_in_R. It currently consists of six sessions/chapters:
(1) Getting Started with R
(2) A Geographical Demonstration of R
(3) A Little More about the Workings of R
(4) Using R as a Simple GIS
(5) Defining Neighbours and Creating a Weights Matrix
(6) Spatial Regression analysis
More will be added as time and tides allow.

Published in: Education, Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
10,925
On SlideShare
0
From Embeds
0
Number of Embeds
8,387
Actions
Shares
0
91
0
Likes
0
Embeds 0
No embeds

No notes for slide

### An introduction to Mapping and Spatial Modelling in R (preview version)

4. 4. An Introduction to Mapping and Spatial Modelling in R. © Richard Harris, 2013 4
6. 6. Figure 1.1. Screen shot of the R Gui for Windows 1.2.1 Using R as a calculator At its simplest, R can be used as a calculator. Typing 1 + 1 after the prompt > will (after pressing the return/enter key, ↵) produce the result 2, as in the following example: > 1 + 1 [1] 2 Comments can be indicated with a hash tag and will be ignored > # This is a comment, no need to type it Some other simple mathematical expressions are given below. 10 20 30 > 10 - 5 [1] 5 > 10 * 2 [1] 20 > 10 - 5 * 2 [1] 0 > (10 - 5) * 2 [1] 10 > sqrt(100) [1] 10 > 10^2 [1] 100 > 100^0.5 [1] 10 > 10^3 [1] 1000 > log10(100) [1] 2 > log10(1000) [1] 3 > 100 / 5 [1] 20 > 100^0.5 / 5 [1] 2 # The order of operations gives priority to # multiplication # The use of brackets changes the order # Uses the function that calculates the square root # 102 # 100.5, i.e. the square root again # Uses the function that calculates the common log An Introduction to Mapping and Spatial Modelling in R. © Richard Harris, 2013 6
7. 7. 1.2.2 Incomplete commands If you see the + symbol instead of the usual (>) prompt it is because what has been typed is incomplete. Often there is a missing bracket. For example, 10 > sqrt( + 100 + ) [1] 10 > (1 + 2) * (5 - 1 + ) [1] 12 # The + symbol indicates that the command is incomplete Commands broken over multiple lines can be easier to read. 20 > for (i in 1:10) { + print(i) + } [1] 1 [1] 2 [1] 3 [1] 4 [1] 5 [1] 6 [1] 7 [1] 8 [1] 9 [1] 10 # This is a simple loop # printing the numbers 1 to 10 on-screen 1.2.3 Repeating or modifying a previous command If there is a mistake in a line of a code that needs to be corrected or if some previously typed commands will be repeated then the ↑ and ↓ keys on the keyboard can be used to scroll between previous entries in the R Console. Try it! 1.3 Scripting and Logging in R 30 1.3.1 Scripting You can create a new script file from the drop down menu File → New script (in Windows) or File → New Document (Mac OS). It is basically a text file in which you could write, for example, a <- 1:10 print(a) 40 In Windows, if you move the cursor up to the required line of the script and press Ctrl + R, then it will be run in the R Console. So, for example, move the cursor to where you have typed a <- 1:10 and press Ctrl + R. Then move down a line and do the same. The contents of a, the numbers 1 to 10, should be printed in the R Console. If you continue to keep the focus on the Scripting window and go to Edit in the RGui you will find an option to run everything. Similar commands are available for other Operating Systems (e.g. Mac key + Return). You can save files and load previously saved files. Scripting is both good practice and good sense. It is good practice because it allows for reproducibility of your work. It is good sense because if you need to go back and change things you can do so easily without having to start from scratch. Tip: It can be sensible to create the script in a simple text editor that is independent of R, such as An Introduction to Mapping and Spatial Modelling in R. © Richard Harris, 2013 7
8. 8. Notepad. Although you will not be able to use Ctrl + R in the same way, if R crashes for any reason you will not lose your script file. 1.3.2 Logging You can save the contents of the R Console window to a text file which will then give you a log file of the commands you have been using (including any mistakes). The easiest way to do this is to click on the R Console (to take the focus from the Scripting window) and then use File → Save History (in Windows) or File → Save As (Mac). Note that graphics are not usually plotted in the R Console and therefore need to be saved separately. 1.4 Some R Basics 10 1.4.1 Functions, assignments and getting help It is helpful to understand R as an object-oriented system that assigns information to objects within the current workspace. The workspace is simply all the objects that have been created or loaded since beginning the session in R. Look at it this way: the objects are like box files, containing useful information, and the workspace is a larger storage container, keeping the box files together. A useful feature of this is that R can operate on multiple tables of data at once: they are just stored as separate objects within the workspace. To view the objects currently in the workspace, type > ls() character(0) 20 Doing this runs the function ls(), which lists the contents of the workspace. The result, character(0), indicates that the workspace is empty. (Assuming it currently is). To find out more about a function, type ? or help with the function name, > ?ls() > help(ls) This will provide details about the function, including examples of its use. It will also list the arguments required to run the function, some of which may be optional and some of which may have default values which can be changed as required. Consider, for example, > ?log() A required argument is x, which is the data value or values. Typing log() omits any data and generates an error. However, log(100) works just fine. The argument base takes a default value of e1 which is approximately 2.72 and means the natural logarithm is calculated. Because the default is assumed unless otherwise stated so log(100) gives the same answer as log(100, base=exp(1)). Using log(100, base=10) gives the common logarithm, which can also be calculated using the convenience function log10(100). 30 The results of mathematical expressions can be assigned to objects, as can the outcome of many commands executed in the R Console. When the object is given a name different to other objects within the current workspace, a new object will be created. Where the name and object already exist, the previous contents of the object will be over-written, without warning – so be careful! > a <- 10 – 5 > print(a) [1] 5 > b <- 10 * 2 An Introduction to Mapping and Spatial Modelling in R. © Richard Harris, 2013 8
9. 9. > print(b) [1] 20 > print(a * b) [1] 100 > a <- a * b > print(a) [1] 100 10 20 In these examples the assignment is achieved using the combination of < and -, as in a <- 100. Alternatively, 100 -> a could be used or, more simply, a = 100. The print(..)command can often be omitted, though it is useful, and sometimes necessary (for example, when what you hope should appear on-screen doesn't). > f = a * b > print(f) [1] 2000 > f [1] 2000 > sqrt(b) [1] 4.472136 > print(sqrt(b), digits=3) [1] 4.47 > c(a,b) [1] 100 20 > c(a,sqrt(b)) [1] 100.000000 4.472136 > print(c(a,sqrt(b)), digits=3) [1] 100.00 4.47 # The additional parameter now specifies # the number of significant figures # The c(...) function combines its arguments 1.4.2 Naming objects in the workspace Although the naming of objects is flexible, there are some exceptions, 30 > _a <- 10 Error: unexpected input in "_" > 2a <- 10 Error: unexpected symbol in "2a" Note also that R is case sensitive, so a and A are different objects > a > A > a [1] <- 10 <- 20 == A FALSE The following is rarely sensible because it won't appear in the workspace, although it is there, 40 > .a <- 10 > ls() [1] "a" "b" "f" > .a [1] 10 > rm(.a, A) # Removes the objects .a and A (see below) 1.4.3 Removing objects from the workspace From typing ls() we know when the workspace is not empty. To remove an object from the workspace it can be referenced to explicitly – as in rm(A) – or indirectly by its position in the workspace. To see how the second of these options will work, type > ls() An Introduction to Mapping and Spatial Modelling in R. © Richard Harris, 2013 9
10. 10. [1] "a" "b" "f" The output returned from the ls() function is here a vector of length three where the first element is the first object (alphabetically) in the workspace, the second is the second object, and so forth. We can access specific elements by using notation of the form ls[index.number]. So, the first element – the first object in the workspace – can be obtained using, > ls()[1] [1] "a" > ls()[2] [1] "b" Note how the square brackets Similarly, 10 # Get the brackets right! some rounded some square […] are used to reference specific elements within the vector. > ls()[3] [1] "f" > ls()[c(1,3)] [1] "a" "f" > ls()[c(1,2,3)] [1] "a" "b" "f" > ls()[c(1:3)] [1] "a" "b" "f" # 1:3 means the numbers 1 to 3 Using the remove function, rm(...), the second and third objects in the workspace can be removed using 20 > rm(list=ls()[c(1,3)]) > ls() [1] "b" Alternatively, objects can be removed by name > rm(b) To delete all the objects in the workspace and therefore empty it, type the following code but – be warned! – there is no undo function. Whenever rm(...) is used the objects are deleted permanently. > rm(list=ls()) > ls() character(0) 30 # In other words, the workspace is empty 1.4.4 Saving and loading workspaces Because objects are deleted permanently, a sensible precaution prior to using rm(...) is to save the workspace. To do so permits the workspace to be reloaded if necessary and the objects recovered. One way to save the workspace is to use > save.image(file.choose(new=T)) Alternatively, the drop-down menus can be used (File → Save Workspace in the Windows version of the RGui). In either case, type the extension .RData manually else it risks being omitted, making it harder to locate and reload what has been saved. Try creating a couple of objects in your workspace and then save it with the names workspace1.RData To load a previously saved workspace, use 40 > load(file.choose()) or the drop-down menus. When quitting R, it will prompt to save the workspace image. If the option to save is chosen it will An Introduction to Mapping and Spatial Modelling in R. © Richard Harris, 2013 10
12. 12. Field, A., Miles, J. & Field, Z., 2012. Discovering Statistics Using R. London: Sage However, none of these books is about mapping or spatial analysis (of particular interest to me as a geographer). For that, the authoritative guide making the links between geographical information science, geographical data analysis and R (but not really written for R newcomers) is, Bivand, R.S., Pebesma, E.J. & Gómez-Rubio, V., 2008. Applied Spatial Data Analysis with R. Berlin: Springer. Also helpful is, Ward, M.D. & Skrede Gleditsch, K., 2008. Spatial Regression Models. London: Sage. (Which uses R code examples). 10 And Chun, Y. & Griffith, D.A., 2013. Spatial Statistics and Geostatistics. London: Sage. (I found this book a little eccentric but it contains some very good tips on its subject and gives worked examples in R). The following book has a short section of maps as well as other graphics in R (and is also, as the title suggests, good for practical guidance on how to analyse surveys using cluster and stratified sampling, for example): Lumley, T., 2010. Complex Surveys. A Guide to Analysis Using R. Hoboken, NJ: Wiley. 20 Springer publish an ever-growing series of books under the banner Use R! If you are interested in visualization, time-series analysis, Bayesian approaches, econometrics, data mining, …, then you'll find something of relevance at http://www.springer.com/series/6991. But you may well also find what you are looking for for free on the Internet. An Introduction to Mapping and Spatial Modelling in R. © Richard Harris, 2013 12