Problem 1: Numerator Relationship Matrix

The following pedigree is given

Calf Sire Dam
4 1 2
5 3 2
6 4 5

The pedigree can be read from the file

https://charlotte-ngs.github.io/lbgfs2022/data/ped_num_rel_mat.csv 

Compute the numerator relationship matrix \(A\) for the given pedigree. Recall from the course notes that elements of matrix \(A\) are computed differently for elements on the diagonal and for off-diagonal elements. In summary, we compute

Task

Use two nested loops over the rows and the columns of matrix \(A\) to compute all the elements of matrix \(A\) using the formulas given above.

Your Solution

  • Read the pedigree from the given file
  • Add parents that do not appear as animals to the column of animals
  • Create an empty square matrix \(A\) with as many rows and columns as we have animals in the pedigree.
  • Use the above formula to compute the diagonal element of the first row
  • Create a first loop to compute all the off-diagonal elements of the first row
  • Create a loop to repeat the computations over all rows.

Problem 2: Verification

Use the function pedigreemm::getA() from package pedigreemm to verify your result from problem 1.

Your Solution

  • Define the pedigree
  • Compute the relationship matrix with getA()

Problem 3: Functions in R

Computations such as the computation of the diagonal elements or such as the one of the off-diagonal elements can be isolated and factored out in a important programming construct which is called function. A function takes a set of input parameter and transforms them into a result which is returned. For our example of the numerator relationship matrix two functions can be constructed according to the following template

compute_square <- function(pn_number){
  square_result <- pn_number*pn_number
  return(square_result)
}

The function can be used by function calls which take a given input and return a result

compute_square(pn_number = 3)
[1] 9
compute_square(1:10)
 [1]   1   4   9  16  25  36  49  64  81 100

Task

Use the above template to construct a function and factor out the computations of the diagonal elements and of the off-diagonal elements into two separate functions.

Your Solution

  • Write a function to compute a diagonal element
  • Write a function to compute an off-diagonal element
  • Use the functions to compute all elements of the numerator relationship matrix

Latest Changes: 2022-11-18 07:25:38 (pvr)

LS0tCnRpdGxlOiBMaXZlc3RvY2sgQnJlZWRpbmcgYW5kIEdlbm9taWNzIC0gTm90ZWJvb2sgNwphdXRob3I6IFBldGVyIHZvbiBSb2hyCmRhdGU6ICcyMDIyLTExLTExJwpvdXRwdXQ6IGh0bWxfbm90ZWJvb2sKcGFyYW1zOgogIGRvY3R5cGU6CiAgICBsYWJlbDogR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