Basic Grammar: Linear Algebra"

knitr::opts_chunk$set(echo = TRUE,
                      message = FALSE,
                      warning = FALSE,
                      out.width="100%")

d <- seq(from = 1, to = 5, by = 0.5)

Key Points

Elementary Algebra

Four Basic Operations

1 + 2; 1.5 + 2.4
3 - 4; -6 - 7
5 * 6; 23 * 14
7 / 8; -9 / 0.2

; in R is used to connect two independent command lines.

Based on the above command, please solve the following question:

$$\frac{\frac{365}{12}}{\frac{28 + 24}{10} - (36 + 72) \times 5}$$


(365 / 12) / ((28 + 24)/10 - (36 + 72) * 5)

Complex Algebra

sqrt(9)
10^2
5%%3 #remainder
log(11)
log10(100)
exp(12)

trigonometric functions:

sin(pi)
cos(pi)
tan(pi/2)

acos(1)
asin(1)
atan(0.5)

Scientific Notation

tan(pi/2)

A number is written as a real number "a" multiplied by 10 to the power of "n". Here, E (exponential) is used to represent the power of 10. Here are some examples:

$$200 = 2e + 2, $$ $$0.002 = 2e-3,$$ $$333.3 = 3.333e + 2,$$ $$-45,000 = -4.5e + 4.$$

What does 2.8e + 10 represent?


format(2.8e+10, scientific = FALSE, big.mark = ',')

Boolean Algebra

Here are some examples:

1 == 2
1 != 2
1 > 2
1 < 2
1 >= 2
1 <= 2
1 & 2 > 2
1 | 2 > 2

What is the result of the following formula?

3 > 2 > 1
(3 > 2) & (2 > 1)

Many programming language beginners may feel uncomfortable when using double equal signs (==) to represent "equals". However, it can be viewed as a member of ==, !=, >=, <=.

Vectors

In R, vectors are also the most basic way of representing data.

Variables can be viewed as "conceptually meaningful vectors".

Vector Representation

a <- c(11, 12, 13, 14, 15)

a[2]
a[5]

b <- 10 * 1:5
b

# How about the result?
c(1.5:3)

How to represent $\vec {b}=(1, 3, 4, 5, 6, 7, 9)$?


b <- c(1, 3:7, 9)

Repeat the above example:

a <- seq(from = 1, to = 5)
a

The convenience of seq() is that the step size can be defined:

c <- seq(from = 1, to = 5, by = 3)
c

d <- seq(from = 1, to = 5, by = 0.5)
d

Another function of seq() is to reflect the position of the element.

For example: how to choose the element of d at the even-numbered position?


Hint: Determine the index → Find out whether each bit in the index is an even number (whether it can be divisible by 2), it is marked with TRUE → On the basis of the second step, according to the rule of "TRUE, FALSE is not reserved", the elements with even numbers in the vector are reserved as a new vector.

d[seq(d) %% 2 == 0]

Four Arithmetic Operations of Vector

a + b
a - b
a * b
a / b

When the vectors used for calculation are of unequal length, the elements of the shorter vector will be cycled through in sequence, as shown in the following example:

a
e <- c(1, 2)

a * e

Cross-product in R can actually be used for more than just manipulating data.

For example, this functionality can be used to calculate the combination of the ten Heavenly Stems and twelve Earthly Branches in Chinese astrology using R.

tiangan <- c("甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸")
dizhi <- c("子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥")

Note: As you all know, there are ten Heavenly Stems and twelve Earthly Branches in Chinese astrology. There are a total of 120 combinations, but our ancestors selected 60 of them to form the basis of the Chinese zodiac. The selection method is to pair odd numbers with odd numbers and even numbers with even numbers.

tg_yang <- tiangan[seq(tiangan) %% 2 == 1]
tg_yin <- tiangan[seq(tiangan) %% 2 == 0]

dz_yang <- dizhi[seq(dizhi) %% 2 == 1]
dz_yin <- dizhi[seq(dizhi) %% 2 == 0]

outer(tg_yang, dz_yang, FUN = paste0)
outer(tg_yin, dz_yin, FUN = paste0)

Properties of Vectors

a

#length
length(a)

#best value
max(a)
min(a)

#average value
mean(a)

#median
median(a)

#variance
var(a)

These commands can be used commonly in subsequent data-based variable operations.

Matrix

$$ A_{2\times2} = \left(\begin{array}{cc} 1 & 3\ 2 & 4 \end{array}\right) $$

A <- matrix(1:4, nrow = 2)
A

-R:

A[1, 2]
A[1, ]
A[ , 2]

Single Matrix Operations

B <- matrix(1:6, nrow = 3)
B

dim(B)
t(B)
t(t(B))

t(t(B)) is a nested approach.

Matrix Operations

In R, you can perform this operation by using %*% to concatenate two vectors that meet the defined properties.

B
C <- matrix(7:12, nrow = 3)

B + C
B - C

10 * B

D <- matrix(1:6, nrow = 2)
D

dim(B)
dim(D)

B %*% D

System of Equations

Use R to solve a system of three linear equations:

$$ \begin{equation} \begin{cases} x + y + z = 6, \ 3x + 2y + 4z = 9, \ 2x + 2y - 6z = 4. \end{cases} \end{equation} $$

$$ \begin{align} \vec{b} =& \vec{E}\vec{x},\ \begin{bmatrix} 6 \ 9 \ 4 \end{bmatrix} =& \begin{bmatrix} 1 & 1 & 1 \ 3 & 2 & 4 \ 2 & 2 & -6 \end{bmatrix} \begin{bmatrix} x \ y \ z \end{bmatrix} \end{align}. $$

E <- matrix(c(1, 3, 2, 1, 2, 2, 1, 4, -6), nrow = 3)
E

b <- c(6, 9, 4)

x <- solve(E, b)
x

One direct application of solving systems of equations is to solve regression equations.

Regression equations can typically be expressed in terms of linear algebra.

For example, a basic regression equation can be written in the following form, which is very similar to the system of equations we solved above:

$$ \begin{align} y& =& &\beta_0 + \beta_1x_1 + \beta_2x_2 + ... + \beta_nx_n + \epsilon\ \vec{y}& =& &\vec{x}\vec{\beta} + \vec{\epsilon}.\ \uparrow& & &\uparrow\uparrow\ \vec{b}& =& &\vec{E}\vec{x} \end{align} $$

Summary



Try the drhur package in your browser

Any scripts or data that you put into this service are public.

drhur documentation built on May 31, 2023, 6:03 p.m.