-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path01-Introduction-to-R.Rmd
More file actions
927 lines (637 loc) · 28.3 KB
/
01-Introduction-to-R.Rmd
File metadata and controls
927 lines (637 loc) · 28.3 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
# Introduction to R
This tutorial aims to serve as an introduction to the software package R. Other very good and much more exhaustive tutorials and useful reference-cards can be found at the following links:
* [Reference card for R commands](http://cran.r-project.org/doc/contrib/refcard.pdf) (always useful)
* [Matlab/R reference card](http://www.math.umaine.edu/~hiebeler/comp/matlabR.pdf) (for those who are more familiar with Matlab)
* [The official Introduction to R](https://cran.r-project.org/doc/manuals/r-release/R-intro.pdf) (very detailed)
* And many more at [www.r-project.org](https://www.r-project.org/other-docs.html) (see "Documents")
<!-- * An interactive introduction can be done online at: [www.datacamp.com](https://www.datacamp.com/) -->
* An R-package for learning R: [www.swirl.com](https://swirlstats.com/)
* An excellent book project which covers also advanced issues such as "writing performant code" and "package development": [adv-r.had.co.nz](http://adv-r.had.co.nz/)
* Another excellent book: [R for Data Science](https://r4ds.had.co.nz/)
Some other tutorials:
* [Introduction to data science](https://idc9.github.io/stor390/)
* [Scraping the web using R](https://stat4701.github.io/edav/2015/04/02/rvest_tutorial/)
* [Creating dynamic graphics](https://gganimate.com/)
Why R?
* R is **free** of charge from: [www.r-project.org](https://www.r-project.org/)
* The celebrated IDE **RStudio** for R is also **free** of charge: [www.rstudio.com](http://www.rstudio.com/)
* R is equipped with one of the most flexible and powerful graphics routines available anywhere. \
For instance, check out one of the following repositories:
* [Clean Graphs](http://shinyapps.org/apps/RGraphCompendium/index.php)
* [R graph catalog](http://shiny.stat.ubc.ca/r-graph-catalog/)
* [Publication Ready Plots](http://www.sthda.com/english/rpkgs/ggpubr/)
* Today, R is the de-facto standard for statistical science.
## Short Glossary
Lets start the tutorial with a (very) short glossary:
* **Console**: The thing with the "**>**" sign at the beginning.
* **Script file**: An ordinary text file with suffix "**.R**". For instance, **yourFavoritFileName.R**.
* **Working directory**: The file-directory you are working in. Useful commands: with `getwd()` you get the location of your current working directory and `setwd()` allows you to set a new location for it.
* **Workspace**: This is a hidden file (stored in the working directory), where all objects you use (e.g., data, matrices, vectors, variables, functions, etc.) are stored. Useful commands: `ls()` shows all elements in our current workspace and `rm(list=ls())` deletes all elements in our current workspace.
## First Steps
A good idea is to use a script file such as **yourFavoritFileName.R** in order to store your R commands. You can send single lines or marked regions of your R-code to the console by pressing the keys **STRG+ENTER**.
To begin with baby steps, do some simple computations:
```{r}
2+2 # and all the others: *,/,-,^2,^3,...
```
Note: Everything that is written after the `#`-sign is ignored by R, which is very useful to comment your code.
The **assignment operator** will be your most often used tool. Here an example to create a **scalar** variable:
```{r}
x <- 4
x
4 -> x # possible but unusual
x
```
Note: The R community loves the `<-` assignment operator, which is a very unusual syntax. Alternatively, you can use the `=` operator.
And now a more interesting object - a **vector**:
```{r}
y <- c(2,7,4,1)
y
```
The command `ls()` shows the total content of your current workspace, and the command `rm(list=ls())` deletes all elements of your current workspace:
```{r}
ls()
rm(list=ls())
ls()
```
Note: RStudio's **Environment** pane also lists all the elements in your current workspace. That is, the command `ls()` becomes a bit obsolete when working with RStudio.
Let's try how we can compute with vectors and scalars in R.
```{r}
x <- 4
y <- c(2,7,4,1)
x*y # each element in the vector, y, is multiplied by the scalar, x.
y*y # this is a term by term product of the elements in y
```
Performing vector multiplications as you might expect from your last math-course, e.g., an outer product: $y\,y^\top$:
```{r}
y %*% t(y)
```
Or an inner product $y^\top y$:
```{r}
t(y) %*% y
```
Note: Sometimes, R's treatment of vectors can be annoying. The product `y %*% y` is treated as the product `t(y) %*% y`.
The term-by-term execution as in the above example, `y*y`, is actually a central strength of R. We can conduct many operations **vector-wisely**:
```{r}
y^2
log(y)
exp(y)
y-mean(y)
(y-mean(y))/sd(y) # standardization
```
This is a central characteristic of so called matrix based languages like R (or Matlab). Other programming languages often have to use **loops** instead:
```{r, eval=FALSE}
N <- length(y)
1:N
y.sq <- numeric(N)
y.sq
for(i in 1:N){
y.sq[i] <- y[i]^2
if(i == N){
print(y.sq)
}
}
```
The `for()`-loop is the most common loop. But there is also a `while()`-loop and a `repeat()`-loop. However, loops in R can be rather slow, therefore, try to avoid them!
<!-- Try to write your own loop, which computes the element-wise logarithms of a vector `y` and finally prints these values as well as their mean! -->
Useful commands to produce **sequences** of numbers:
```{r, eval=FALSE}
1:10
-10:10
?seq # Help for the seq()-function
seq(from=1, to=100, by=7)
```
Using the sequence command `1:16`, we can go for our first **matrix**:
```{r}
?matrix
A <- matrix(data=1:16, nrow=4, ncol=4)
A
A <- matrix(1:16, 4, 4)
```
Note that a matrix has always two **dimensions**, but a vector has only one dimension:
```{r}
dim(A) # Dimension of matrix A?
dim(y) # dim() does not operate on vectors.
length(y) # Length of vector y?
```
Lets play a bit with the matrix `A` and the vector `y`. As we have seen in the loop above, the `[]`-operator **selects elements** of vectors and matrices:
```{r, eval=FALSE}
A[,1]
A[4,4]
y[c(1,4)]
```
This can be done on a more **logical** basis, too. For example, if you want to know which elements in the first column of matrix `A` are strictly greater than 2:
```{r}
A[,1][A[,1]>2]
# Note that this give you a boolean vector:
A[,1]>2
# And you can use it in a non-sense relation, too:
y[A[,1]>2]
```
Note: Logical operations return so-called **boolean** objects, i.e., either a `TRUE` or a `FALSE`. For instance, if we ask R whether `1>2` we get the answer `FALSE`.
## Further Data Objects
Besides classical data objects such as scalars, vectors, and matrices there are three further data objects in R:
\
\
1. The **array**: As a matrix but with more dimensions. Here is an example of a $2\times 2\times 2$-dimensional `array`:
```{r}
myFirst.Array <- array(c(1:8), dim=c(2,2,2)) # Take a look at it!
```
\
\
2. The **list**: In `lists` you can organize different kinds of data. E.g., consider the following example:
```{r}
myFirst.List <- list("Some_Numbers" = c(66, 76, 55, 12, 4, 66, 8, 99),
"Animals" = c("Rabbit", "Cat", "Elefant"),
"My_Series" = c(30:1))
```
A very useful function to find specific values and entries within lists is the `str()`-function:
```{r}
str(myFirst.List)
```
\
\
3. The **data frame**: A `data.frame` is a `list`-object but with some more formal restrictions (e.g., equal number of rows for all columns). As indicated by its name, a `data.frame`-object is designed to store data:
```{r}
myFirst.Dataframe <- data.frame("Credit_Default" = c( 0, 0, 1, 0, 1, 1),
"Age" = c(35,41,55,36,44,26),
"Loan_in_1000_EUR" = c(55,65,23,12,98,76))
# Take a look at it!
```
\
## Simple Regression Analysis using R
Alright, let's do some statistics with real data. You can download the data [HERE](https://github.com/lidom/Teaching_Repo). Save it on your computer, at a place where you can find it, and give the path (e.g. `"C:\textbackslash path\textbackslash auto.data.csv"`, which references to the data, to the *file*-argument of the function `read.csv()`:
```{r, eval=FALSE}
# ATTENTION! YOU HAVE TO CHANGE "\" TO "/":
auto.data <- read.csv(file="C:/your_path/autodata.txt", header=TRUE)
head(auto.data)
```
\
If you have problems to read the data into R, go on with these commands. (For this you need a working internet connection!):
```{r}
# install.packages("readr")
library("readr")
auto.data <- suppressMessages(read_csv(file = "https://cdn.rawgit.com/lidom/Teaching_Repo/bc692b56/autodata.csv",col_names = TRUE))
# head(auto.data)
```
\
You can select specific variables of the `auto.data` using the `$`-operator:
```{r}
gasolin.consumption <- auto.data$MPG.city
car.weight <- auto.data$Weight
## Take a look at the first elements of these vectors:
head(cbind(gasolin.consumption,car.weight))
```
\
This is how you can produce your first plot:
```{r fig-margin, fig.margin = TRUE,fig.width=4.5, fig.height=3.5, cache=TRUE}
## Plot the data:
plot(y=gasolin.consumption, x=car.weight,
xlab="Car-Weight (US-Pounds)",
ylab="Consumption (Miles/Gallon)",
main="Buy Light-Weight Cars!")
```
\
As a first step, we might assume a simple kind of linear relationship between the variables `gasolin.consumption` and `car.weight`. Let us assume that the data was generated by the following simple regression model:
$$
y_i=\alpha+\beta_1 x_i+\varepsilon_i,\quad i=1,\dots,n
$$
where $y_i$ denotes the gasoline-consumption, $x_i$ the weight of car $i$, and $\varepsilon_i$ is a mean zero constant variance noise term. (This is clearly a non-sense model!)
The command `lm()` computes the estimates of this linear regression model. The command (in fact it's a *method*) `summary()` computes further quantities of general interest from the *object* that was returned from the `lm()` function.
```{r}
lm.result <- lm(gasolin.consumption~car.weight)
lm.summary <- summary(lm.result)
lm.summary
```
\
Of course, we want to have a possibility to access all the quantities computed so far, e.g., in order to plot the results. This can be done as following:
```{r,fig.width=10, fig.height=5, out.width='\\textwidth', fig.align='center'}
## Accessing the computed quantities
names(lm.summary) ## Alternatively: str(lm.summary)
alpha <- lm.summary$coefficients[1]
beta <- lm.summary$coefficients[2]
## Plot all:
plot(y=gasolin.consumption, x=car.weight,
xlab="Car-Weight (US-Pounds)",
ylab="Consumption (Miles/Gallon)",
main="Buy light-weight Cars!")
abline(a=alpha,
b=beta, col="red")
```
\
## Programming in R
Let's write, i.e., program our own R-function for estimating linear regression models. In order to be able to validate our function, we start with **simulating data** for which we then *know* all true parameters. Simulating data is like being the "Data-God": For instance, we generate realizations of the error term $\varepsilon_i$, i.e., something which we *never* observe in real data.
\
Let us consider the following multiple regression model:
$$y_i=\beta_1 +\beta_2 x_{2i}+\beta_3 x_{3i}+\varepsilon_{i},\quad i=1,\dots,n,$$
where $\varepsilon_{i}$ is a heteroscedastic error term
$$\varepsilon_{i}\sim N(0,\sigma_i^2),\quad \sigma_i=x_{3i},$$
and where for all $i=1,\dots,n=50$:
* $x_{2i}\sim N(10,1.5^2)$
* $x_{3i}$ comes from a t-distribution with 5 degrees of freedom and non-centrality parameter 2
```{r}
set.seed(109) # Sets the "seed" of the random number generators:
n <- 50 # Number of observations
## Generate two explanatory variables plus an intercept-variable:
X.1 <- rep(1, n) # Intercept
X.2 <- rnorm(n, mean=10, sd=1.5) # Draw realizations form a normal distr.
X.3 <- rt(n, df=5, ncp=2) # Draw realizations form a t-distr.
X <- cbind(X.1, X.2, X.3) # Save as a Nx3-dimensional data matrix.
```
OK, we have regressors, i.e., data that we also have in real data sets.
Now we define the elements of the $\beta$-vector. Be aware of the difference: In real data sets we do not know the true $\beta$-vector, but try to estimate it. However, when simulating data, we determine (as "Data-Gods") the true $\beta$-vector and can compare our estimate $\hat{\beta}$ with the true $\beta$:
```{r}
## Define the slope-coefficients
beta.vec <- c(1,-5,5)
```
\
We still need to simulate realizations of the dependent variable $y_i$. Remember that $y_i=\beta_1 x_{1i}+\beta_1 x_{2i}+\beta_3 x_{3i}+\varepsilon_{i}$. That is, we only need realizations from the error terms $\varepsilon_i$ in order to compute the realizations from $y_i$. This is how you can simulate realizations from the heteroscedastic error terms $\varepsilon_i$:
```{r}
## Generate realizations from the heteroscadastic error term
eps <- (X.3)*rnorm(n, mean=0, sd=1)
```
Take a look at the heteroscedasticity in the error term:
```{r, fig.margin = TRUE, fig.width=4.5, fig.height=3.5, cache=TRUE}
plot(y=eps, x=X.3,
main="Realizations of the \nHeteroscedastic Error Term")
```
With the (pseudo-random) realizations from $\varepsilon_i$, we can finally generate realizations from the dependent variable $y_i$:
```{r}
## Dependent variable:
y <- X %*% beta.vec + eps
```
Let's take a look at the data:
```{r,fig.width=10, fig.height=5, out.width='\\textwidth', fig.align='center'}
mydata <- data.frame("Y"=y, "X.1"=X.1, "X.2"=X.2, "X.3"=X.3)
pairs(mydata[,-2]) # The '-2' removes the intercept variable "X.1"
```
\
Once we have data, we can compute the OLS estimate of the true $\beta$ vector. Remember the formula:
$$\hat{\beta}=(X^\top X)^{-1}X^\top y$$
In R-Code this is: $(X^\top X)^{-1}=$`solve(t(X) %*% X)`, i.e.:
```{r}
## Computation of the beta-Vector:
beta.hat <- solve(t(X) %*% X) %*% t(X) %*% y
beta.hat
```
\
Well done. Using the above lines of code we can easily program our own `myOLSFun()` function!
```{r}
myOLSFun <- function(y, x, add.intercept=FALSE){
## Number of Observations:
n <- length(y)
## Add an intercept to x:
if(add.intercept){
Intercept <- rep(1, n)
x <- cbind(Intercept, x)
}
## Estimation of the slope-parameters:
beta.hat.vec <- solve(t(x) %*% x) %*% t(x) %*% y
## Return the result:
return(beta.hat.vec)
}
## Run the function:
myOLSFun(y=y, x=X)
```
\
Can you extend the function for the computation of the covariance matrix of the slope-estimates, several measures of fits (R$^2$, adj.-R$^2$, etc.), t-tests, ...?
## R-packages
One of the best features in R are its contributed packages. The list of all packages on CRAN is impressive! Take a look at it [HERE](https://cran.r-project.org/web/packages/available_packages_by_name.html)
For instance, nice plots can be produced using the R-package is `ggplot2`. You can find an intro do this package [HERE](http://ggplot2.tidyverse.org/).
```{r, fig.width=10, fig.height=5, out.width='\\textwidth', fig.align='center'}
# install.packages("ggplot2")
library("ggplot2")
qplot(Sepal.Length, Petal.Length, data = iris, color = Species)
```
\
Of course, `ggplot2` concerns "only" plotting, but you'll find R-packages for almost any statistical method out there.
## Tidyverse
The `tidyverse` package is a collection of packages that lets you import,
manipulate, explore, visualize and model data in a harmonized and consistent way which
helps you to be more productive.
Installing the `tidyverse` package:
```{r installTidyverse, echo=TRUE, eval=FALSE}
install.packages("tidyverse")
```
To use the `tidyverse` package load it using the `library()` function:
```{r usePackage}
library(tidyverse)
```
**Chick Weight Data**
R comes with many datasets installed. We will use the `ChickWeight` dataset
to learn about the tidyverse. The help system gives a basic summary of the experiment from
which the data was collect:
> *"The body weights of the chicks were measured at birth and every second day thereafter
until day 20. They were also measured on day 21. There were four groups of chicks on
different protein diets."*
You can get more information, including references by typing:
```{r helpCWdata, echo=TRUE, eval=FALSE}
help("ChickWeight")
```
**The Data: **
There are `r nrow(ChickWeight)` observations (rows) and `r ncol(ChickWeight)` variables:
* `Chick` -- unique ID for each chick.
* `Diet` -- one of four protein diets.
* `Time` -- number of days since birth.
* `weight` -- body weight of chick in grams.
Note: `weight` has a lower case `w` (recall R is case sensitive).
Store the data locally:
```{r writeCW}
ChickWeight %>%
select(Chick, Diet, Time, weight) %>%
arrange(Chick, Diet, Time) %>%
write_csv("ChickWeight.csv")
```
First we will import the data from a file called `ChickWeight.csv` using the `read_csv()`
function from the `readr` package (part of the `tidyverse`). The first thing to do,
outside of R, is to open the file `ChickWeight.csv` to check what it contains and that
it makes sense. Now we can import the data as follows:
```{r readCW}
CW <- read_csv("ChickWeight.csv")
```
If all goes well then the data is now stored in an R object called `CW`. If you get the
following error message then you need to change the working directory to where the data is
stored.
```
Error: 'ChickWeight.csv' does not exist in current
working directory ...
```
**Changing the working directory:**
In RStudio you can use the menu bar ("Session - Set Working Directory - Choose Directory..."). Alternatively, you can use the function `setwd()`.
\
**Looking at the Dataset:**
To look at the data type just type the object (dataset) name:
```{r printCW}
CW
```
If there are too many variables then not all them may be printed. To overcome this issue
we can use the `glimpse()` function which makes it possible to see every column in your
dataset (called a "data frame" in R speak).
```{r glimpseCW}
glimpse(CW)
```
The function `View()` allows for a spread-sheet type of view on the data:
```{r ViewCW, eval=FALSE}
View(CW)
```
### Tidyverse: Plotting Basics
To **visualise** the chick weight data, we will use the `ggplot2` package (part of the
`tidyverse`). Our interest is in seeing how the *weight changes over time for the chicks by
diet*. For the moment don't worry too much about the details just try to build your own
understanding and logic. To learn more try different things even if you get an error
messages.
Let's plot the weight data (vertical axis) over time (horizontal axis).
```{r emptyPlot, fig.width=1.74, fig.height=1.74, fig.show='hold', fig.align='center'}
# An empty plot (the plot on the left)
ggplot(CW, aes(Time, weight))
# With data (the plot on the right)
ggplot(CW, aes(Time, weight)) + geom_point()
```
Add color for `Diet`. The graph above does not differentiate between the diets. Let's use a different color for
each diet.
```{r addColourPlot, fig.height=2.0}
# Adding colour for diet
ggplot(CW,aes(Time,weight,colour=factor(Diet))) +
geom_point()
```
It is difficult to conclude anything from this graph as the points are printed on top of
one another (with diet 1 underneath and diet 4 at the top).
**Factor Variables:**
Before we continue, we have to make an important change to the `CW` dataset by making
`Diet` and `Time` *factor variables*. This means that R will treat them as categorical
variables (see the `<fct>` variables below) instead of continuous variables. It will
simplify our coding. The next section will explain the `mutate()` function.
```{r makeFactor}
CW <- mutate(CW, Diet = factor(Diet))
CW <- mutate(CW, Time = factor(Time))
glimpse(CW)
```
The `facet_wrap()` function: To plot each diet separately in a grid using `facet_wrap()`:
```{r ScatterPlot}
# Adding jitter to the points
ggplot(CW, aes(Time, weight, colour=Diet)) +
geom_point() +
facet_wrap(~Diet) +
theme(legend.position = "bottom")
```
<!-- To overcome the issue of ovelapping points we can ***jitter*** the points using -->
<!-- `geom_jitter()`. Replace the `geom_point()` above with `geom_jitter()`. What do you -->
<!-- observe? -->
<!-- ```{r jitterPlot2} -->
<!-- # Adding jitter to the points -->
<!-- ggplot(CW, aes(Time, weight, colour=Diet)) + -->
<!-- geom_jitter() + -->
<!-- facet_wrap(~Diet) + -->
<!-- theme(legend.position = "bottom") -->
<!-- ``` -->
**Interpretation:** Diet 4 has the least variability but we can't really say anything about the mean effect
of each diet although diet 3 seems to have the highest.
Next we will plot the **mean changes** over time for each diet using the `stat_summary()` function:
```{r meanlinesPlot, fig.height=2.0}
ggplot(CW, aes(Time, weight,
group=Diet, colour=Diet)) +
stat_summary(fun.y="mean", geom="line")
```
**Interpretation:**
We can see that diet 3 has the highest mean weight gains by the end of the experiment. However,
we don't have any information about the variation (uncertainty) in the data.
To see variation between the different diets we use `geom_boxplot` to plot a box-whisker plot.
A note of caution is that the number of chicks per diet is relatively low to produce this plot.
```{r boxPlot}
ggplot(CW, aes(Time, weight, colour=Diet)) +
facet_wrap(~Diet) +
geom_boxplot() +
theme(legend.position = "none") +
ggtitle("Chick Weight over Time by Diet")
```
**Interpretation:**
Diet 3 seems to have the highest "average" weight gain but it has more variation
than diet 4 which is consistent with our findings so far.
Let's finish with a plot that you might include in a publication.
```{r finalPlot}
ggplot(CW, aes(Time, weight, group=Diet,
colour=Diet)) +
facet_wrap(~Diet) +
geom_point() +
# geom_jitter() +
stat_summary(fun.y="mean", geom="line",
colour="black") +
theme(legend.position = "none") +
ggtitle("Chick Weight over Time by Diet") +
xlab("Time (days)") +
ylab("Weight (grams)")
```
### Tidyverse: Data Wrangling Basics
```{r resettblprint, echo = FALSE}
# Temporarily resetting the print limit
options(tibble.print_min=3, tibble.print_max=3)
```
In this section we will learn how to wrangle (manipulate) datasets using the `tidyverse`
package. Let's start with the `mutate()`, `select()`, `rename()`, `filter()` and `arrange()`
functions.
\
`mutate()`: Adds a new variable (column) or modifies an existing one. We already used this above to create
factor variables.
```{r mutateDemo}
# Added a column
CWm1 <- mutate(CW, weightKg = weight/1000)
CWm1
# Modify an existing column
CWm2 <- mutate(CW, Diet = str_c("Diet ", Diet))
CWm2
```
\
`select()`: Keeps, drops or reorders variables.
```{r selectDemo}
# Drop the weight variable from CWm1 using minus
select(CWm1, -weight)
# Keep variables Time, Diet and weightKg
select(CWm1, Chick, Time, Diet, weightKg)
```
\
`rename()`: Renames variables whilst keeping all variables.
```{r renameDemo}
rename(CW, Group = Diet, Weight = weight)
```
\
`filter()`: Keeps or drops observations (rows).
```{r filterDemo}
filter(CW, Time==21 & weight>300)
```
For comparing values in vectors use: `<` (less than), `>` (greater than), `<=`
(less than and equal to), `>=` (greater than and equal to), `==` (equal to) and `!=`
(not equal to). These can be combined logically using `&` (and) and `|` (or).
\
`arrange()`: Changes the order of the observations.
```{r arrangeDemo}
arrange(CW, Chick, Time)
arrange(CW, desc(weight))
```
What does the `desc()` do? Try using `desc(Time)`.
### The pipe operator `%>%`
In reality you will end up doing multiple data wrangling steps that you want to save.
The pipe operator `%>%` makes your code nice and readable:
```{r pipeOpDemo}
CW21 <- CW %>%
filter(Time %in% c(0, 21)) %>%
rename(Weight = weight) %>%
mutate(Group = factor(str_c("Diet ", Diet))) %>%
select(Chick, Group, Time, Weight) %>%
arrange(Chick, Time)
CW21
```
Hint: To understand the code above we should read the pipe operator ` %>%` as "then".
> Create a new dataset (object) called `CW21` using dataset `CW` ***then***
keep the data for days 0 and 21 ***then*** rename variable `weight` to `Weight`
***then*** create a variable called `Group` ***then*** keep variables `Chick`,
`Group`, `Time` and `Weight` and ***then*** finally arrange the data by
variables `Chick` and `Time`.
This is the same code:
```{r pipeOpExplain, echo=TRUE, eval=FALSE}
CW21 <- CW %>%
filter(., Time %in% c(0, 21)) %>%
rename(., Weight = weight) %>%
mutate(., Group=factor(str_c("Diet ",Diet))) %>%
select(., Chick, Group, Time, Weight) %>%
arrange(., Chick, Time)
```
The pipe operator, `%>%`, replaces the dots (`.`) with whatever is returned from code
preceding it. For example, the dot in `filter(., Time %in% c(0, 21))` is replaced by
`CW`. The output of the `filter(...)` then replaces the dot in
`rename(., Weight = weight)` and so on. Think of it as a data assembly line with
each function doing its thing and passing it to the next.
### The `group_by()` function
From the data visualizations above we concluded that the diet 3 has the highest mean
and diet 4 the least variation. In this section, we will quantify the effects of the
diets using **summmary statistics**. We start by looking at the number of observations
and the mean by **diet** and **time**.
```{r mnsdStatCW}
mnsdCW <- CW %>%
group_by(Diet, Time) %>%
summarise(N = n(), Mean = mean(weight)) %>%
arrange(Diet, Time)
mnsdCW
```
For each distinct combination of `Diet` and `Time`, the chick weight data is summarized
into the number of observations (`N`) and the mean (`Mean`) of `weight`.
**Further summaries:** Let's also calculate the standard deviation, median, minimum and maximum values but only
at days 0 and 21.
```{r}
sumCW <- CW %>%
filter(Time %in% c(0, 21)) %>%
group_by(Diet, Time) %>%
summarise(N = n(),
Mean = mean(weight),
SD = sd(weight),
Median = median(weight),
Min = min(weight),
Max = max(weight)) %>%
arrange(Diet, Time)
sumCW
```
Let's make the summaries "prettier", say, for a report or publication.
```{r prettySum, echo=TRUE, eval=FALSE}
library("knitr") # to use the kable() function
prettySumCW <- sumCW %>%
mutate(`Mean (SD)` = str_c(format(Mean, digits=1),
" (", format(SD, digits=2), ")")) %>%
mutate(Range = str_c(Min, " - ", Max)) %>%
select(Diet, Time, N, `Mean (SD)`, Median, Range) %>%
arrange(Diet, Time) %>%
kable(format = "latex")
prettySumCW
```
```{r prettySum2, echo=FALSE}
library("knitr") # to use the kable() function
if( knitr:::is_latex_output() ) {
prettySumCW <- sumCW %>%
mutate(`Mean (SD)` = str_c(format(Mean, digits=1),
" (", format(SD, digits=2), ")")) %>%
mutate(Range = str_c(Min, " - ", Max)) %>%
select(Diet, Time, N, `Mean (SD)`, Median, Range) %>%
arrange(Diet, Time) %>%
kable(format = "latex")
prettySumCW
} else {
prettySumCW <- sumCW %>%
mutate(`Mean (SD)` = str_c(format(Mean, digits=1),
" (", format(SD, digits=2), ")")) %>%
mutate(Range = str_c(Min, " - ", Max)) %>%
select(Diet, Time, N, `Mean (SD)`, Median, Range) %>%
arrange(Diet, Time) %>%
kable(format = "html")
prettySumCW
}
```
<!-- Eventually you should be able to produce a publication ready version as follows: -->
<!-- ```{r prettyKable, echo = FALSE} -->
<!-- library("knitr") -->
<!-- prettySumCW %>% -->
<!-- kable("latex", booktabs = TRUE, linesep = "", align = "cccrc") %>% -->
<!-- #kable_styling(position = "center") %>% -->
<!-- row_spec(0, bold = TRUE) %>% -->
<!-- row_spec(c(2, 4, 6, 8), background = "lightgray") -->
<!-- # Uncomment below once bugs are fixed - https://github.com/haozhu233/kableExtra/issues/243 & 237 -->
<!-- # column_spec(1, bold = T) %>% -->
<!-- # collapse_rows(columns = 1, latex_hline = "none", valign = "top") -->
<!-- ``` -->
**Interpretation:**
This summary table offers the same interpretation as before, namely that diet 3 has the
highest mean and median weights at day 21 but a higher variation than group 4.
However it should be noted that at day 21, diet 1 lost 4 chicks from 20 that started
and diet 4 lost 1 from 10. This could be a sign of some health related issues.
```{r tblprintdef, echo = FALSE}
# Resetting the print limit back to the defaults
options(tibble.print_min=10, tibble.print_max=20)
```
## Further Links
### Further R-Intros
* https://eddelbuettel.github.io/gsir-te/Getting-Started-in-R.pdf
* https://www.datacamp.com/courses/free-introduction-to-r
* https://swcarpentry.github.io/r-novice-gapminder/
* https://support.rstudio.com/hc/en-us/articles/200526207-Using-Projects
### Version Control (Git/GitHub)
* https://support.rstudio.com/hc/en-us/articles/200532077-Version-Control-with-Git-and-SVN
* http://happygitwithr.com/
* https://www.gitkraken.com/
### R-Ladies
* https://rladies.org/