> exp(-2+0.8*1)
[1] 0.3011942Homework 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.
TipYou can use
exp(), which provides powers of \(e\) (a number that is very important in the field of statistics). For example, \(e^3\) isexp(3).TipSolutionIn 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.
TipSolution> 2*0.8*(1-0.8) [1] 0.32In 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.
TipSolution> numerator<-0.7*0.3Now, 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.
TipSolution> numerator/(numerator+(1-0.9)*(1-0.3)) [1] 0.75When 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 thatxis 5. Then, use object assignment to store the number 100 asx. Notice that your computer does not provide any warning about replacingx. Now, verify thatxis 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.
TipSolution> x<-5 > x [1] 5 > x<-100 > x [1] 100Continuing the discussion above, your computer will seemingly replace objects like
pi, which I mentioned is automatically included in R.Verify that the value of
piprints as 3.141593 (for those wondering, yes, there are more digits behind this than you can see). Use object assignment to store 25/8 aspi. Verify thatpiis now 3.125. Now, remove your object, using therm()function:rm(pi). Verify thatpiis 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, applyingrm(x)would truly makexunavailable, rather than restoring the value ofxback 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
piis that we end up with two jars labelledpi, in different pantries: ourpijar of 25/8 is in our own pantry (which I’ll call the chef’s pantry), while the originalpijar is in the computer’s pantry. So to be clear:- chef’s pantry:
piis 25/8 - computer’s pantry:
piis 3.141593…
Since we’re the chef, when we ask the computer for the
pijar, it’s going to first look in our own pantry, and bring us thepijar of 25/8.After we remove our
pijar of 25/8 usingrm()there is no longer apijar in the chef’s pantry. At that point, when we ask our computer for thepijar, 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.Noterm()originated as a shorthand for “remove”.TipSolution> pi [1] 3.141593 > pi<-25/8 > pi [1] 3.125 > rm(pi) > pi [1] 3.141593- chef’s pantry:
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 seex, 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.
NoteIn R lingo, the chef’s pantry is known as a workspace (and pantries in general are known as environments).
Notels()originated as a shorthand for “list”.