-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathr_background.tex
127 lines (115 loc) · 3.79 KB
/
r_background.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
\chapter{R Background}
This chapter describes the R programming techniques that we will need in order
to do Bayesian statistics both within R and also by using JAGS.
\section{Vectors}
We will use vectors frequently throughout the course. A vector can be thought
of as a list of numbers. In R, you can create a vector using the function
{\tt c}, which stands for ``concatenate''.
\begin{verbatim}
my_vector = c(12.4, -6.2, 4.04)
\end{verbatim}
You can then examine the contents of the vector by typing its name.
\begin{verbatim}
> my_vector
[1] 12.40 -6.20 4.04
\end{verbatim}
There are various helpful things you can do with vectors. You can get the length
of a vector (the number of elements in it) like so:
\begin{verbatim}
> length(my_vector)
[1] 3
\end{verbatim}
You can do arithmetic
with vectors too, if they're the same length. For example:
\begin{verbatim}
> c(1,2,3)*c(2,5,3)
[1] 2 10 9
\end{verbatim}
If you wanted, you could assign the output to a new variable:
\begin{verbatim}
> x = c(1,2,3)
> y = c(2,5,3)
> z = x*y
> z
[1] 2 10 9
\end{verbatim}
It is important to understand how to access subsets (or ``slices'') of vectors
based on which elements satisfy a certain condition. Here is an example:
\begin{verbatim}
> x = c(1, 2, 3, 0, 10)
> test = x > 3
> test
[1] FALSE FALSE FALSE FALSE TRUE
> y = x[x <= 2]
> y
[1] 1 2 0
> z = sum(x[x <= 2])
> z
[1] 3
\end{verbatim}
This kind of thing will be used frequently in the computational parts of the
course.
\section{Lists}
Lists are a bit like vectors, in that they can contain a lot of information in
a single variable. But instead of just being numbers, lists can contain all
sorts of things. For example, suppose I want a variable/object in an R program
to represent a person. A person can have a name and an age. Here is how to make
a list:
\begin{verbatim}
a_person = list(name="Nicole", age=21)
\end{verbatim}
If you needed to extract certain elements from a list, you can do it using the
\$ operator in R. For example suppose I wanted to extract the {\tt name} variable
from within {\tt a\_person}. I could do this:
\begin{verbatim}
> a_person$name
[1] "Nicole"
\end{verbatim}
and voila. When we use JAGS, a data set will be represented using a list.
So will our JAGS output.
If you have ever learned C, C++, or Matlab, a list in R is basically the same thing as
a ``struct'' in these languages.
If you have learned C++, Java, or Python, a list is
like a ``class'' but with just variables and no functions.
In Python, ``dictionaries''
are also very similar to R lists.
\section{Functions}
In this course you'll need to be able to read and understand simple R
functions, and perhaps write a few. A function is like a machine that takes
an input, does something, and returns an output. You have probably used many
built-in R functions already, like {\tt sum()}.
\begin{verbatim}
# Defining a function called my_function
my_function = function(x)
{
# Do some stuff
result = 3*x + 0.5
return(result)
}
\end{verbatim}
\section{For Loops}
For loops are mostly used to repeat an action many times. Here is an example.
\begin{verbatim}
N = 100
for(i in 1:N)
{
print(i)
}
\end{verbatim}
\section{Useful Probability Distributions}
Since R is a statistics program, it knows about a lot of probability
distributions already. So, if I wanted to use the probability density function
of a normal distribution, instead of having to code something like this:
\begin{verbatim}
f = exp(-0.5*((x - mu)/sigma)**2)/(sigma*sqrt(2*pi))
\end{verbatim}
I can just use the built-in function {\tt dnorm}.
\begin{verbatim}
f = dnorm(x, mean=mu, sd=sigma)
\end{verbatim}
Much easier! If, instead of wanting to evaluate the PDF, I wanted to generate
random samples from a normal distribution, I could use {\tt rnorm}.
\begin{verbatim}
# Generate 1000 samples
samples = rnorm(1000, mean=50., sd=10.)
\end{verbatim}