-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathExercises.Rmd
More file actions
153 lines (89 loc) · 4.99 KB
/
Exercises.Rmd
File metadata and controls
153 lines (89 loc) · 4.99 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
# Programming Exercises
We want to practice what we have learned so far.
Work in the Rstudio editor and write a script that also serves as documentation. Try to write clean code (readable and as simple as possible):
* use consistent variable names (e.g. PropBlond or Prop_Blond)
* indent your code
* write functions
***
Before starting let's install the `ggplot2` package:
```{r, eval=FALSE}
install.packages("ggplot2")
```
Load the `ggplot2` package as we will use some of its data:
```{r}
library(ggplot2)
```
***
# Data Set 1: Sleep in mammals
The msleep data set is part of the `ggplot2` package. It contains a mammals sleep dataset (see ?msleep for details).
First let's first look at the structure of the dataset:
```{r}
str(msleep)
```
### 1. Which mammal sleeps the least, the most?
### 2. Is there a association between total sleep duration and body weight (bodywt)?
### 3. Make a scatterplot of sleep_total vs. sleep_rem
### 4. Make point size proportional to log(body mass)
### 5. Add a OLS (Ordinary least square) regression line
### 6. Color-code the points according to vore. Does the scaling of REM & total sleep differ with diet?
### (advanced) 7. Make the figure from the question 6 in publication quality (Axes labels, font sizes, ..)
Original publication in [PNAS](http://www.pnas.org/content/104/3/1051.abstract)
****
## Data Set 2: Baby names
(Data set 1 is borrowed from a [lecture](http://stat405.had.co.nz/lectures/11-adv-data-manip.pdf) by Hadley Wickham)
The data set contains the top 1000 male and female baby names in the US, from
1880 to 2008 (1000* 2 * 129 = 258,000 records). All names with more than 5 uses
are given.
It contains 5 variables: year, name, soundex, sex and proportion
Download bnames2.csv.bz2 from http://stat405.had.co.nz/data/bnames2.csv.bz2
(Under Windows download the zipped file [bnames2.csv.zip](https://www.dropbox.com/s/hax6jullt8a9kd7/bnames2.csv.zip?dl=0) and extract it before reading)
You can directly read in the compressed file like (on Linux and Mac OS)
```{r eval=FALSE}
bnames <- read.csv("bnames2.csv.bz2")
```
Also load a file containing the total number of birth per years (for boys and girls separately)
```{r eval=FALSE}
births <- read.csv("http://stat405.had.co.nz/data/births.csv")
```
Now it's your turn.
### 1. Extract your name from the dataset. Plot the trend over time.
### 2. Use the soundex variable to extract all names that sound like yours. Plot the trend over time.
### 3. Find out the most frequently used similar sounding name
### (advanced) 4. Which boy and girl name was used most over the whole time?
### (advanced) 5. Did first names became shorter over time?
### (advanced) 6. Which names became very rare after 1944?
### (advanced) 7. Think of another question you could answer with the dataset. E.g. Identify the most popular firstname in 1980ies the or identify the most popular name that was used for boys and girls.
***
## Data Set 3: Hair and Eye Color
The next data set is the distribution of hair and eye color and sex in 592 statistics students stored in the table `HairEyeColor` (see ?HairEyeColor for details).
We load the data set with the data() function and have a look at the structure using str().
```{r}
data("HairEyeColor")
str(HairEyeColor)
```
Now it's your turn.
### 1. What class of data is HairEyeColor?
### 2. Visualize the data
Use the mosaicplot() function to plot categorical data
### 3. Look at the mosaicplot() help
The most important parts are at the top and bottom. Try to understand what similar functions are available (<See Also>). Run the mosaicplot() examples.
With a new function I often just look at the examples or run them. Its often faster to understand what it does than reading the whole help entry.
`example(mosaicplot)` runs the examples of any function
***
# Data Set 4: Movie ratings
The movies data set is from the `ggplot2` package. The internet movie database,
[http://imdb.com/](http://imdb.com/), is a website devoted to collecting movie
data supplied by studios and fans (See ?movies for details).
The data set contains data for 58'788 movies, namely the title of the movie,
year of release, budget, length, rating and genre.
### 1. Look at the structure of `movies` using function str() or head(). Make a histogram of the rating.
### 2. Do old movies perform better or worse than recent movies?
Some movies obtained less than 10 votes. Remove them and repeat the plotting. Do you see a change?
### 3. Does the movie genre influence the rating?
Add a new variable Genre.simple containing genre information like this:
movies$Genre.simple <- ifelse(movies$Action == 1, "Action", ifelse(movies$Comedy == 1, "Comedy", ifelse(movies$Drama == 1, "Drama", "other")))
Then plot the rating per genre
By default genres are alphabetically ordered. Let's order the genres according to their median rating (tip: use factors and function factor()).
### 4. Which movie is the longest and how long is it?
### 5. Does the movie length have an impact on its rating?
Tip: use the function cut to make categories.