Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```{r echo=FALSE}
- opts_chunk$set(echo=TRUE, tidy=FALSE)
- ```
- WASSUP
- ===========
- (c) Gail Gong 2013
- 03a More R sharpening
- ===========
- ### 1. Review some functions that might be useful
- ```{r review, eval=FALSE}
- setdiff(1:10, c(3, 1, 4, 100))
- sort(c(3, 1, 4, 1, 5, 9))
- 1:3 %in% c(3, 1, 4, 1, 5, 9)
- any(c(TRUE, FALSE))
- any(c(FALSE, FALSE))
- all(c(TRUE, FALSE))
- all(c(TRUE, TRUE))
- ifelse(c(TRUE, FALSE, TRUE, FALSE), c(1, 2, 3, 4), c(10, 20, 30, 40))
- seats = c("", "", "o", "", "", "o", "o", "o")
- success = TRUE
- data.frame(t(c(s = seats, success = success)))
- look = data.frame(chairs = letters[1:7], score = sort(c(3, 1, 4, 1 ,3, 4, 4)))
- look
- look[look$score >= 4, ]
- ```
- # AAAA
- 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`.)
- Your code should work if I give you a different set of rolls.
- ```{r look}
- set.seed(1)
- NRolls = 13
- index = 1:NRolls
- roll = sample(1:6, size = NRolls, replace = TRUE)
- runningTotal = cumsum(roll)
- look = data.frame(index, roll, runningTotal)
- look
- ```
- ```{r aaaa}
- smallest = c(look$index[look$runningTotal >=13])
- smallest[1]
- ```
- ### 2. Review `while`, and my example has some useful ideas.
- ```{r whileExample, eval=FALSE}
- rollHistory = c()
- runningTotal = 0
- gameOver = runningTotal >= 13
- while(!gameOver){
- roll = sample(1:6, size = 1)
- rollHistory = c(rollHistory, roll)
- runningTotal = runningTotal + roll
- gameOver = (runningTotal >= 13)
- print(data.frame(runningTotal, gameOver, t(c(roll = rollHistory))))
- }
- rollHistory
- runningTotal
- data.frame(rollHistory, look = cumsum(rollHistory))
- ```
- # BBBB
- What does this while loop (above) do?
- 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.
- ### 3. `sample` with the `prob` option.
- ```{r sample}
- pRed = 0.9
- oneHundredBalls = sample(c("blue", "red"),
- size = 100, replace = TRUE, prob = c(1-pRed, pRed))
- table(oneHundredBalls)
- ```
- # CCCC
- 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.
- ```{r cccc}
- pHeads = 0.8
- toss = sample(c("tails", "heads"), size =10, replace = TRUE, prob = c(1-pHeads, pHeads))
- toss
- table(toss)
- proportion = sum(toss == "heads")/10
- proportion
- ```
- ### 4. Bayes rule
- 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?
- I will rewrite Bayes rule (see the lecture notes on Conditional Probabilities) substituting the notation here.
- ```
- M1 = Machine 1 = Careful
- M2 = Machine 2 = Quicky
- Bad = Defective
- P(M1) = 0.4
- P(M2) = 0.6
- P(Bad | M1) = 0.04
- P(Bad | M2) = 0.10
- P(M1 and Bad) = P(M1) P(Bad | M1) = ?
- P(M2 and Bad) = P(M2) P(Bad | M2) = ?
- P(Bad) = P(M1 and Bad) + P(M2 and Bad) = ?
- P(M2 | Bad) = P(M2 and Bad) / P(Bad) = ?
- ```
- # DDDD
- Write R code to calculate `P(M2 | Bad)`.
- ```{r dddd}
- pM1 = 0.4
- pM2 = 0.6
- pBadGivenM1 = 0.04
- pBadGivenM2 = 0.10
- pBad = (pM1 * pBadGivenM1) + (pM2 * pBadGivenM2)
- pM2Bad = (pM2 * pBadGivenM2) / pBad
- pM2Bad
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement