From d8aa5c21d78712d6d4e84dbbe1747f4c834b799e Mon Sep 17 00:00:00 2001 From: hsiaotingyang Date: Tue, 1 Oct 2019 13:48:44 -0400 Subject: [PATCH] Class activity 2 --- class-activity-2.Rmd | 41 ++- class-activity-2.html | 569 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 598 insertions(+), 12 deletions(-) create mode 100644 class-activity-2.html diff --git a/class-activity-2.Rmd b/class-activity-2.Rmd index e547dd9..cfc3432 100644 --- a/class-activity-2.Rmd +++ b/class-activity-2.Rmd @@ -1,6 +1,6 @@ --- title: "intro to viz" -author: "Charles Lang" +author: "Hsiao Yang" date: "September 26, 2019" output: html_document --- @@ -17,7 +17,7 @@ D2 <- filter(D1, schoolyear == 20112012) ```{r} #Generate a histogramof the percentage of free/reduced lunch students (frl_percent) at each school -hist() + #Change the number of breaks to 100, do you get the same impression? @@ -80,13 +80,18 @@ pairs(D5) #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 - - +set.seed(321) +id<- seq(1,100,1) +student_score<-round(pmax(1,pmin(100, rnorm(100,75,15)))) +interest<- c("sport", "music", "nature", "literature") +Interest <- sample(interest,100, replace=TRUE) +df <- data.frame(id,student_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(df$student_score, breaks = 5) ``` @@ -95,6 +100,8 @@ pairs(D5) ```{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. +letters<- c("F", "D", "C", "B", "A") +df$letter_grade<-cut(df$student_score, breaks = c(100,90,80,70,60,0), labels = letters) ``` @@ -103,12 +110,14 @@ pairs(D5) ```{r} library(RColorBrewer) #Let's look at the available palettes in RColorBrewer - + colors <- brewer.pal(5, "BuPu") #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 +#Use named palette in histogram + +hist(df$student_score, breaks = 5, col =colors) ``` @@ -116,37 +125,45 @@ library(RColorBrewer) ```{r} #Make a vector of the colors from RColorBrewer - +boxplot(df$student_score~df$Interest, col =colors) ``` 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} +df$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} - - +plot(df$logins, df$student_score, col=df$Interest, 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} - +plot.ts(AirPassengers) ``` 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} - +I <- iris +plot(I) +# sepal.length and petal.length; petal.width and petal.length ``` - +```{r} +cor.test(I$Sepal.Length, I$Petal.Length) +``` +```{r} +cor.test(I$Petal.Width, I$Petal.Length) +``` + 10. Finally use the knitr function to generate an html document from your work. If you have time, try to change some of the output using different commands from the RMarkdown cheat sheet. 11. Commit, Push and Pull Request your work back to the main branch of the repository diff --git a/class-activity-2.html b/class-activity-2.html new file mode 100644 index 0000000..16590cb --- /dev/null +++ b/class-activity-2.html @@ -0,0 +1,569 @@ + + + + + + + + + + + + + + + + +intro to viz + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + +

#Input

+
D1 <- read.csv("School_Demographics_and_Accountability_Snapshot_2006-2012.csv", header = TRUE, sep = ",")
+
+#Create a data frame only contains the years 2011-2012
+library(dplyr)
+D2 <- filter(D1, schoolyear == 20112012)
+

#Histograms

+
#Generate a histogramof the percentage of free/reduced lunch students (frl_percent) at each school
+
+
+
+#Change the number of breaks to 100, do you get the same impression?
+
+hist(D2$frl_percent, breaks = 100)
+

+
#Cut the y-axis off at 30
+
+hist(D2$frl_percent, breaks = 100, ylim = c(0,30))
+

+
#Restore the y-axis and change the breaks so that they are 0-10, 10-20, 20-80, 80-100
+
+hist(D2$frl_percent, breaks = c(0,10,20,80,100))
+

+

#Plots

+
#Plot the number of English language learners (ell_num) by Computational Thinking Test scores (ctt_num) 
+
+plot(D2$ell_num, D2$ctt_num)
+

+
#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 enrollment for each year and plot the two against each other as a lines
+
+library(tidyr)
+D3 <- D1 %>% group_by(schoolyear) %>% summarise(mean_enrollment = mean(total_enrollment))
+
+plot(D3$schoolyear, D3$mean_enrollment, type = "l", lty = "dashed")
+

+
#Create a boxplot of total enrollment for three schools
+D4 <- filter(D1, DBN == "31R075"|DBN == "01M015"| DBN == "01M345")
+#The drop levels command will remove all the schools from the variable with not data  
+D4 <- droplevels(D4)
+boxplot(D4$total_enrollment ~ D4$DBN)
+

#Pairs

+
#Use matrix notation to select columns 5,6, 21, 22, 23, 24
+D5 <- D2[,c(5,6, 21:24)]
+#Draw a matrix of plots for every combination of variables
+pairs(D5)
+

# Exercise

+
    +
  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.
  2. +
+
#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
+set.seed(321)
+id<- seq(1,100,1)
+student_score<-round(pmax(1,pmin(100, rnorm(100,75,15))))
+interest<- c("sport", "music", "nature", "literature")
+Interest <- sample(interest,100, replace=TRUE)
+df <- data.frame(id,student_score,Interest)
+
    +
  1. Using base R commands, draw a histogram of the scores. Change the breaks in your histogram until you think they best represent your data.
  2. +
+
hist(df$student_score, breaks = 5)
+

+
    +
  1. Create a new variable that groups the scores according to the breaks in your histogram.
  2. +
+
#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.
+letters<- c("F", "D", "C", "B", "A")
+df$letter_grade<-cut(df$student_score, breaks = c(100,90,80,70,60,0), labels = letters)
+
    +
  1. 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.
  2. +
+
library(RColorBrewer)
+#Let's look at the available palettes in RColorBrewer
+ colors <- brewer.pal(5, "BuPu")
+#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
+ 
+hist(df$student_score, breaks = 5, col =colors)
+

+
    +
  1. Create a boxplot that visualizes the scores for each interest group and color each interest group a different color.
  2. +
+
#Make a vector of the colors from RColorBrewer
+boxplot(df$student_score~df$Interest, col =colors)
+

+
    +
  1. Now simulate a new variable that describes the number of logins that students made to the educational game. They should vary from 1-25.
  2. +
+
df$logins<- sample(1:25,100,replace=TRUE)
+
    +
  1. Plot the relationships between logins and scores. Give the plot a title and color the dots according to interest group.
  2. +
+
plot(df$logins, df$student_score, col=df$Interest, pch=19)
+

+
    +
  1. 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.
  2. +
+
plot.ts(AirPassengers)
+

+
    +
  1. 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?
  2. +
+
I <- iris
+plot(I)
+

+
# sepal.length and petal.length; petal.width and petal.length
+
cor.test(I$Sepal.Length, I$Petal.Length)
+
## 
+##  Pearson's product-moment correlation
+## 
+## data:  I$Sepal.Length and I$Petal.Length
+## t = 21.646, df = 148, p-value < 2.2e-16
+## alternative hypothesis: true correlation is not equal to 0
+## 95 percent confidence interval:
+##  0.8270363 0.9055080
+## sample estimates:
+##       cor 
+## 0.8717538
+
cor.test(I$Petal.Width, I$Petal.Length)
+
## 
+##  Pearson's product-moment correlation
+## 
+## data:  I$Petal.Width and I$Petal.Length
+## t = 43.387, df = 148, p-value < 2.2e-16
+## alternative hypothesis: true correlation is not equal to 0
+## 95 percent confidence interval:
+##  0.9490525 0.9729853
+## sample estimates:
+##       cor 
+## 0.9628654
+
    +
  1. Finally use the knitr function to generate an html document from your work. If you have time, try to change some of the output using different commands from the RMarkdown cheat sheet.

  2. +
  3. Commit, Push and Pull Request your work back to the main branch of the repository

  4. +
+ + + + +
+ + + + + + + + + + + + + + +