/
lab5.Rmd
160 lines (130 loc) · 5 KB
/
lab5.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
---
title: "STA100 Lab5"
author: "Po"
date: "4/27/2021"
output: html_document
---
```{r setup, include=FALSE}
options(width = 100)
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE )
```
# Panda Village
![](https://static.wikia.nocookie.net/kungfupanda/images/6/63/Panda-village2.jpg)
1. Po visits the Panda Village. He captures **10 pandas** and marks their butts with paint.
2. A week later he returns to the village and captures **15 pandas**. **Five of these 15 pandas** have paint on their butts, indicating that they are recaptured pandas.
3. Can you guess/estimate the **total numbers of pandas** in the Panda Village?
Poll:
- $N=20$ `r emo::ji("panda")`
- $N=30$ `r emo::ji("panda")`
- $N=40$ `r emo::ji("panda")`
```{r echo=FALSE, fig.height = 6, fig.width = 10}
possibleN <- c(20, 30, 40)
panda_col <- hcl.colors(length(possibleN), palette = "TealGrn")
barplot(t(sapply(possibleN, function(N) dhyper(0:10, 10, N-10, 15))),
beside=TRUE,
main = paste0("Recapture probability of 10 marked pandas when population N = 20, 30, 40"),
ylab = "Probability",
xlab = "number of recaptured marked pandas out of 15 pandas",
col=panda_col,
names=0:10)
legend("topleft", legend = paste0("N=", possibleN), fill=panda_col)
```
```{r echo=FALSE, fig.height = 7, fig.width = 10}
possibleN <- 20:40
panda_col <- hcl.colors(length(possibleN), palette = "TealGrn")
barplot(t(sapply(possibleN, function(N) dhyper(4:6, 10, N-10, 15))),
beside=TRUE,
main = paste0("Recapture probability of 10 marked pandas when population N = 20, 21, ..., 40"),
ylab = "Probability",
xlab = "number of recaptured marked pandas out of 15 pandas",
col=panda_col,
bty='L',
names=4:6)
legend("topright", legend = paste0("N=", possibleN), fill=panda_col)
```
Based on maximum likelihoodk, what N would you pick if you recapture 4, 5 or 6 pandas?
```{r fig.height = 8, fig.width = 8}
hyperLik <- function(N,M,n,m){dhyper(m,M,N-M,n)}
possibleN <- 20:40
likelihoods <- hyperLik(possibleN, 10, 15, 5)
plot(possibleN,likelihoods,
type="l",
ylab="Likelihood",
xlab="N",
main = "Likelihood of N")
```
For more information aobut **Mark and recapture**, you may read [Wikipedia: Mark and recapture](https://en.wikipedia.org/wiki/Mark_and_recapture).
# Lab Assignment 3 (Lab projects?)
1. Go though `LabAssignment3.pdf` [LabAssignment3.pdf](LabAssignment3.pdf)
2. Download and open `LabAssignment3.Rmd` [LabAssignment3.Rmd](LabAssignment3.Rmd) in RStudio and run codes inside. Things should be exactly same as `LabAssignment3.pdf`.
3. Download, open `LabAssignment3_otg.Rmd` [LabAssignment3_jp.Rmd](LabAssignment3_jp.Rmd) and save as a new Rmd file for your own Lab Assignment submission. Knit this Rmd file to see if you can make a html, pdf or words. If you couldn't, just use RStudio Cloud or the Binder RStudio [![Binder](http://mybinder.org/badge_logo.svg)](http://mybinder.org/v2/gh/ucdavis-sta-100-spring-2021/polabs/main?urlpath=rstudio) set by Po.
4. Run/add/edit/delete the codes in your own copy of `LabAssignment3_jp.Rmd` and add/edit/delete some writings. Knit to see how the .pdf looks like. Tidy up the codes and writings. Knit and submit!
### Help/Documentation
Read the R Documentation of the functions you learn today. Enter the following codes, one by one, and read the corresponding documentation/help in the **Help** tab.
```{r eval=FALSE}
?dhyper
```
```{r eval=FALSE}
?plot
```
```{r eval=FALSE}
?which.max
```
```{r eval=FALSE}
?read.table
```
```{r eval=FALSE}
?c
```
```{r eval=FALSE}
?head
```
```{r eval=FALSE}
?mean
```
```{r eval=FALSE}
?sd
```
```{r eval=FALSE}
?summary
```
```{r eval=FALSE}
?hist
```
```{r eval=FALSE}
?table
```
```{r eval=FALSE}
?mosaicplot
```
```{r}
?boxplot
```
```{r}
?aggregate
```
How Po knows all those built-in functions in R?
- Way 1: Google it
- Way 2: Read R Help/Documentation
> There is no secret ingredient. -- Mr. Ping `r emo::ji("noodle")`
# R tutorials that help you survive in Panda Village `r emo::ji("bamboo")`
- Interactive R course lessons **swirl** https://swirlstats.com/students.html
Install **swirl** with the following commands.
```{r eval=FALSE}
# If you haven't installed the swirl package yet
install.packages("swirl")
library(swirl)
install_course_github("swirldev", "R_Programming_E")
```
After **swirl** is installed (you only need to do it once), you can start/resume the R tutorial with the commands:
```{r eval=FALSE}
library(swirl)
swirl()
```
Schedule some time to go though all the lessons by yourself! Almost all of the R codes you need to know are here in those lessons.
- If you prefer more static readings (Po did read them to learn R!):
- R for Beginners https://cran.r-project.org/doc/contrib/Paradis-rdebuts_en.pdf
- An Introduction to R https://cran.r-project.org/doc/manuals/r-release/R-intro.pdf
- If you like RStudio's data science things,
- R for Data Science https://r4ds.had.co.nz/
- RStudio Primer https://rstudio.cloud/learn/primers/