The function `lln.ani()`

in the **animation** package provides us a
visualization method for the LLN. It plots the sample mean as the sample size grows to check
whether the sample mean approaches to the population mean. Here we make an animation with the
Chi-squared distribution as the population
distribution.

```
library(animation)
ani.options(interval = 0.3)
lln.ani(FUN = function(n, mu) rchisq(n, df = mu), mu = 5, cex = 0.6)
```

The solution, in the case where the needle length is not greater than the width of the strips, can be used to design a Monte Carlo method for approximating the number .

In the **animation** package, the function `buffon.needle()`

can be
used to simulate Buffon’s needle. There are three graphs made in each step: the top one is a
simulation of the scenario, the bottom-left one can help us understand the connection between
dropping needles and the mathematical method to estimate , and the bottom-right one is the
simulation result for each drop.

```
library(animation)
ani.options(nmax = 100, interval = 0.5)
par(mar = c(3, 2.5, 0.5, 0.2), pch = 20, mgp = c(1.5, 0.5, 0))
buffon.needle(mat = matrix(c(1, 2, 1, 3), 2))
```

You can use larger `nmax`

values in the code to drop the needle for more times.

As shown in the Bean Machine article, CLT has a number of variants. This article shows you as long as the conditions of CLT are satisfied, the distribution of the sample mean will be approximate to the Normal distribution when the sample size n is large enough, no matter what is the original distribution.

In the **animation** package, there is a function named `clt.ani()`

.
It shows the distribution of the sample mean when the sample size grows up. The test
`shapiro.test()`

is provided as a measure of normality.

With the parameter `FUN`

in the function `clt.ani()`

you can select distribution which will be
shown in the animation. Here is the example with the Poisson
distribution.

```
library(animation)
ani.options(interval = 0.5)
par(mar = c(3, 3, 1, 0.5), mgp = c(1.5, 0.5, 0), tcl = -0.3)
lambda = 4
f = function(n) rpois(n, lambda)
clt.ani(FUN = f, mean = lambda, sd = lambda)
```

The Cauchy distribution is an example of a distribution which has no mean, variance or higher moments defined, so we cannot apply CLT to this distribution.

```
ani.options(interval = 0.5)
par(mar = c(3, 3, 1, 0.5), mgp = c(1.5, 0.5, 0), tcl = -0.3)
f = function(n) rcauchy(n, location = 0, scale = 2)
clt.ani(FUN = f, mean = NA, sd = NA)
```

The function `quincunx()`

in the **animation** package shows you how
balls bounce left and right as they hit the pins. You can see the height of ball columns in the
bins approximates a bell curve.

```
library(animation)
ani.options(interval = 0.03, nmax = 213)
quincunx()
```

There are many plotting symbols in the **graphics** package. We can set the `pch`

parameter to set
the symbols. See `?points`

for more information.

Below is a figure containing the plot symbols from `pch = 0`

to `25`

along with some
character-based plot symbols. We can use, for example, `plot(x, y, pch = 3)`

for plus signs (`+`

)
in a scatterplot.

The default color palette in R:

```
(pal = palette())
```

```
## [1] "black" "red" "green3" "blue" "cyan" "magenta"
## [7] "yellow" "gray"
```

```
par(mar = rep(0, 4))
pie(rep(1, length(pal)), labels = sprintf("%d (%s)", seq_along(pal),
pal), col = pal)
```

Below are the color palettes in **RColorBrewer** (if you do not want to use **ggplot2**, you should
consider using this package to generate colors for your plots; just do not use the meaningless
`rainbow()`

palette):

```
library(RColorBrewer)
par(mar = c(0, 4, 0, 0))
display.brewer.all()
```

```
# generate 8 colors from the Set2 palette
brewer.pal(8, "Set2")
```

```
## [1] "#66C2A5" "#FC8D62" "#8DA0CB" "#E78AC3" "#A6D854" "#FFD92F"
## [7] "#E5C494" "#B3B3B3"
```

In R, the function `colors()`

returns a vector of 657 color names. When you really
need to specify the color by its name, you are recommended to check out this nice color
chart created by Earl F.
Glynn.

`expression()`

instead of normal text, e.g. `plot(1:10, main = expression(alpha + beta))`

. Below is a demo that
shows you everything about plotting math in R (it was written by the R Core Team; see `?plotmath`

for details):
```
demo(plotmath)
```

If you want to combine multiple mathematical expressions with text, use `paste()`

*inside*
`expression()`

, as in the following.

```
par(mar = c(4, 4, 2, 0.1))
plot(rnorm(100), rnorm(100),
xlab = expression(hat(mu)[0]), ylab = expression(alpha^beta),
main = expression(paste("Plot of ", alpha^beta, " versus ", hat(mu)[0])))
```

Finally, if we want to include variables from an R session in mathematical expressions, and
substitute in their actual values, we can use `substitute()`

.

```
par(mar = c(4, 4, 2, 0.1))
x_mean <- 1.5
x_sd <- 1.2
hist(rnorm(100, x_mean, x_sd),
main = substitute(
paste(X[i], " ~ N(", mu, "=", m, ", ", sigma^2, "=", s2, ")"),
list(m = x_mean, s2 = x_sd^2)
)
)
```

]]>

`ani.pause()`

.
Let’s get started!

The equations that define an egg are:

In the figure below, let’s swap the x axis and y axis for an egg in the vertical direction.

```
t = seq(-pi, pi, by = 0.01)
H = 1
x = H * 0.78 * cos(t/4) * sin(t)
y = -H * cos(t)
par(mar = rep(0, 4))
plot(x, y, type = "l", xlim = c(-1, 1), ylim = c(-1, 1), asp = 1,
col = "orange1", lwd = 5, axes = FALSE)
```

We need a Rotation matrix to draw the rotating egg:

The `ani.pause()`

function is called to pause for a time interval (by default specified in
`ani.options('interval')`

) and flush the current device. We draw a egg with with different angles
in 30 images, and you will see the egg rotating below:

```
library(animation)
egg_rotation = function(H = 1, angle = seq(0, pi * 2, length = 30),
pos = c(0, 0)) {
t = seq(-pi, pi, by = 0.01)
for (i in 1:length(angle)) {
x = H * 0.78 * cos(t/4) * sin(t)
y = -H * cos(t)
# Rotation matrix
x1 = cos(angle[i]) * x - sin(angle[i]) * y + pos[1]
y1 = sin(angle[i]) * x + cos(angle[i]) * y + pos[2]
cols = colors()
flag = sample(1:length(cols), 1)
plot(x1, y1, type = "l", xlim = c(-1, 1), ylim = c(-1, 1),
asp = 1, col = cols[flag], lwd = 8, axes = FALSE)
ani.pause(0.1)
}
}
par(mar = rep(0, 4))
egg_rotation()
```

Another example:

```
library(animation)
egg = function(h = rnorm(1), angle = rnorm(1), pos = rnorm(2)) {
t = seq(-pi, pi, by = 0.01)
for (i in 1:10) {
H = h - h/10 * i
x = H * 0.78 * cos(t/4) * sin(t)
y = -H * cos(t)
# Rotation matrix
x1 = cos(angle) * x - sin(angle) * y + pos[1]
y1 = sin(angle) * x + cos(angle) * y + pos[2]
cols = colors()
flag = sample(1:length(cols), 1)
plot(x1, y1, type = "l", xlim = c(-1, 1), ylim = c(-1, 1),
asp = 1, col = cols[flag], lwd = 8, axes = FALSE)
ani.pause(0.1)
}
}
par(mar = rep(0, 4))
set.seed(123)
for (j in 1:10) {
egg()
ani.pause(1)
}
```

If you want to draw a 3D egg. The **rgl** package can help you do it. The 3D egg is just a
perturbation of a sphere. The function is:

Here we set $c=0.2, b=0.3$.

```
meshgrid <- function(a, b) {
list(x = outer(b * 0, a, FUN = "+"), y = outer(b, a * 0, FUN = "+"))
}
library(rgl)
c = 0.2
b = 1.7
theta = seq(0, 2 * pi, length = 40 * 4)
phi = seq(0, pi, length = 40 * 4)
theta1 = meshgrid(theta, phi)$x
phi2 = meshgrid(theta, phi)$y
x = (1 + c * phi2) * sin(phi2) * cos(theta1)
y = (1 + c * phi2) * sin(phi2) * sin(theta1)
z <- b * cos(phi2)
surface3d(x, y, z, color = rainbow(10))
par3d(zoom = 0.7)
```

`flip.coin()`

in the `heads`

and `tails`

.
Coin flipping is a well-known Bernoulli trial. When
you flip a coin, there are two possible outcomes: `head`

or `tail`

. A fair coin has the probability
0.5 for `head`

by definition.
We toss a fair coin 100 times below.

```
library(animation)
ani.options(nmax = 100, interval = 0.3)
par(mar = c(2, 4, 2, 2))
flip.coin(bg = "yellow")
```

Note the outcome is random, so if you run the code above again, you are likely to see different results, but on average you should get 50 heads and 50 tails in the long run.

The coin here does not have to mean a coin literally. We can generalize it to an object that can
produce $n$ possible outcomes. For example, three outcomes `Head`

, `Stand`

(a coin may stand on the
table) and `Tail`

with probabilities 0.45, 0.1 and 0.45 respectively:

```
ani.options(nmax = 100, interval = 0.3)
par(mar = c(2, 4, 2, 2))
flip.coin(faces = c("Head", "Stand", "Tail"), type = "n", prob = c(0.45,
0.1, 0.45), col = c(1, 2, 4))
```

`vi.grid.illusion()`

and `vi.lilac.chaser()`

can
be used to produce visual illusions.
`vi.grid.illusion()`

function provides illustrations for the Scintillating grid illusion and
Hermann grid illusion. They are two most common types of grid
illusions.

You can see dark dots appear and disappear rapidly at random intersections.

```
library(animation)
vi.grid.illusion()
```

From Hermann grid illusion picture, you can see grey blobs disappear when looking directly at an intersection.

```
vi.grid.illusion(type = "h", lwd = 22, nrow = 5, ncol = 5, col = "white")
```

We can draw a Lilac chaser with the function
`vi.lilac.chaser()`

.

Stare at the center cross for a few (say 30) seconds to experience the illusion.

- A gap running around the circle of lilac discs;
- A green disc running around the circle of lilac discs in place of the gap;
- The green disc running around on the grey background, with the lilac discs having disappeared in sequence.

```
ani.options(nmax = 20)
par(mar = c(1, 1, 1, 1))
vi.lilac.chaser()
```

Don’t worry if you can’t see all the phenomena described. For many illusions, there is a percentage of people with perfectly normal vision who just don’t see it, often for reasons currently unknown.

You can see more illusions created by Kohske in R, which
also illustrated the power of the **grid** package.

`grad.desc()`

. It provides a visual illustration for the process of minimizing a real-valued
function through the Gradient Descent Algorithm.
The two examples below show you how to use the `grad.desc()`

function.
The default objective function in `grad.desc()`

is . The arrows will take
you to the minima step by step:

```
library(animation)
par(mar = c(4, 4, 2, 0.1))
grad.desc()
```

This example shows how the gradient descent algorithm will fail with a too large step length.

To find a local minimum of a bivariate objective function:

```
ani.options(nmax = 70)
par(mar = c(4, 4, 2, 0.1))
f2 = function(x, y) sin(1/2 * x^2 - 1/4 * y^2 + 3) * cos(2 * x + 1 -
exp(y))
grad.desc(f2, c(-2, -2, 2, 2), c(-1, 0.5), gamma = 0.3, tol = 1e-04)
```

```
## Warning: Maximum number of iterations reached!
```

Apparently the arrows get lost eventually. You can replace `gamma=0.3`

with a smaller value and
retry the function.