forked from core-methods-in-edm/assignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment 2-2020.Rmd
417 lines (238 loc) · 11 KB
/
Assignment 2-2020.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
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
---
title: "Assignment 2"
author: "Charles Lang"
date: "September 24, 2020"
output: html_document
---
#Part I
## Data Wrangling
In the hackathon a project was proposed to collect data from student video watching, a sample of this data is available in the file video-data.csv.
stid = student id
year = year student watched video
participation = whether or not the student opened the video
watch.time = how long the student watched the video for
confusion.points = how many times a student rewatched a section of a video
key,points = how many times a student skipped or increased the speed of a video
```{r}
#Install the 'tidyverse' package or if that does not work, install the 'dplyr' and 'tidyr' packages.
#Load the package(s) you just installed
library(tidyverse)
library(tidyr)
library(dplyr)
D1 <- read.csv("video-data.csv", header = TRUE)
#Create a data frame that only contains the years 2018
D2 <- filter(D1, year == 2018)
```
## Histograms
```{r}
#Generate a histogram of the watch time for the year 2018
hist(D2$watch.time)
#Change the number of breaks to 100, do you get the same impression?
hist(D2$watch.time, breaks = 100)
#Cut the y-axis off at 10
hist(D2$watch.time, breaks = 100, ylim = c(0,10))
#Restore the y-axis and change the breaks so that they are 0-5, 5-20, 20-25, 25-35
hist(D2$watch.time, breaks = c(0,5,20,25,35))
```
## Plots
```{r}
#Plot the number of confusion points against the watch time
plot(D1$confusion.points, D1$watch.time)
#Create two variables x & y
x <- c(1,3,2,7,6,4,4)
y <- c(2,4,2,3,2,4,3)
#Create a table from x & y
table1 <- table(x,y)
#Display the table as a Barplot
barplot(table1)
#Create a data frame of the average total key points for each year and plot the two against each other as a lines
D3 <- D1 %>% group_by(year) %>% summarise(mean_key = mean(key.points))
plot(D3$year, D3$mean_key, type = "l", lty = "dashed")
#Create a boxplot of total enrollment for three students
D4 <- filter(D1, stid == 4|stid == 20| stid == 22)
#The drop levels command will remove all the schools from the variable with no data
D4 <- droplevels(D4)
boxplot(D4$watch.time~D4$stid, xlab = "Student", ylab = "Watch Time")
```
## Pairs
```{r}
#Use matrix notation to select columns 2, 5, 6, and 7
D5 <- D1[,c(2,5,6,7)]
#Draw a matrix of plots for every combination of variables
pairs(D5)
```
## Part II
1. Create a simulated data set containing 100 students, each with a score from 1-100 representing performance in an educational game. The scores should tend to cluster around 75. Also, each student should be given a classification that reflects one of four interest groups: sport, music, nature, literature.
```{r}
#rnorm(100, 75, 15) creates a random sample with a mean of 75 and standard deviation of 20
#pmax sets a maximum value, pmin sets a minimum value
#round rounds numbers to whole number values
#sample draws a random samples from the groups vector according to a uniform distribution
#https://www.youtube.com/watch?v=zAYzAZwufKI
#https://www.youtube.com/watch?v=CM7ncRGlViE
library(TruncatedNormal)
#i ended up using these set.seed functions to keep the data from changing every time i ran the block
set.seed(100)
#fancy random student id's
StudID = round(runif(100,100000,999999))
#set.seed(101)
#Score = round(rnorm(100,75,15, max(100), min(0)))
set.seed(101)
#i used the rtnorm function, instead of rnorm, to keep scores from going above 100
Score = round(rtnorm(100, lb = 0, ub = 100, sd = 15, mu = 75))
set.seed(102)
InterestOptions <- c("sport", "music", "nature", "literature")
Interest <- sample(InterestOptions, 100, replace=TRUE)
SDS1 <- data.frame(StudID, Score, Interest)
```
2. Using base R commands, draw a histogram of the scores. Change the breaks in your histogram until you think they best represent your data.
```{r}
hist(Score)
hist(Score, main = "Distribution of Scores", breaks = 100)
#https://stackoverflow.com/questions/38810453/how-to-plot-a-histogram-with-different-colors-in-r
hist(Score, main = "Distribution of Scores", breaks = 100, col = 3)
#colors http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf
hist(Score, main = "Distribution of Scores", breaks = c(seq(0,100,10)), col = c("steelblue", "steelblue1", "steelblue2", "steelblue3", "steelblue4"))
#couldn't get this one going... yet
#library(ggplot2)
#
#ScoreGroup = ifelse(Score < 70, "<70", ifelse(Score < 91, "70-90", ">90"))
#
#ggplot(Score, aes(Score, fill = ScoreGroup)) + geom.histogram() +
# scale_fill_manual(values = c("<70" = "#CD7F32",
# "70-90" = "#C0C0C0",
# ">90" = "gold"))
```
3. Create a new variable that groups the scores according to the breaks in your histogram.
```{r}
#cut() divides the range of scores into intervals and codes the values in scores according to which interval they fall. We use a vector called `letters` as the labels, `letters` is a vector made up of the letters of the alphabet.
#ScoreCuts <- cut(Score, breaks = c(seq(0,100,10)), labels = "A","B","C","D","E","F","G","H","I","K")
SDS1$ScoreCuts <- cut(Score, breaks = c(seq(0,100,10)), labels = c("A","B","C","D","E","F","G","H","I","K"))
#thanks for the letters command... the above looks a bit archaic in comparison!
```
4. Now using the colorbrewer package (RColorBrewer; http://colorbrewer2.org/#type=sequential&scheme=BuGn&n=3) design a pallette and assign it to the groups in your data on the histogram.
```{r}
library(RColorBrewer)
display.brewer.all()
#Let's look at the available palettes in RColorBrewer
#The top section of palettes are sequential, the middle section are qualitative, and the lower section are diverging.
#Make RColorBrewer palette available to R and assign to your bins
#Use named palette in histogram
HistPalette <- brewer.pal(10, "Set3")
hist(SDS1$Score, main = "Distribution of Scores", xlab = "Scores", ylab = "Frequency", col=HistPalette)
```
5. Create a boxplot that visualizes the scores for each interest group and color each interest group a different color.
```{r}
#Make a vector of the colors from RColorBrewer
library(RColorBrewer)
InterestColors <- brewer.pal(4, "Accent")
boxplot(Score ~ Interest, #x and y
SDS1, #dataframe
col = InterestColors, #color palette
main = "Boxplot of Interest Groups") #title
Pal <- colorRampPalette(c("lightblue", "darkblue"))
PalRange <- Pal(4)
boxplot(Score ~ Interest, #x and y
SDS1, #dataframe
col = PalRange, #color palette
main = "Boxplot of Interest Groups") #title
```
6. Now simulate a new variable that describes the number of logins that students made to the educational game. They should vary from 1-25.
```{r}
SDS1$Logins <- sample(1:25, 100, replace = TRUE)
```
7. Plot the relationships between logins and scores. Give the plot a title and color the dots according to interest group.
```{r}
Pal <- colorRampPalette(c("red", "blue"))
PalRange <- Pal(4)
#View(PalRange)
SDS1$InterestColor <- 0
SDS1$InterestColor[SDS1$Interest=="sport"] <- "#FF0000"
SDS1$InterestColor[SDS1$Interest=="music"] <- "#AA0055"
SDS1$InterestColor[SDS1$Interest=="nature"] <- "#5500AA"
SDS1$InterestColor[SDS1$Interest=="literature"] <- "#0000FF"
plot(SDS1$Logins, SDS1$Score,
main = "Scores vs Logins",
xlab = "Logins", ylab = "Scores",
col=SDS1$InterestColor,
pch = 19)
#i believe there's a problem with the plot above, i intended that the plot would color the datapoints with the color specified in $InterestColor... when i create a plot with a filter for 'interest==sport'... all the points should be the same color... and they're not... :(
plot(SDS1$Logins[SDS1$Interest=="sport"],
SDS1$Score[SDS1$Interest=="sport"],
main = "Scores vs Logins Interest=sport",
xlab = "Logins", ylab = "Scores",
col=SDS1$InterestColor,
pch = 19)
```
8. R contains several inbuilt data sets, one of these in called AirPassengers. Plot a line graph of the the airline passengers over time using this data set.
```{r}
data.frame(AirPassengers)
plot(AirPassengers, col="red")
```
9. Using another inbuilt data set, iris, plot the relationships between all of the variables in the data set. Which of these relationships is it appropraiet to run a correlation on?
```{r}
data.frame(iris)
pairs(iris)
#it would seem comparing petal.length to petal.width produce the most clustered arrangement... the longer larger the petal.length, the larger the petal.width (i.e. smooth line of data)... i would start there to look for correlations.
```
# Part III - Analyzing Swirl
## Data
In this repository you will find data describing Swirl activity from the class so far this semester. Please connect RStudio to this repository.
### Instructions
1. Insert a new code block
```{r}
```
2. Create a data frame from the `` file called `DF1`
The variables are:
`course_name` - the name of the R course the student attempted
`lesson_name` - the lesson name
`question_number` - the question number attempted
`correct` - whether the question was answered correctly
`attempt` - how many times the student attempted the question
`skipped` - whether the student skipped the question
`datetime` - the date and time the student attempted the question
`hash` - anonymyzed student ID
```{r}
DF1 <- read.csv("swirl-data.csv",TRUE)
```
3. Create a new data frame that only includes the variables `hash`, `lesson_name` and `attempt` called `DF2`
```{r}
DF2 <- data.frame(DF1$hash, DF1$lesson_name, DF1$attempt)
names(DF2) <- c("hash", "lesson_name", "attempt")
```
4. Use the `group_by` function to create a data frame that sums all the attempts for each `hash` by each `lesson_name` called `DF3`
```{r}
GroupBy_LessonName <- group_by(DF2, lesson_name)
DF3 <- summarise(GroupBy_LessonName,
TotalAttempts = sum(attempt, na.rm = TRUE))
```
5. On a scrap piece of paper draw what you think `DF3` would look like if all the lesson names were column names
6. Convert `DF3` to this format
```{r}
#needed to add a blank column for the plot command
DF3$blank <-0
plot(DF3)
hist(DF3$TotalAttempts)
#this section is still... no bueno...
#stay tuned
```
7. Create a new data frame from `DF1` called `DF4` that only includes the variables `hash`, `lesson_name` and `correct`
```{r}
#stay tuned
```
8. Convert the `correct` variable so that `TRUE` is coded as the **number** `1` and `FALSE` is coded as `0`
```{r}
#stay tuned
```
9. Create a new data frame called `DF5` that provides a mean score for each student on each course
```{r}
#stay tuned
```
10. **Extra credit** Convert the `datetime` variable into month-day-year format and create a new data frame (`DF6`) that shows the average correct for each day
```{r}
#stay tuned
```
Finally use the knitr function to generate an html document from your work. Commit, Push and Pull Request your work back to the main branch of the repository. Make sure you include both the .Rmd file and the .html file.
```{r}
#stay tuned
```