The following code creates in R the 5×5 matrix shown below.
[11121314152122232425313233343541424344455152535455]> m = rbind(c(11,12,13,14,15), c(21,22,23,24,25), c(31,32,33,34,35), c(41,42,43,44,45), c(51,52,53,54,55))
Using the above, write R code to generate the following sub-matrices of m
:
the sub-matrix of elements in rows 3, 4, or 5 and in columns 1 or 2
the sub-matrix formed by deleting columns 2 and 4
the sub-matrix of rows whose first column element has an even ten's digit
the sub-matrix of elements in column 5 that correspond to an element in the same row and the first column that is less than 50. (Hint: the result should be a column matrix, not a vector.)
# (a) > m[3:5,1:2] [,1] [,2] [1,] 31 32 [2,] 41 42 [3,] 51 52 # (b) > m[,-c(2,4)] [,1] [,2] [,3] [1,] 11 13 15 [2,] 21 23 25 [3,] 31 33 35 [4,] 41 43 45 [5,] 51 53 55 # (c) > m[(m[,1] %/% 10) %% 2 == 0,] [,1] [,2] [,3] [,4] [,5] [1,] 21 22 23 24 25 [2,] 41 42 43 44 45 # (d) > m[m[,1]<50,5,drop=FALSE] [,1] [1,] 15 [2,] 25 [3,] 35 [4,] 45
Use R to determine the matrix product (in the traditional mathematical sense) of the following matrices:
A=[782591463] and B=[13−17019−3−5]There are many ways to construct the matrices in question before multiplying them together. Below, we use the matrix()
function to create matrix A and the cbind()
function to create matrix B. Note that byrow=FALSE
in the matrix()
function by default, so we don't have to specify it when we list the matrix contents by column. Also remember there is a rbind()
function we could have used too.
> A = matrix(c(7,5,4,8,9,6,2,1,3),nrow=3) > B = cbind(c(13,0,-3),c(-17,1,-5)) > A %*% B [,1] [,2] [1,] 85 -121 [2,] 62 -81 [3,] 43 -77
Matrices can be used to solve systems of linear equations. For example, one can solve for x, y, and z in the system of equations ax+by+cz=pdx+ey+fz=qgx+hy+iz=r by finding the product [xyz]=[abcdefghi]−1⋅[pqr] Use a similar calculation to solve the following system:
2x−y+5z+w=−33x+2y+2z−6=−32x+3y+3z−w=−475x−2y−3z+3w=49> M = rbind(c(2,-1,5,1), c(3,2,2,-6), c(1,3,3,-1), c(5,-2,-3,3)) > C = rbind(-3,-32,-47,49) > solve(M) %*% C [,1] [1,] 2 [2,] -12 [3,] -4 [4,] 1 Alternatively, one can simply execute the following and get the same answer: > solve(M,C)
So x=2,y=−12,z=−4, and w=1.
Suppose the following matrix is defined in R: A=[113526−2−1−3]
Use R to verify that A3=0 where 0 is a 3×3 matrix with every entry equal to 0.
Use R to create a new matrix B equal to A, except that its third column has been replaced by the sum of its second and third columns.
Check (a) with
A %*% A %*% A
For (b), the matrix B
should display as the following:
> B [,1] [,2] [,3] [1,] 1 1 4 [2,] 5 2 8 [3,] -2 -1 -4
Create a 6×10 matrix of random integers chosen from 1,2,…,10 by executing the following two lines of code:
set.seed(75) m = matrix( sample(10, size=60, replace=T), nrows=6)
Use R to find the number of entries in this matrix which are greater than 4.
sum(m>4)
Create the following patterned matrices. In each case your solution should make use of the special form of the matrix -- this means that the solution should easily generalize to creating a larger matrix with the same structure and should not involve typing in all the entries of the matrix.
(a) [0123412340234013401240123] (b) [01234567891234567890⋮⋮⋮⋮⋮⋮⋮⋮⋮⋮89012345679012345678]
(c) [087654321108765432210876543321087654432108765543210876654321087765432108876543210]
Hint: Use the outer()
function and the %%
operator.
Use R to calculate the following sums:
(a) 20∑i=15∑j=1i4(3+ij) (b) 10∑i=1i∑j=1i4(3+ij)
(a) 89912.02; (b) 6944.743
Suppose one wishes to simulate how frequently a new dam will overflow, which depends on the number of rainy days seen in a row. We might start by making observations over some sequence of consecutive days of whether each day was rainy (R) or sunny (S). What we observed is shown below.
RRRRRRRRSSSSSSSRSSSSSRSSSSRRRRRRRRRRSRRRRSSSSSS
It appears that exactly half of the days observed were rainy. As such, one might try to simulate similar data by essentially "flipping a coin" for each day so that the chance that it rains on any particular day is 50%. This would be easy to do in R with the following:
s = sample(c("R","S"),size=28,replace=TRUE,prob=c(0.5,0.5)) paste0(s,collapse="")
Below is some data simulated in this way:
RRRSSSRRRRSSSRRRSRSRRRRSSSRSSRRRSRSSSSSRRSSRRRS
Notice how the simulated sequence doesn't look quite like the actual observed data. In the original data, there are long sequences of R's and S's, while the observed data has much shorter sequences. Granted, this is just one sequence, so additional observations and simulations are made. Strangely, the same pattern is seen in these as well. The observations always seem to have longer subsequences of R's and S's than the simulated data.
Upon reflection, you might realize that one possible reason for the discrepancy is that the probability that any particular day is sunny or rainy is not independent of whether or not the previous day was sunny or rainy. In particular, it appears that if a day is rainy, the probability the next day is rainy is higher than if it was sunny. Likewise, if a day is sunny, the probability the next day is sunny is higher than if it was rainy.
Based on all your observations, it appears that consecutive days have the same weather 90% of the time. With this, we can build the following model:
Such a model is called a Markov chain, named after Andrey Markov, a Russian mathematician who first published a paper on the topic in 1906.
In the model above, there are two "states" (R and S). However, a general Markov chain can have many more. One of the more interesting characteristics of a Markov chain is that the probability that you land in any one state after some number of iterations (i.e., days in the example discussed) can be found through a power of an associated transition matrix.
Let's see how this works...
Suppose it is rainy today (day 1), and you want to know what is the probability that it is rainy or sunny tomorrow (day 2). Of course, the answer is simple -- 90% chance of rain, 10% chance of sun.
Suppose however that you want to know the probability that it is rainy or sunny the day after that (day 3). We can find such a probability using our basic probability rules. For the calculations that follow, suppose Ri is the event "day i was rainy" and Si is the event "day i was sunny". Then notice:
P(R3)=P((R2&R3)or(S2&R3))=P(R2&R3)+P(S2&R3)=P(R2)P(R3|R2)+P(S2)P(R3|S2)=(0.90)(0.90)+(0.10)(0.10)=0.82We could of course use the fact that R and S are complements to find P(S3), but let's find it in a manner similar to the above:
P(S3)=P((R2&S3)or(S2&S3))=P(R2&S3)+P(S2&S3)=P(R2)P(S3|R2)+P(S2)P(S3|S2)=(0.90)(0.10)+(0.10)(0.90)=0.18Similar calculations reveal the probabilities for day 4:
P(R4)=(0.90)(.82)+(0.10)(.18)[=0.756]P(S4)=(0.10)(0.82)+(0.90)(0.18)[=0.244]More generally, we have the following relationship:
P(Ri+1)=0.90⋅P(Ri)+0.10⋅P(Si)P(Si+1)=0.10⋅P(Ri)+0.90⋅P(Si)Which we could express using matrices with
[P(Ri+1)P(Si+1)]=[0.900.100.100.90]⋅[P(Ri)P(Si)]The middle matrix above (i.e., the one with the 0.90's and 0.10's in it) is called the transition matrix for this Markov chain. It can be applied over and over again to find the probabilities associated with both states after any number of days.
With all of this in mind, write a function p.of.rain()
that if it rains on day 1, returns the probability that it also rains on both days 7 and 8.
Remember the events of raining on day 7 and raining on day 8 are not independent!