Advertisement
Guest User

Untitled

a guest
Apr 24th, 2013
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. ```{r echo=FALSE}
  2. opts_chunk$set(echo=TRUE, tidy=FALSE)
  3. ```
  4.  
  5. WASSUP
  6. ===========
  7. (c) Gail Gong 2013
  8.  
  9. 03a More R sharpening
  10. ===========
  11.  
  12. ### 1. Review some functions that might be useful
  13.  
  14. ```{r review, eval=FALSE}
  15. setdiff(1:10, c(3, 1, 4, 100))
  16. sort(c(3, 1, 4, 1, 5, 9))
  17. 1:3 %in% c(3, 1, 4, 1, 5, 9)
  18. any(c(TRUE, FALSE))
  19. any(c(FALSE, FALSE))
  20. all(c(TRUE, FALSE))
  21. all(c(TRUE, TRUE))
  22. ifelse(c(TRUE, FALSE, TRUE, FALSE), c(1, 2, 3, 4), c(10, 20, 30, 40))
  23.  
  24. seats = c("", "", "o", "", "", "o", "o", "o")
  25. success = TRUE
  26. data.frame(t(c(s = seats, success = success)))
  27.  
  28. look = data.frame(chairs = letters[1:7], score = sort(c(3, 1, 4, 1 ,3, 4, 4)))
  29. look
  30. look[look$score >= 4, ]
  31. ```
  32.  
  33.  
  34. # AAAA
  35.  
  36. I repeatedly roll a 6-sided die and I record the result in `roll`. I also keep a `runningTotal`. Write code to find the number of rolls it takes to have a `runningTotal` at least `13`. (I want the smallest `index` such that `runningTotal >= 13`.)
  37.  
  38. Your code should work if I give you a different set of rolls.
  39.  
  40.  
  41. ```{r look}
  42. set.seed(1)
  43. NRolls = 13
  44. index = 1:NRolls
  45. roll = sample(1:6, size = NRolls, replace = TRUE)
  46. runningTotal = cumsum(roll)
  47. look = data.frame(index, roll, runningTotal)
  48. look
  49. ```
  50.  
  51.  
  52. ```{r aaaa}
  53. smallest = c(look$index[look$runningTotal >=13])
  54. smallest[1]
  55. ```
  56.  
  57. ### 2. Review `while`, and my example has some useful ideas.
  58.  
  59. ```{r whileExample, eval=FALSE}
  60. rollHistory = c()
  61. runningTotal = 0
  62. gameOver = runningTotal >= 13
  63. while(!gameOver){
  64. roll = sample(1:6, size = 1)
  65. rollHistory = c(rollHistory, roll)
  66. runningTotal = runningTotal + roll
  67. gameOver = (runningTotal >= 13)
  68. print(data.frame(runningTotal, gameOver, t(c(roll = rollHistory))))
  69. }
  70. rollHistory
  71. runningTotal
  72. data.frame(rollHistory, look = cumsum(rollHistory))
  73. ```
  74.  
  75. # BBBB
  76.  
  77. What does this while loop (above) do?
  78.  
  79. The `while` loop checks `gameOver`; if `gameOver == FALSE`, go inside the curly braces and muck in there; if `gameOver == TRUE` exit the `while`. Do you agree? Yes.
  80.  
  81. ### 3. `sample` with the `prob` option.
  82.  
  83. ```{r sample}
  84. pRed = 0.9
  85. oneHundredBalls = sample(c("blue", "red"),
  86. size = 100, replace = TRUE, prob = c(1-pRed, pRed))
  87. table(oneHundredBalls)
  88. ```
  89.  
  90. # CCCC
  91.  
  92. I have a biased coin that has probability `0.8` of coming up Heads. Write code that simulates tossing this coin 10 times. Show me the tosses and show me the proportion that came up heads.
  93.  
  94. ```{r cccc}
  95. pHeads = 0.8
  96. toss = sample(c("tails", "heads"), size =10, replace = TRUE, prob = c(1-pHeads, pHeads))
  97. toss
  98. table(toss)
  99. proportion = sum(toss == "heads")/10
  100. proportion
  101. ```
  102.  
  103.  
  104. ### 4. Bayes rule
  105.  
  106. At a widget factory there are two machines. Quicky is faster than Careful so it produces 60 percent of the widgets. The proportion of widgets made by Quicky that are defective is 10 percent, and the proportion of widgets made by Careful that are defective is 4 percent. If a widget is found to be defective, what is the probability it was made by Quicky?
  107.  
  108. I will rewrite Bayes rule (see the lecture notes on Conditional Probabilities) substituting the notation here.
  109.  
  110. ```
  111. M1 = Machine 1 = Careful
  112. M2 = Machine 2 = Quicky
  113. Bad = Defective
  114. P(M1) = 0.4
  115. P(M2) = 0.6
  116. P(Bad | M1) = 0.04
  117. P(Bad | M2) = 0.10
  118. P(M1 and Bad) = P(M1) P(Bad | M1) = ?
  119. P(M2 and Bad) = P(M2) P(Bad | M2) = ?
  120. P(Bad) = P(M1 and Bad) + P(M2 and Bad) = ?
  121. P(M2 | Bad) = P(M2 and Bad) / P(Bad) = ?
  122. ```
  123. # DDDD
  124.  
  125. Write R code to calculate `P(M2 | Bad)`.
  126.  
  127. ```{r dddd}
  128. pM1 = 0.4
  129. pM2 = 0.6
  130. pBadGivenM1 = 0.04
  131. pBadGivenM2 = 0.10
  132. pBad = (pM1 * pBadGivenM1) + (pM2 * pBadGivenM2)
  133. pM2Bad = (pM2 * pBadGivenM2) / pBad
  134. pM2Bad
  135. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement