0% found this document useful (0 votes)
6 views9 pages

Tutorial 1

This tutorial covers plotting lines and working with matrices in R, including typing matrices, performing operations, and applying restrictions on lines. It provides examples of creating and manipulating 2x2 and 3x3 matrices, as well as visualizing intersections of lines. The document also includes instructions for using the amsmath package and best practices for coding in R.

Uploaded by

Trân La
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

Tutorial 1

This tutorial covers plotting lines and working with matrices in R, including typing matrices, performing operations, and applying restrictions on lines. It provides examples of creating and manipulating 2x2 and 3x3 matrices, as well as visualizing intersections of lines. The document also includes instructions for using the amsmath package and best practices for coding in R.

Uploaded by

Trân La
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Tutorial 1

STAT394

Contents
1 Introduction 1

2 General Instructions 1

3 Lines 1
3.1 Restrictions on lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

4 Typing matricies 4
4.1 A 2 × 2 matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
4.2 Transpose this matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.3 A 3 × 3 diagonal matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.4 Printing nice matricies from R . . . . . . . . . . . . . . . . . . . . . . . . . . 6

5 Operations with matrices 7

1 Introduction
In this tutorial we construct plots of lines and with restrictions. The tutorial will also cover
typing matrices and working with matrices in R. The amsmath package is used for typing
matricies.

2 General Instructions
Only load those R packages that you effectively use. This will help check if you are reusing
somebody else’s code.
Always use relative paths. You will share your code, and somebody will run it on a different
computer.

3 Lines
A line in R2 is the set of points that satisfy y = β0 + β1 x, with β0 , β1 ∈ R.

1
Figure 1 is my first example. This example was produced by defining the starting and end
points of a segment. Can you think of other ways of defining a straight line, for instance by
stipulating the real numbers β0 and β1 ? How about a point and an angle?
ggplot() +
# geom_hline(yintercept=0) +
# geom_vline(xintercept=0) +
geom_segment(aes(x=-1,
y=1,
xend=1,
yend=-1),
linewidth=2, col="red", alpha=.3) +
geom_point(aes(x=-1, y=1), size=2) +
geom_point(aes(x=1, y=-1), size=2) +
xlim(c(-1.5, 1.5)) +
ylim(c(-1.5, 1.5)) +
coord_fixed() +
labs(x=expression(italic(x)),
y=expression(italic(y))) +
theme(text=element_text(size=12,
family="serif"))

Figure 2 plots the intersection of two lines without the use of the ggplot library.
plot(x=c(-100, 100), y=c(-100, 100), xlab="x",
ylab="y", ylim=c(-10, 10), xlim=c(-10, 10),
main="Intersection of y=x and y=2-x")
grid(nx = NULL, ny = NULL, lty = 2, col = "gray", lwd = 1)
abline(h=0)
abline(v=0)
abline(a=0, b=tan(45*pi/180), col="red", lwd=2)
abline(a=2, b=tan(-pi/4), col="red", lwd=2)
points(x=1, y=1, col="blue", cex=0.8)
text(x=8, y=6, label="y=x")
text(x=-6, y=6, label="y=2-x")

3.1 Restrictions on lines


In the lectures we discussed placing restrictions on the lines. In Figure 3 we have drawn
y = 4x + 2 for x ∈ [2, 4].
ggplot(data = data.frame(x=c(-1, 10), y=c(-1, 20)), aes(x=x, y=y)) +
geom_point(aes(x, y), size=0) +
geom_hline(aes(yintercept = 0)) +
geom_vline(aes(xintercept = 0)) +
geom_abline(aes(slope=4, intercept = 2), color="black", linewidth=.2) +

2
1

0
y

−1

−1 0 1
x

Figure 1: My very first straight line, with several improvements over a plain plot

3
Intersection of y=x and y=2−x

10

y=2−x y=x
5
0
y

−5
−10

−10 −5 0 5 10

Figure 2: Intersection of two lines

geom_segment(x=2, xend=4, y=10, yend=4*4+2, color="red", linewidth=2) +


coord_fixed() +
labs(title=expression(paste(italic(y) == 4 ~ italic(x) + 2,
" such that ",
2<= ~ italic(x) <= 4
)
),
xlab=expression(italic(x)),
ylab=expression(italic(y))) +
theme_tufte() +
theme(plot.margin = unit(c(0,0,0,0), "lines"),
title =element_text(size=8))

4 Typing matricies
4.1 A 2 × 2 matrix
$$
A = \begin{bmatrix}
4 & 2 \\

4
y = 4 x + 2 such that 2 ≤ x ≤ 4

20

15

10
y

0.0 2.5 5.0 7.5 10.0


x

Figure 3: My very first straight line, with several improvements over a plain plot

5
1 & -2
\end{bmatrix}
$$

Produces: " #
4 2
A=
1 −2

4.2 Transpose this matrix


$$
Aˆ{T} = \begin{bmatrix}
4 & 1 \\
2 & -2
\end{bmatrix}
$$

Produces: " #
4 1
AT =
2 −2

4.3 A 3 × 3 diagonal matrix


$$
\begin{bmatrix}
1 & 0 & 0\\
0 & -3 & 0 \\
0 & 0 & 2 \\
\end{bmatrix}
$$

Produces:  
1 0 0
0 −3 0
 

0 0 2

4.4 Printing nice matricies from R


This function prints nicely formatted matricies.
# Writes an R matrix as a matrix
# From https://stackoverflow.com/questions/45591286/for-r-markdown-how-do-i-display-a-m
write_matex2 <- function(x) {
begin <- "\\begin{bmatrix}"
end <- "\\end{bmatrix}"
X <-

6
apply(x, 1, function(x) {
paste(
paste(x, collapse = "&"),
"\\\\"
)
})
paste(c(begin, X, end), collapse = "")
}

As an example:
A<-matrix(c(1, 0, 0, 0, 2, 0, 0, 0, 1), nrow=3, ncol=3)

In text we write:
$$
A=`r write_matex2(A)`.
$$

This produces:  
1 0 0
A = 0 2 0 .
 

0 0 1

5 Operations with matrices


(a <- c(1, 2, 3)) # A vector

## [1] 1 2 3
(b <- c(3, 4, 5)) # A vector

## [1] 3 4 5
dim(a) # Vectors don't have "dim", they have "length"

## NULL
length(a)

## [1] 3
dim(t(t(a))) # But they receive "dim" once transformed

## [1] 3 1
# By default, they are column vectors

7
a <- matrix(a, nrow=length(a), ncol=1) # to be on the safe side
dim(a)

## [1] 3 1
b <- matrix(b, nrow=1, ncol=length(b))
dim(b)

## [1] 1 3
b %*% a # Inner product of compatible matrices

## [,1]
## [1,] 26
(M <- matrix(c(2, 3, 5, 1, -8, 4), nrow=2))

## [,1] [,2] [,3]


## [1,] 2 5 -8
## [2,] 3 1 4
dim(M)

## [1] 2 3
3 * M # Scalar times a matrix

## [,1] [,2] [,3]


## [1,] 6 15 -24
## [2,] 9 3 12
(M1 <- matrix(c(1, 2, 5, 3, 5, 9, 7, 8, 7), nrow = 3))

## [,1] [,2] [,3]


## [1,] 1 3 7
## [2,] 2 5 8
## [3,] 5 9 7
# Colum-wise creation of a matrix

(M2 <- matrix(c(1, 2, 5, 3, 6, 9, 7, 8, 7), nrow = 3, byrow = TRUE))

## [,1] [,2] [,3]


## [1,] 1 2 5
## [2,] 3 6 9
## [3,] 7 8 7
# Row-wise creation of a matrix

M1 %*% M2

8
## [,1] [,2] [,3]
## [1,] 59 76 81
## [2,] 73 98 111
## [3,] 81 120 155
# Determinants
det(M1)

## [1] -8
det(M2)

## [1] -36
# Inverses
solve(M1)

## [,1] [,2] [,3]


## [1,] 4.625 -5.25 1.375
## [2,] -3.250 3.50 -0.750
## [3,] 0.875 -0.75 0.125
solve(M2)

## [,1] [,2] [,3]


## [1,] 0.8333333 -0.7222222 3.333333e-01
## [2,] -1.1666667 0.7777778 -1.666667e-01
## [3,] 0.5000000 -0.1666667 -3.965082e-18
eigen(M1)

## eigen() decomposition
## $values
## [1] 16.8815273 -4.0000000 0.1184727
##
## $vectors
## [,1] [,2] [,3]
## [1,] -0.4211382 -0.6396021 -0.8180022
## [2,] -0.5542791 -0.4264014 0.5587920
## [3,] -0.7179257 0.6396021 -0.1364692

You might also like