Ronald Wesonga (Ph.D)
18 Sep 2022
We can write fractions: \(\frac{2}{3}\). We can also handle things like estimated population growth rate, e.g., \(\hat{\lambda}=1.02\). And, \(\sqrt{4}=2\).
\[\alpha, \beta, \gamma, \Gamma\]
\[a \pm b\] \[x \ge 15\] \[a_i \ge 0~~~\forall i\]
\[A_{m,n} = \begin{pmatrix} a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\ a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m,1} & a_{m,2} & \cdots & a_{m,n} \end{pmatrix}\]
The binomial probability: \[f(y|N,p) = \frac{N!}{y!(N-y)!}\cdot p^y \cdot (1-p)^{N-y} = {{N}\choose{y}} \cdot p^y \cdot (1-p)^{N-y}\]
To calculate the mean of observations of variable , you can use: \[\bar{x} = \frac{1}{n} \sum_{i=1}^{n}x_{i}\]
Note that this equation looks quite nice above where it’s in display math mode. It is more compact but not quite as nice looking if we present it using inline mode, e.g., \(\bar{x} = \frac{1}{n} \sum_{i=1}^{n}x_{i}\).
Let’s do the same with the equation for variance. First the inline version, which is \(\sigma^{2} = \frac{\sum\limits_{i=1}^{n} \left(x_{i} - \bar{x}\right)^{2}} {n-1}\). And then the display mode version: \[\sigma^{2} = \frac{\sum_{i=1}^{n} \left(x_{i} - \bar{x}\right)^{2}} {n-1}\]
Next, it’s good to look at the equation for covariance to see how it is just a generalization of variance to two variables. An inline version of the equation is \(cov_{x,y} = \frac{\sum\limits_{i=1}^{n}{(x_i-\overline{x}) \cdot (y_i-\overline{y})} }{n-1}\). And, the display mode is: \[cov_{x,y} = \frac{\sum\limits_{i=1}^{n}{(x_i-\overline{x}) \cdot (y_i-\overline{y})} }{n-1}\]
And, finally, we’ll end with the standard deviation. Here’s the inline version, \(\sigma = \sqrt{\frac{\sum\limits_{i=1}^{n} \left(x_{i} - \bar{x}\right)^{2}} {n-1}}\). And here’s the display version. \[\sigma = \sqrt{\frac{\sum\limits_{i=1}^{n} \left(x_{i} - \bar{x}\right)^{2}} {n-1}}\]
There are helpful online editors to help you learn code for various equations you might want to include. There is one at: http://visualmatheditor.equatheque.net/VisualMathEditor.html. You can work out the code there and then copy it over to your RMarkdown document in between dollar signs (1 or 2 on either end depending on whether you want the equation in line or in display mode).
Hint: To make sure R respects vector dimensions, save them as matrices
## [1] 3 1
## [1] 3 1
## [1] 3 1
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [1] 1
## [1] 1 2 3
## [1] 1 4
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 2 0
## [3,] 0 0 3
## [,1] [,2] [,3]
## [1,] 1 1 1
## [2,] 1 1 1
## [3,] 1 1 1
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
## [3,] 5 6
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 2 0
## [3,] 0 0 3
## [,1] [,2] [,3]
## [1,] 2 5 8
## [2,] 6 9 12
## [,1] [,2] [,3]
## [1,] 0 -1 -2
## [2,] 2 1 0
## [,1] [,2] [,3]
## [1,] 1 6 15
## [2,] 8 20 36
## [,1] [,2] [,3]
## [1,] 1 0.6666667 0.6
## [2,] 2 1.2500000 1.0
## [,1] [,2] [,3]
## [1,] 1 8 243
## [2,] 16 625 46656
## [,1]
## [1,] 5
## [2,] 7
## [3,] 9
## [,1]
## [1,] -3
## [2,] -3
## [3,] -3
## [,1]
## [1,] 4
## [2,] 10
## [3,] 18
## [,1]
## [1,] 0.25
## [2,] 0.40
## [3,] 0.50
## [,1]
## [1,] 4
## [2,] 25
## [3,] 216
## [1] 2 3
## [1] 2 3
## [,1] [,2]
## [1,] 22 28
## [2,] 49 64
## [,1] [,2] [,3]
## [1,] 9 19 29
## [2,] 12 26 40
## [3,] 15 33 51
## [,1] [,2] [,3]
## [1,] 9 12 15
## [2,] 19 26 33
## [3,] 29 40 51
## [,1] [,2]
## [1,] 22 49
## [2,] 28 64
## [,1] [,2] [,3]
## [1,] 4 5 6
## [2,] 8 10 12
## [3,] 12 15 18
## [,1]
## [1,] 32
## [,1] [,2]
## [1,] 14 32
## [,1] [,2] [,3]
## [1,] 1 6 15
## [2,] 2 8 18
## [,1] [,2] [,3]
## [1,] 3 9 15
## [2,] 8 16 24
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
## [,1] [,2] [,3]
## [1,] 1 -5.820766e-11 0
## [2,] 0 1.000000e+00 0
## [3,] 0 0.000000e+00 1
## [,1] [,2] [,3]
## [1,] 1 0.000000e+00 0.000000e+00
## [2,] 0 1.000000e+00 1.164153e-10
## [3,] 0 2.328306e-10 1.000000e+00
## eigen() decomposition
## $values
## [1] 3 2 1
##
## $vectors
## [,1] [,2] [,3]
## [1,] 0 0 1
## [2,] 0 1 0
## [3,] 1 0 0
## eigen() decomposition
## $values
## [1] -1.323489+0.000000i 1.118697+0.444746i 1.118697-0.444746i
##
## $vectors
## [,1] [,2] [,3]
## [1,] -0.08708167+0i -0.1135622+0.6059050i -0.1135622-0.6059050i
## [2,] -0.38344958+0i 0.6560852+0.0000000i 0.6560852+0.0000000i
## [3,] -0.91944723+0i -0.3916733-0.1900713i -0.3916733+0.1900713i
## eigen() decomposition
## $values
## [1] 1.303054e+00 6.281558e-01 2.220446e-16
##
## $vectors
## [,1] [,2] [,3]
## [1,] 0.7717766 0.4634804 0.4353697
## [2,] 0.6105456 -0.7314777 -0.3036023
## [3,] 0.1777495 0.5001262 -0.8475133
N<-100
D<-matrix(rnorm(N),N,1)
E<-matrix(rnorm(N,sd=0.5),N,1)
D<-cbind(D,-D+E)
A<-var(D)
e<-eigen(A)
e## eigen() decomposition
## $values
## [1] 1.7384200 0.1158355
##
## $vectors
## [,1] [,2]
## [1,] -0.6314137 -0.7754462
## [2,] 0.7754462 -0.6314137
## [,1] [,2]
## [1,] 0.7627328 -0.7944617
## [2,] -0.7944617 1.0915227
## [,1] [,2]
## [1,] 0.7627328 -0.7944617
## [2,] -0.7944617 1.0915227
lambda<-sqrt(e$values)
ellipse(c(0,0),A,3,add=FALSE)
points(D[,1],D[,2],pch=19,col=4)
ellipse(c(0,0),A,2,add=TRUE)
ellipse(c(0,0),A,1,add=TRUE)
arrows(0,0,lambda[1]*e$vectors[1,1],lambda[1]*e$vectors[2,1])
arrows(0,0,lambda[2]*e$vectors[1,2],lambda[2]*e$vectors[2,2])
grid()## [,1] [,2]
## [1,] 2.2277406 -0.85127152
## [2,] 1.1501472 -0.03577439
## [3,] 0.4009243 0.80914034
## [1] 0.6168177
## [1] 0.6882345 0.5571864 0.6050323
## [1] 1.25960401 -0.02596852
## [,1]
## [1,] 1
## [2,] 1
## [3,] 1
## [,1]
## [1,] 1.25960401
## [2,] -0.02596852
## [,1] [,2]
## [1,] 2.2277406 -0.85127152
## [2,] 1.1501472 -0.03577439
## [3,] 0.4009243 0.80914034
## [1] 0.8433
## [1] 0.689314
## [1] 0.9183137
## [1] 0.8302494
## [1] -0.7575119
## [,1] [,2]
## [1,] 0.8433000 -0.7575119
## [2,] -0.7575119 0.6893140
## [,1] [,2]
## [1,] 1.0000000 -0.9935502
## [2,] -0.9935502 1.0000000
## [,1]
## [1,] 0.9681366
## [2,] -0.1094568
## [3,] -0.8586797
## [,1]
## [1,] -0.825302999
## [2,] -0.009805867
## [3,] 0.835108866
## [,1]
## [1,] -1.515024
## [1] -1.515024
## [,1] [,2] [,3]
## [1,] 1 1 1
## [2,] 1 1 1
## [3,] 1 1 1
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
## [,1] [,2]
## [1,] 0.8433000 -0.7575119
## [2,] -0.7575119 0.6893140
## [,1] [,2]
## [1,] 0.8433000 -0.7575119
## [2,] -0.7575119 0.6893140
ellipse<-function(mu,Sigma,R,col='red',add=FALSE,xlim=NULL,ylim=NULL,N=1000)
{
# Find coordinates of a circle
t<-seq(0,2*pi,length.out=N)
x<-R*cos(t)
y<-R*sin(t)
# Spectral decomposition of Sigma
e<-eigen(Sigma) # spectral decomposition
P<-e$vectors # eigenvectors
L<-e$values
# Square root matrix
S05<-P%*%sqrt(diag(L))%*%t(P)
# Ellipse cordinates
vec<-cbind(x,y)
vec<-t(vec%*%S05)
x<-vec[1,]+mu[1]
y<-vec[2,]+mu[2]
if (add)
{
points(x,y,type='l',col=col)
}
else
plot(x,y,type='l',col=col,xlim=xlim,ylim=ylim)
}