Announcement

GitHub account

Let’s first setup a GitHub academic account. Go to GitHub Education to get your student benifit (you need to use your tulane.edu email address).

Once you have got your GitHub id, please tell me through email or simply add that to your self-introduction on Canvas. Your GitHub id will then be invited to the course GitHub organization.

Our course has a GitHub page now. Please take a look at tulane-math7360.github.io and its source code at https://github.com/tulane-math7360/tulane-math7360.github.io. You could check all the development history of this website through the commit history.

R

Install R on your computer. This is the workhorse for R programs.

RStudio

(Optional) Install RStudio on your computer. This is the IDE(Integrated Development Environment) for working with R. It is recommended that you select an IDE (e.g., RStudio) for your R program development. However, you are welcome to work in whichever environment you prefer.

Run your “first” R code

Open RStudio and you should see a window similar to the one below:

The big window on the left with “Console” is the R console that functions the same as the consoles you get from opening R (not RStudio). You can run R command in the console directly. Try with

2 + 3
## [1] 5

Now you see that you can use R as a calculator!

a <- 2 + 3
If you just run the above line the console, you will not see any pop-up. However, your calculation is stored in the element ‘a’. You can find it in the upper-right window of your RStudio under the Environment tab. Take a look:

And, of course you can output the value of a by

a
## [1] 5

The above screenshot also reveals that I use RStudio for writing RMarkdown files which are the source code for all of your lecture slides. We will go through RMarkdown and git commands in next week’s lab session. Let’s finish today’s lab session by plotting some figures (the saying “a figure is worth a thousand words” should echo now). We will draw \(1000\) samples from a normal distribution with mean \(10\) and variance \(100\) (i.e., N(10, 100)).

#Let's take a look at the PDF (probability density function) first
x <- 0:2000 * 0.05 - 50
y <- dnorm(x, mean = 10, sd = sqrt(100))
# X spans over [-50, 50] and y store the pdf value for each element in x.
# The following command generates a line plot
plot(x, y, type = "l")

# search the R Documentation for this "plot" function in the lower right window.
Now, let’s search the help documentation of the default “plot” function in R. Locate the “Help” tab in the lower-right window of your RStudio app.

# Let's draw some random variables from the distribution
normal.samples <- rnorm(1000, 10, 10)  # avoid using sample as the variable name because it's already used for a built-in function.  The bad behavior of R (python shares this overriding problem too)
# Produce a histogram of the random samples
hist(normal.samples)

The above histogram has the default “Frecuency” as the Y axis. Let’s change it to “Density”

hist(normal.samples, probability = TRUE)

Now let’s place the line plot over the histogram.

hist(normal.samples, probability = TRUE)
lines(x, y, col="red", lwd=4)

#you may want to try the function "curve" too.  Go to its help page to find the usage.

This concludes our lab 1. Please let me know if you run into any trouble.