UofUEpiBio / PHS7045-advanced-programming

https://uofuepibio.github.io/PHS7045-advanced-programming
3 stars 4 forks source link

Week 2 #4

Closed chipmanj closed 1 month ago

chipmanj commented 1 year ago

Please study the below material:

Read/Watch: Selections from R Programming for Data Science

Read: Selections from The Art of R Programming

Note 1: Big-picture principles in chapter 2, on vectors, have relevance to matrixes, lists, and data frames.

Note 2: A reference for good coding practices. This blog describes common strategies for writing clear, concise code. It may be helpful to refer to it frequently as you develop your coding style.

chipmanj commented 1 year ago

@UofUEpiBio/phs7045-2023 While the website is being updated, today's lab can be accessed here

chipmanj commented 1 year ago

@UofUEpiBio/phs7045-2023: There are two options for submitting your lab: (1) attaching directly to this thread (the week's issue) or (2) sending it to us if you prefer to not have your code be public.

hyejung0 commented 1 year ago

Hi Jonathan,

The attached is my code for equal allocation rule and RAR. Please let me know if you have any comments or suggestions to make it better. Thank you!

On Jan 19, 2023, at 8:51 PM, Jonathan J Chipman @.***> wrote:

@UofUEpiBio/phs7045-2023 https://github.com/orgs/UofUEpiBio/teams/phs7045-2023: There are two options for submitting your lab: (1) attaching directly to this thread (the week's issue) or (2) sending it to us if you prefer to not have your code be public.

— Reply to this email directly, view it on GitHub https://github.com/UofUEpiBio/PHS7045-advanced-programming/issues/4#issuecomment-1397891041, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASHK76L26KBVFSMZOCI24QDWTIDVZANCNFSM6AAAAAATV452XQ. You are receiving this because you are on a team that was mentioned.

gvegayon commented 1 year ago

Hey @UofUEpiBio/phs7045-2023,

@chipmanj and I were thinking about interesting problems involving vectorization. Here is one:

In network science, the Erdos-Renyi model (Bernoulli graph) is a fundamental piece of random graphs. We often use it in Montecarlo studies to generate null distributions, for which speed is key. The model is very simple. Let A = {a_ij} denote the matrix representation of a graph, where the ij-th entry equals 1 if there's a tie from node i to node j and zero otherwise. In the ER model, ties are treated independently and have a probability p of occurring. Furthermore, A's diagonal is zero, meaning that loops (connections to oneself) are forbidden. Your task is to create a vectorized algorithm that generates Bernoulli graphs with probability p fast enough to create networks with 10,000 nodes, i.e., 10,000 x 10,000 0/1 matrix. The network should have a zero diagonal. The following is a wrong way of doing it:

er_graph <- function(n, p) {

  # Making room
  A <- matrix(0, ncol = n, nrow = n)

  for (i in 1:n) {
    for (j in 1:n) {

     # Loops are not allowed
      if ( (i != j) && (runif(1) < p) ) {
        A[i, j] <- 1
      }
    }
  }

  A
}

As the size of the graph increases, memory will become a problem. In that case, you can use sparse matrices instead of base R matrices. I recommend using the dgCMatrix class from the Matrix R package.

gvegayon commented 1 year ago

Hi Jonathan, The attached is my code for equal allocation rule and RAR. Please let me know if you have any comments or suggestions to make it better. Thank you!  On Jan 19, 2023, at 8:51 PM, Jonathan J Chipman @.***> wrote: @UofUEpiBio/phs7045-2023 https://github.com/orgs/UofUEpiBio/teams/phs7045-2023: There are two options for submitting your lab: (1) attaching directly to this thread (the week's issue) or (2) sending it to us if you prefer to not have your code be public. — Reply to this email directly, view it on GitHub <#4 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASHK76L26KBVFSMZOCI24QDWTIDVZANCNFSM6AAAAAATV452XQ. You are receiving this because you are on a team that was mentioned.

@hyejung0, unfortunately, attachments can only be done directly on GitHub. You can't add attachments to issues through your email. Just upload the file here whenever you have time 😃