Homework 1

  1. Suppose that we’ve fit a model for the odds of a specific disease. Using that model, we would estimate that a smoker’s odds for developing the disease is:

    \[ e^{-2 + (0.8)(1)} \]

    Use R, and the expression above, to estimate a smoker’s odds for the disease. Type your solution all in one line, without relying on any mental math.

    You can use exp(), which provides powers of \(e\) (a number that is very important in the field of statistics). For example, \(e^3\) is exp(3).

    > exp(-2+0.8*1)
    [1] 0.3011942
  2. In genetics, the Hardy–Weinberg principle states that the frequency of heterozygotes is given by \(2p(1-p)\), where \(p\) is the frequency of the dominant allele. Use R, and the expression above, to find the frequency of heterozygotes when \(p=0.8\). Type your solution all in one line, without relying on any mental math.

    > 2*0.8*(1-0.8)
    [1] 0.32
  3. In diagnostic testing, positive predictive value is given by:

    \[ \frac{\text{sensitivity}\times{\text{prevalence}}}{\text{sensitivity}\times{\text{prevalence}}+(1-\text{specificity})\times(1-{\text{prevalence}})} \]

    You may notice that the numerator also appears in the denominator. Suppose that a diagnostic test has sensitivity of 0.7 and specificity of 0.9. Use object assignment to temporarily store the product of sensitivity and prevalence.

    > numerator<-0.7*0.3
  4. Now, use the object you assigned above, and the expression above, to calculate the positive predictive value of the diagnostic test in settings where the prevalence of the disease is 0.3. Type your solution all in one line, without relying on any mental math.

    > numerator/(numerator+(1-0.9)*(1-0.3))
    [1] 0.75
  5. When you use object assignment, your computer will automatically replace any existing objects, without warning you. Use object assignment to store the number 5 as x. Verify that x is 5. Then, use object assignment to store the number 100 as x. Notice that your computer does not provide any warning about replacing x. Now, verify that x is 100.

    Using the jar analogy, replacing an existing object is like taking an existing jar, emptying out the contents, and putting something else in the jar.

    > x<-5
    > x
    [1] 5
    > x<-100
    > x
    [1] 100
  6. Continuing the discussion above, your computer will seemingly replace objects like pi, which I mentioned is automatically included in R.

    Verify that the value of pi prints as 3.141593 (for those wondering, yes, there are more digits behind this than you can see). Use object assignment to store 25/8 as pi. Verify that pi is now 3.125. Now, remove your object, using the rm() function: rm(pi). Verify that pi is now 3.141593 again (phew).

    What’s going on here? First, I want to emphasize that rm() is not an undo operation. In the exercise above, for example, applying rm(x) would truly make x unavailable, rather than restoring the value of x back to 5.

    So, what’s actually going on here? Using the jar analogy: one way to think about what happens when we store 25/8 as pi is that we end up with two jars labelled pi, in different pantries: our pi jar of 25/8 is in our own pantry (which I’ll call the chef’s pantry), while the original pi jar is in the computer’s pantry. So to be clear:

    • chef’s pantry: pi is 25/8
    • computer’s pantry: pi is 3.141593…

    Since we’re the chef, when we ask the computer for the pi jar, it’s going to first look in our own pantry, and bring us the pi jar of 25/8.

    After we remove our pi jar of 25/8 using rm() there is no longer a pi jar in the chef’s pantry. At that point, when we ask our computer for the pi jar, it will still look in the chef’s pantry (the default), but because it won’t be able to find it, it will then look in its own pantry.

    rm() originated as a shorthand for “remove”.

    > pi
    [1] 3.141593
    > pi<-25/8
    > pi
    [1] 3.125
    > rm(pi)
    > pi
    [1] 3.141593
  7. Continuing the jar analogy, you might ask: well, what’s in my pantry? Type ls() to see a list of all the objects in your pantry. You should see x, unless you ended up removing it. You may also see other objects.

    In RStudio, you can also look in the Environment tab to see a list of objects.

    In R lingo, the chef’s pantry is known as a workspace (and pantries in general are known as environments).

    ls() originated as a shorthand for “list”.