Beruflich Dokumente
Kultur Dokumente
Ott Toomet
November 23, 2014
R has superb graphical capabilities and is very well suited for solving a large
variety of problems numerically. Here we introduce a few concepts.
> dtriangle(1)
[1] 0
> dtriangle(1/3)
[1] 1.333333
1.1
Plotting
The main command for plotting is plot(). Lets plot the triangular density
function:
1.0
0.0
0.5
dtriangle
1.5
2.0
0.0
0.2
0.4
0.6
0.8
1.0
This command plots function (dtriangle) in the interval [0,1]. This command also allows to plot data (numbers). For instance, lets plot a function with
some noise added on it:
> x <- 1:10
> y <- sqrt(x) + 0.2*rnorm(10)
> plot(y ~ x, main="Noisy square root")
3.0
2.5
2.0
1.0
1.5
10
Here are several things to observe. First, we create vector x of length 10.
Next, we take square root of it (this gives us another vector of length 10) and add
normal random numbers to it: rnorm(10) draws 10 numbers from the standard
normal distribution, and finally, we add these together. Plot command in this
form plots y (vertically) against x (horizontally). Both x and y must be vectors.
We also use argument main="", that adds a main title to the figure.
If plotting few points like above, it is appropriate to use points plot. In case
of much more data, you may want to use lines:
> x <- 1:100
> y <- sqrt(x) + 0.2*rnorm(100)
> plot(y ~ x, type="l", main="Noisy square root")
10
20
40
60
80
100
The argument type="l" does not use points but draws lines.
1.2
Numerical Integration
One should always prefer analytic integrals. However, all too often you cannot
do that. In those cases, numeric integration is handy. In R, you can take defined
integrals with the command integrate(f, lower, upper). This takes integral
R2
over f from lower to upper. For instance, lets compute 0 x dx:
> fcn <- function(x) {
+
x
+ }
> integrate(fcn, 0, 2)
2 with absolute error < 2.2e-14
First, we define function fcn which simply returns its argument. Second,
we integrate this function from 0 to 2. The result is 2, and we also learn
something about the precision of numerical calculations. Another example: was
our triangular density function correct? Does it integrate to unity in over the
interval [0,1]?
> integrate(dtriangle, 0, 1)
1 with absolute error < 1.1e-14
Indeed. That was correct.
If you want to use the result returned by integrate() for further calculations, it is important to know that this is not a number but a more complex
4
object.1 You can get the result, a simple number, by extracting its component
value like this:
> int <- integrate(dtriangle, 0, 1)
> z <- int$value
Now z is a number which you can manipulate in all ways:
> log(z)
[1] 0
Logarithm of 1 is indeed 0.
The previous plotting examples only work on vectorized functons, i.e. on functions that accept a vector argument and return the corresponding vector of
function values. Sometimes we have a function that only accepts a single argument, for instance because integrate() is not vectorized. In that case it is
most straightforward to store all the individual function values in a vector, and
thereafter to plot that vector.
R x Lets compute and plot the cdf of the triangular
density function F (x) = 0 f (s) ds where f () is the triangular density:
>
+
+
+
>
>
>
+
+
>
>
1 This is literally and object (of class integrate) in the sense of object-oriented programming.
0.0
0.2
0.4
0.6
0.8
1.0
Triangular CDF
0.0
0.2
0.4
0.6
0.8
1.0
First we define the function cdf that integrates triangluar density from 0 to
x. Note that this function returns not the integrate-object but just the numeric
value. Next, we create a vector of x-s, 100 equally spaced values between 0 and
1. We also create an empty y-vector of the same length as x. Third, we do the
calculations. Index i goes through all the values from 1 to 100 (this is the length
of x), computes cdf of the corresponding x[i] and stores it into y[i]. Now we
have a vector of x and corresponding cdf, and plot these. Finally, abline draws
a 45 line with intercept 0, slope 1, and line type 3dotted line. We see a cdf
that rises slowly in both tails but rather fast in the middle. It crosses the 45
line at 0.5 (why?).