diff --git a/Assignment II VM.Rmd b/Assignment II VM.Rmd new file mode 100644 index 0000000..e73e90d --- /dev/null +++ b/Assignment II VM.Rmd @@ -0,0 +1,250 @@ +--- +title: "Assignment II" +author: "Vidya Madhavan" +date: "04/10/2020" +output: html_document +--- + +```{r} +library (tidyverse) +library (tidyr) +library (dplyr) + +D1 <- read.csv("video-data.csv", header = TRUE) + +D2 <- filter(D1, year == 2018) +``` +#Part I - Data Wrangling + +## Histograms + +```{r} +hist(D2$watch.time) +#Graph is more even. It gives a general overview of the watching pattern amongst subjects. + +hist(D2$watch.time, breaks = 15) + +#This graph seems to give a better understanding of the watch time distribution of those that decided to watch the video. + +hist(D2$watch.time, breaks = 100) + +#The graph I feel is unnecessarily complex while more or less delivering the same information as the graph with fewer breaks (15). I would prefer to use a lesser number of breaks as to not be distracted the smaller changes in data, while there is a larger trend to be observed. +``` + +```{r} +hist(D2$watch.time, breaks = 100, ylim = c(0,10)) +``` + +```{r} +hist(D2$watch.time, breaks = c(0, 5, 20, 25, 35)) + +# Tried to revert the graph to original Y axis (of frequency) just to try out the Prob = False and Freq = TRUE commands. The outputs reflects incorrect area. Hence Y axis is displayed in Density rather than Frequency. +``` + +##Plots + +```{r} + +plot(D1$confusion.points, D1$watch.time) + +x <- c( 1, 3, 2, 7, 6, 4, 4) +y <- c( 2, 4, 2, 3, 2, 4, 3) + +table1 <- table(x, y) + +barplot( table1) +``` + +```{r} +D3 <- D1 %>% group_by (year) %>% summarise(mean_key = mean(key.points)) + +options (dplyr.summarise.inform = FALSE) + +plot(D3$year, D3$mean_key, type = "l", lty = "dashed" ) +``` + +## Box Plot +```{r} +D4 <- filter (D1, stid == 4| stid == 20| stid == 22) +D4 <- droplevels(D4) +boxplot (D4$watch.time~D4$stid, xlab = "Student ID", ylab = "Watch Time", main = "Boxplot of student watch time", col = "blue") +``` + +##Plot Matrix +```{r} +D5 <- D1 [,c(2,5,6,7)] +pairs((D5), col = "red", main = "Matrix plots for every combination of variables") +``` + +#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} +scores <- rnorm(100, 75, 15) + +scores <- pmax(scores, 1) +scores <- pmin (scores, 100) +scores <- round(scores, digits = 0) +``` + +```{r} +intgroup <- sample(c('sport', 'music', 'nature', 'literature'), 100, replace = TRUE) +d1 <-data.frame(scores,intgroup) +``` +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(d1$scores) + +hist(d1$scores, breaks =80, ylim = c(0,5)) + +hist(d1$scores, breaks = c(30,40,50,60,70,80,90,100)) +``` +3. Create a new variable that groups the scores according to the breaks in your histogram. +```{r} +range <- cut(scores, c(30,40,50,60,70,80,90,100), labels = letters[1:7]) +``` +4. Now using the colorbrewer package design a pallette and assign it to the groups in your data on the histogram. +```{r} +library(RColorBrewer) + +display.brewer.pal(n=7, name="Pastel1") +hist(d1$scores, col= brewer.pal(n=7, name = "Pastel1")) + +``` +5. Create a boxplot that visualizes the scores for each interest group and color each interest group a different color. +```{r} +boxplot(d1$scores~d1$intgroup, xlab = "Score", ylab = "Interest Group", col = brewer.pal(n=7, name = "Pastel1")) +``` + +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} +log.num <- rnorm(100, 12.5, 5) + +log.num <- pmax(log.num, 1) +log.num <- pmin(log.num, 25) + +log.num <- round(log.num, digits = 0) +``` +7. Plot the relationships between logins and scores. Give the plot a title and color the dots according to interest group. +```{r} +d1$log.num <- log.num + +plot(d1$log.num~d1$scores, xlab = "Number of Logins", ylab = "Scores", col = brewer.pal(n=7, name = "Pastel1")) +``` +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} +library(datasets) +data("AirPassengers") +summary(AirPassengers) +A1 <- AirPassengers +plot(A1, type = "o", col = "purple", ylab = "Airline Passengers") +``` + +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 appropriate to run a correlation on? +```{r} +library(datasets) +data("iris") +pairs(iris, col ="red") +# It would be appropriate to run correlations on petal length and petal width, as well as sepal length and sepal widths for each of the species. +``` + + +# 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 +2. Create a data frame from the `swirl-data.csv` 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", header = TRUE) + +``` +3. Create a new data frame that only includes the variables `hash`, `lesson_name` and `attempt` called `DF2` + +```{r} + + DF2 <- data.frame(DF1[,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} + + DF3 <- DF2 %>% group_by(hash, lesson_name) %>% summarise(sum_attempt = sum(attempt)) + +``` + + 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 + 6. Convert `DF3` to this format + +```{r} + + newDF3 <- as.data.frame(t(DF3)) + +``` + + 7. Create a new data frame from `DF1` called `DF4` that only includes the variables `hash`, `lesson_name` and `correct` + +```{r} + + DF4 <- data.frame(DF1$hash, DF1$lesson_name, DF1$correct) + + +``` + + 8. Convert the `correct` variable so that `TRUE` is coded as the **number** `1` and `FALSE` is coded as `0` + +```{r} + + DF4$DF1.correct <- ifelse(DF4$DF1.correct == 'TRUE', 1, 0) + + DF4$DF1.correct <- as.numeric(DF4$DF1.correct) + +``` + + 9. Create a new data frame called `DF5` that provides a mean score for each student on each course + +```{r} + + DF5 <- DF1 %>% group_by(hash) %>% summarise(mean_score = mean(scores)) + + +``` + + 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} +DF6 <- data.frame(DF1$hash, DF1$lesson_name, DF1$correct, DF1$datetime) + +DF6$DF1.correct <- ifelse(DF6$DF1.correct == 'TRUE', 1, 0) + +DF6$DF1.correct <- as.numeric(DF6$DF1.correct) + +DF6$DF1.datetime <- as.Date(as.POSIXct(DF6$DF1.datetime, origin="1970-01-01")) + +DF6 <- DF6 %>% group_by(DF1.datetime) %>% summarise(avg_correct = sum(DF1.correct)) + +``` +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. + diff --git a/Assignment-II-VM.html b/Assignment-II-VM.html new file mode 100644 index 0000000..f0daf39 --- /dev/null +++ b/Assignment-II-VM.html @@ -0,0 +1,620 @@ + + + + +
+ + + + + + + + + + +library (tidyverse)
+## ── Attaching packages ───────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
+## ✓ ggplot2 3.3.2 ✓ purrr 0.3.4
+## ✓ tibble 3.0.3 ✓ dplyr 1.0.2
+## ✓ tidyr 1.1.2 ✓ stringr 1.4.0
+## ✓ readr 1.3.1 ✓ forcats 0.5.0
+## ── Conflicts ──────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
+## x dplyr::filter() masks stats::filter()
+## x dplyr::lag() masks stats::lag()
+library (tidyr)
+library (dplyr)
+
+D1 <- read.csv("video-data.csv", header = TRUE)
+
+D2 <- filter(D1, year == 2018)
+#Part I - Data Wrangling
+hist(D2$watch.time)
+#Graph is more even. It gives a general overview of the watching pattern amongst subjects.
+
+hist(D2$watch.time, breaks = 15)
+#This graph seems to give a better understanding of the watch time distribution of those that decided to watch the video.
+
+hist(D2$watch.time, breaks = 100)
+#The graph I feel is unnecessarily complex while more or less delivering the same information as the graph with fewer breaks (15). I would prefer to use a lesser number of breaks as to not be distracted the smaller changes in data, while there is a larger trend to be observed.
+hist(D2$watch.time, breaks = 100, ylim = c(0,10))
+hist(D2$watch.time, breaks = c(0, 5, 20, 25, 35))
+# Tried to revert the graph to original Y axis (of frequency) just to try out the Prob = False and Freq = TRUE commands. The outputs reflects incorrect area. Hence Y axis is displayed in Density rather than Frequency.
+##Plots
+plot(D1$confusion.points, D1$watch.time)
+x <- c( 1, 3, 2, 7, 6, 4, 4)
+y <- c( 2, 4, 2, 3, 2, 4, 3)
+
+table1 <- table(x, y)
+
+barplot( table1)
+D3 <- D1 %>% group_by (year) %>% summarise(mean_key = mean(key.points))
+## `summarise()` ungrouping output (override with `.groups` argument)
+options (dplyr.summarise.inform = FALSE)
+
+plot(D3$year, D3$mean_key, type = "l", lty = "dashed" )
+D4 <- filter (D1, stid == 4| stid == 20| stid == 22)
+D4 <- droplevels(D4)
+boxplot (D4$watch.time~D4$stid, xlab = "Student ID", ylab = "Watch Time", main = "Boxplot of student watch time", col = "blue")
+##Plot Matrix
+D5 <- D1 [,c(2,5,6,7)]
+pairs((D5), col = "red", main = "Matrix plots for every combination of variables")
+#Part II
+scores <- rnorm(100, 75, 15)
+
+scores <- pmax(scores, 1)
+scores <- pmin (scores, 100)
+scores <- round(scores, digits = 0)
+intgroup <- sample(c('sport', 'music', 'nature', 'literature'), 100, replace = TRUE)
+d1 <-data.frame(scores,intgroup)
+hist(d1$scores)
+hist(d1$scores, breaks =80, ylim = c(0,5))
+hist(d1$scores, breaks = c(30,40,50,60,70,80,90,100))
+ 3. Create a new variable that groups the scores according to the breaks in your histogram.
range <- cut(scores, c(30,40,50,60,70,80,90,100), labels = letters[1:7])
+library(RColorBrewer)
+
+display.brewer.pal(n=7, name="Pastel1")
+hist(d1$scores, col= brewer.pal(n=7, name = "Pastel1"))
+ 5. Create a boxplot that visualizes the scores for each interest group and color each interest group a different color.
boxplot(d1$scores~d1$intgroup, xlab = "Score", ylab = "Interest Group", col = brewer.pal(n=7, name = "Pastel1"))
+log.num <- rnorm(100, 12.5, 5)
+
+log.num <- pmax(log.num, 1)
+log.num <- pmin(log.num, 25)
+
+log.num <- round(log.num, digits = 0)
+d1$log.num <- log.num
+
+plot(d1$log.num~d1$scores, xlab = "Number of Logins", ylab = "Scores", col = brewer.pal(n=7, name = "Pastel1"))
+ 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.
library(datasets)
+data("AirPassengers")
+summary(AirPassengers)
+## Min. 1st Qu. Median Mean 3rd Qu. Max.
+## 104.0 180.0 265.5 280.3 360.5 622.0
+A1 <- AirPassengers
+plot(A1, type = "o", col = "purple", ylab = "Airline Passengers")
+library(datasets)
+data("iris")
+pairs(iris, col ="red")
+# It would be appropriate to run correlations on petal length and petal width, as well as sepal length and sepal widths for each of the species.
+In this repository you will find data describing Swirl activity from the class so far this semester. Please connect RStudio to this repository.
+swirl-data.csv file called DF1The 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
DF1 <- read.csv("swirl-data.csv", header = TRUE)
+hash, lesson_name and attempt called DF2 DF2 <- data.frame(DF1[,c('hash', 'lesson_name', 'attempt')])
+group_by function to create a data frame that sums all the attempts for each hash by each lesson_name called DF3 DF3 <- DF2 %>% group_by(hash, lesson_name) %>% summarise(sum_attempt = sum(attempt))
+On a scrap piece of paper draw what you think DF3 would look like if all the lesson names were column names
Convert DF3 to this format
+
Convert DF3 to this format
newDF3 <- as.data.frame(t(DF3))
+DF1 called DF4 that only includes the variables hash, lesson_name and correct DF4 <- data.frame(DF1$hash, DF1$lesson_name, DF1$correct)
+correct variable so that TRUE is coded as the number 1 and FALSE is coded as 0 DF4$DF1.correct <- ifelse(DF4$DF1.correct == 'TRUE', 1, 0)
+
+ DF4$DF1.correct <- as.numeric(DF4$DF1.correct)
+DF5 that provides a mean score for each student on each course DF5 <- DF1 %>% group_by(hash) %>% summarise(mean_score = mean(scores))
+datetime variable into month-day-year format and create a new data frame (DF6) that shows the average correct for each dayDF6 <- data.frame(DF1$hash, DF1$lesson_name, DF1$correct, DF1$datetime)
+
+DF6$DF1.correct <- ifelse(DF6$DF1.correct == 'TRUE', 1, 0)
+
+DF6$DF1.correct <- as.numeric(DF6$DF1.correct)
+
+DF6$DF1.datetime <- as.Date(as.POSIXct(DF6$DF1.datetime, origin="1970-01-01"))
+
+DF6 <- DF6 %>% group_by(DF1.datetime) %>% summarise(avg_correct = sum(DF1.correct))
+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.
+