]> code.communitydata.science - stats_class_2019.git/blob - r_lectures/w04-R_lecture.Rmd
adding a bunch of new files around week 4
[stats_class_2019.git] / r_lectures / w04-R_lecture.Rmd
1 ---
2 title: "Week 3 R lecture"
3 subtitle: "Statistics and statistical programming  \nNorthwestern University  \nMTS 525"
4 author: "Aaron Shaw"
5 date: "April 18, 2019"
6 output: html_document
7 ---
8
9 ```{r setup, include=FALSE}
10 knitr::opts_chunk$set(echo = TRUE)
11 ```
12
13 This week, we'll focus on one more way to manage date-time objects and some ways to generate distributions.
14
15 ## as.Date
16
17 First, something I meant to include in last week's materials. The `as.Date()` function provides an alternative to `as.POSIX()` that is far more memorable and readable, but far less precise. Note that it truncates the time of day and the timezone from the ouput
18
19 ```{r}
20 m <- "2019-02-21 04:35:00"
21 class(m)
22
23 a.good.time <- as.Date(m, format="%Y-%m-%d %H:%M:%S", tz="CDT")
24 class(a.good.time)
25 a.good.time
26 ```
27
28 ## Distribution functions
29 distribution functions: lets focus on *unif(): the key is on page 222 of Verzani
30 The “d” functions return the p.d.f. of the distribution
31 dunif(x=1, min=0, max=3) # 1/3 of the area is the to the left 1
32 The “p” functions return the c.d.f. of the distribution.
33 dunif(q=2, min=0, max=3) #1/(b-a) is 2/3
34 The “q” functions return the quantiles.
35 qunif(p=0.5, min=0, max=3) # half way between 0 and 3
36
37 The “r” functions return random samples from a distribution.
38 runif(n=1, min=0, max=3) # a random value in [0,3]
39
40 ## Doing simple simulations with random data
41 runif()
42 rnorm()
43
44 ## A quick simulation
45
46 In case you don't believe the central limit theorem, let's put together a quick simulation to illustrate it in R.
47
48 Write a function to repeatedly take the mean of a sample.
49
50 Experiment by changing the size of the sample
51
52
53 ## Quantile quantile plots
54
55
56 ## Binomial and factorial functions
57 Choose, factorial
58
59

Community Data Science Collective || Want to submit a patch?