Fundamentals of Climates HW help (R – Statistics Program)

profilekingofgoddan
R_introduction.pdf

1

A Brief Introduction to R

It is recommended that you install R on your own personal computer. However, if that is not an option, R is available in most (if not all) computer labs on campus.

Installing R

To install R, go to the UCSC Information Technology Services (ITS) website:

http://its.ucsc.edu/software/list.html

Scroll down to the “R – Statistics” tab, and follow the show link. Next follow the R Project Site link. Under the “Getting Started” section follow the link CRAN mirror then scroll down to “USA” and select one of the sites listed – the first one listed is http://cran.cnr.Berkeley.edu/ and is a good choice. Whichever site you choose, follow the instructions found there for the operating system you that are using (i.e. Mac OS, Windows, etc).

Using R

It is recommended that you create a new folder for each of the required assignments and exercises for this class.

To begin a session in R, simply click on the R-icon that has appeared on your computer. In many cases this icon will be on your desktop, but if not go to the usual folder on your computer where you normally find your other applications.

The first thing you will want to do is to go to the folder that you created for your work (e.g. let’s suppose that you are working on assignment #1, and you created a directory called HW1 for this purpose). To change folders (or directories) you just need to use the “change directory” menu item – for Windows this is under the “File” or “Misc” tab near the top left-hand corner of the R window.

To exit R simply type q() or close the R window. You may be asked if you want to “Save the workspace image” – for now you can answer No.

An Example

1. Create a new folder called R_INTRO 2. Go to the canvas website for the class and in the module “Information about R” module,

download into R_INTRO the file brownian_motion.R which you will find on the page called “R Introduction”.

3. Startup R as described above 4. Within R change to the folder R_INTRO using the appropriate tab as described above.

2

5. The folder R_INTRO should contain the sample R program called brownian_motion.R. To run this program simply type source(“brownian_motion.R”) after the prompt in the R window.

6. If everything is working correctly, an image should appear in a separate window that looks something like this:

7. In addition, a copy of this image will have been created in the directory R_INTRO as a jpeg file called brownian_plot.jpg. You can open this image by clicking on it.

8. In this simple example, R is used to create a sequence of one million random numbers that mimic the motion of a pollen grain under the influence of Brownian motion. We will discuss this example further in class. The image created when you run the program shows the histogram of the pollen grain displacements – more on this is class too! The sequence (or time series) of pollen grain displacements in this example is referred to simply as x. If you simply type x after the R prompt the entire time series of numbers will be listed – all one million of them! Try this for yourself.

9. R is a very powerful statistical analysis tool, and many of its features are beyond the scope of what we will cover in this class. However, we will make extensive use of many of the features of R to analysis time series of observations from the earth climate system. As an illustration try the following: (a) Compute the mean of x by typing mean(x) after the R prompt. The answer should be

very close to zero. (b) Compute the variance of x by typing var(x). The answer should be very close to one. (c) Compute the standard deviation of x by typing sd(x). The answer should also be very

close to one. (d) Pick a number between one and one million (e.g. 123456) and find this element in the

time series by typing x[123456]. (e) Now find the square root of the absolute value of the element you identified in (d)

using the command sqrt(abs(x[123456])).

3

(f) You can use R to do simple arithmetic also, just as you might on a pocket calculator. For example, to compute the square root of 26.51 simply use the command sqrt(26.51). To find the natural log of the same number type log(26.51), or to find the log to base 10 you can use log10(26.51). This can be very handy if you need to do some quick calculations as part of your assignments.

10. When you are done with R, simple close the R window or type q() at the prompt. You can answer “No” if asked whether you want to save the workspace.

Getting Help

R has an excellent web browser for providing help. After starting R simply type help.start() to open the help menu in your web browser. You can also find help about a specific command. For example, to find more about the mean command you used in 9(a) above, simple type help(mean) after the R prompt.

Summary

This should be enough information for now to get you started. Each homework assignment will provide you with further specific information as required.