100% found this document useful (1 vote)
70 views6 pages

STSA 3732: Tutorial 2 (MEMO) : Matrix C Matrix C Matrix C Matrix C Matrix C Matrix C Matrix C Matrix C

This document provides examples of creating data objects like vectors, matrices, and data frames in R and performing operations on them like transposes, sums, and standard deviations. It includes 9 questions with multiple parts each that have the learner: 1) Create various matrices and vectors from given values, 2) Perform operations like addition and multiplication on matrices, 3) Generate matrices and vectors using functions instead of hardcoded values, 4) Manipulate a data frame by calculating sums and standard deviations. The key skills covered are data object creation and basic linear algebra operations in R.

Uploaded by

Presha Marisana
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
100% found this document useful (1 vote)
70 views6 pages

STSA 3732: Tutorial 2 (MEMO) : Matrix C Matrix C Matrix C Matrix C Matrix C Matrix C Matrix C Matrix C

This document provides examples of creating data objects like vectors, matrices, and data frames in R and performing operations on them like transposes, sums, and standard deviations. It includes 9 questions with multiple parts each that have the learner: 1) Create various matrices and vectors from given values, 2) Perform operations like addition and multiplication on matrices, 3) Generate matrices and vectors using functions instead of hardcoded values, 4) Manipulate a data frame by calculating sums and standard deviations. The key skills covered are data object creation and basic linear algebra operations in R.

Uploaded by

Presha Marisana
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/ 6

STSA 3732: Tutorial 2 (MEMO) 2 March 2022

Question 1
1.1 Create the appropriate data objects, either a vector or matrix, for the following in R:

" # 3 2  0 3 " #


3.4 2    −3 6
A= , C = 1 1  , D = 1 2 , X= ,
   
5 −2.5 1 4
7 4  2 4
   
  
" # " # 1 4
7 1 4 8.4 7.7 4.2    
Y= , Z= , b = 3 , d = 2.
   
1 2 5 5.1 4.4 5.1
5 10
   
   

A <- matrix(c(3.4,5,2,-2.5), ncol=2)


C <- matrix(c(3,1,7,2,1,4), ncol=2)
D <- matrix(c(0,1,2,3,2,4), ncol=2)
X <- matrix(c(-3,1,6,4), ncol=2)
Y <- matrix(c(7,1,1,2,4,5), ncol=3)
Z <- matrix(c(8.4,5.1,7.7,4.4,4.2,5.1), ncol=3)
b <- matrix(c(1,3,5), ncol=1)
d <- matrix(c(4,2,10), ncol=1)

1.2 Calculate C + D.

C+D

## [,1] [,2]
## [1,] 3 5
## [2,] 2 3
## [3,] 9 8

1.3 Get the transpose of C.

t(C)

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


## [1,] 3 1 7
## [2,] 2 1 4

1.4 Get the transpose of d .

t(d)

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


## [1,] 4 2 10

1.5 Calculate b ′D. (Hint: use * for element-wise multiplication and %*% for matrix multiplication.)

1
t(b)%*%D

## [,1] [,2]
## [1,] 13 29

1.6 Calculate C′D.

t(C)%*%D

## [,1] [,2]
## [1,] 15 39
## [2,] 9 24

1.7 Calculate 4d ′Y′.

4*t(d)%*%t(Y)

## [,1] [,2]
## [1,] 280 232

1.8 Calculate Y′Z.

t(Y)%*%Z

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


## [1,] 63.9 58.3 34.5
## [2,] 18.6 16.5 14.4
## [3,] 59.1 52.8 42.3

1.9 Calculate A−1 X.

solve(A)%*%X

## [,1] [,2]
## [1,] -0.2972973 1.2432432
## [2,] -0.9945946 0.8864865

Question 2
2.1 Create the following matrices in R:
1 6  6 1
  
1 7  7 1
   

A = 1 8  , B =  8 1 .
   

1 9  9 1
   
1 10 10 1
   
  

2
A <- matrix(c(1,1,1,1,1,6,7,8,9,10), ncol=2)
B <- matrix(c(6,7,8,9,10,1,1,1,1,1), ncol=2)

Aletrnatively:

A <- matrix(c(1,6,1,7,1,8,1,9,1,10), byrow=T, ncol=2)


B <- matrix(c(6,1,7,1,8,1,9,1,10,1), byrow=T, ncol=2)

2.2 Calculate A + B.

A+B

## [,1] [,2]
## [1,] 7 7
## [2,] 8 8
## [3,] 9 9
## [4,] 10 10
## [5,] 11 11

2.3 Get the transpose of A.

t(A)

## [,1] [,2] [,3] [,4] [,5]


## [1,] 1 1 1 1 1
## [2,] 6 7 8 9 10

2.4 Get the transpose of (B′B) −1 .

solve(t(B)%*%B)

## [,1] [,2]
## [1,] 0.1 -0.8
## [2,] -0.8 6.6

Question 3
3.1 Create appropriate data objects for the following in R. Do not type the values out or copy them in any
way. Use functions to generate these values.

3
600
 
580
 
560
 
  1 1 6  1 1 1 
540  
1 2 5  32 4 8 
     
520
 
1 3 4  243 9 27 
   
w = 500 , Y =  , Z =  .
 
1 4 3 1024 16 64 

480
 
1 5 2 3125 25 125
   
460
 
1 6 1 7776 36 216
   
 
440   
 
420
 
400
 
 

w <- seq(600, 400, by=-20)


Y <- matrix(c(rep(1,6),1:6,6:1), ncol=3)
Z <- matrix(c((1:6)^5,(1:6)^2,(1:6)^3), ncol=3, byrow=F)

3.2 Determine 2w + 30.

2*w+30

## [1] 1230 1190 1150 1110 1070 1030 990 950 910 870 830

3.3 Determine the transpose of the matrix Z and store the second row of the transposed matrix in a vector
named x .

x <- t(Z)[2,]
x

## [1] 1 4 9 16 25 36

3.4 Determine the inverse of Z′Z.

solve(t(Z)%*%Z)

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


## [1,] 2.684143e-06 0.0005670459 -0.0001886165
## [2,] 5.670459e-04 0.1367997553 -0.0429358447
## [3,] -1.886165e-04 -0.0429358447 0.0138302100

Question 4
4.1 Create a matrix with the values 1, 5, 2 and −3 in the first column, 1, 2, 3 and 7 in the second column, 3,
6, 7 and 5 in the third column and 6, 8, 11 and −7 in the fourth column.

4
Q6.1 <- matrix(c(1,5,2,-3,1,2,3,7,3,6,7,5,6,8,11,-7), ncol=4)

4.2 Replace the fourth column of the matrix above in (6.1) by the sum of the first and third columns.

Q6.1[,4] <- Q6.1[,1] + Q6.1[,3]


Q6.1

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


## [1,] 1 1 3 4
## [2,] 5 2 6 11
## [3,] 2 3 7 9
## [4,] -3 7 5 2

4.3 Transpose the matrix in (6.2).

Q6.3 <- t(Q6.1)


Q6.3

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


## [1,] 1 5 2 -3
## [2,] 1 2 3 7
## [3,] 3 6 7 5
## [4,] 4 11 9 2

4.4 Determine the mean of the third column of the matrix created in (6.3).

mean(Q6.3[,3])

## [1] 5.25

Question 5
Consider the following data set containing 5 observations for each of the variables named X1, X2 and X3:

X1 X2 X3
10 21 15
9 27 12
7 22 12
11 24 10
15 13 11

5.1 Create a data frame named my.data containing the data shown in the table above.

my.data <- data.frame(X1=c(10,9,7,11,15),


X2=c(21,27,22,24,13),
X3=c(15,12,12,10,11))
my.data

5
## X1 X2 X3
## 1 10 21 15
## 2 9 27 12
## 3 7 22 12
## 4 11 24 10
## 5 15 13 11

5.2 Determine the sum of X1.

attach(my.data)
sum(X1)

## [1] 52

5.3 Determine the standard deviation of X2.

sd(X2)

## [1] 5.22494

5.4 Determine the sum of all the observations of X1 and X3.

sum(X1,X3)

## [1] 112

Alternative solution:

sum(X1+X3)

## [1] 112

You might also like