Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 155 additions & 15 deletions Assignment 2-2020.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ 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))
D3 <- D1 %>% group_by(year) %>% summarise(mean_key = mean(key.points)) %>% print

plot(D3$year, D3$mean_key, type = "l", lty = "dashed")

Expand All @@ -87,7 +87,6 @@ D5 <- D1[,c(2,5,6,7)]
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}
Expand All @@ -96,21 +95,75 @@ pairs(D5)
#round() rounds numbers to whole number values
#sample() draws a random samples from the groups vector according to a uniform distribution

set.seed(1)
s <-rep(NA,100)
for(i in 1:100){
repeat{
s[i]<-round(rnorm(1,mean=75,sd=20))
if(s[i]<=100 && s[i]>=0) break
}
}

set.seed(2)
m <- rep(NA,100)
for(i in 1:100){
repeat{
m[i]<-round(rnorm(1,mean=75,sd=20))
if(m[i]<=100 && m[i]>=0) break
}
}

set.seed(3)
n <-rep(NA,100)
for(i in 1:100){
repeat{
n[i]<-round(rnorm(1,mean=75,sd=20))
if(n[i]<=100 && n[i]>=0) break
}
}

set.seed(4)
l <-rep(NA,100)
for(i in 1:100){
repeat{
l[i]<-round(rnorm(1,mean=75,sd=20))
if(l[i]<=100 && l[i]>=0) break
}
}


performance <- data.frame(
sport = s,
music= m,
nature = n,
literature = l)

performance

```

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(s, breaks = c(0,60,70,80,90,100),main="Histogram of Sport")
hist(m, breaks = c(0,60,70,80,90,100),main="Histogram of Music")
hist(n, breaks = c(0,60,70,80,90,100),main="Histogram of Nature")
hist(l, breaks = c(0,60,70,80,90,100),main="Histogram of Literature")
```


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.

scut <- cut(s,breaks=c(0,60,70,80,90,100),labels = c("F","D","C","B","A"))
scut
mcut <- cut(m,breaks=c(0,60,70,80,90,100),labels = c("F","D","C","B","A"))
mcut
ncut <- cut(n,breaks=c(0,60,70,80,90,100),labels = c("F","D","C","B","A"))
ncut
lcut <- cut(l,breaks=c(0,60,70,80,90,100),labels = c("F","D","C","B","A"))
lcut
```

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.
Expand All @@ -123,43 +176,103 @@ library(RColorBrewer)
#Make RColorBrewer palette available to R and assign to your bins

#Use named palette in histogram

hist(s, breaks = c(0,60,70,80,90,100),main="Histogram of Sport", col=brewer.pal(5,"Blues"))
hist(m, breaks = c(0,60,70,80,90,100),main="Histogram of Music", col=brewer.pal(5,"Greens"))
hist(n, breaks = c(0,60,70,80,90,100),main="Histogram of Nature", col=brewer.pal(5,"Greys"))
hist(l, breaks = c(0,60,70,80,90,100),main="Histogram of Literature", col=brewer.pal(5,"Reds"))
```


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

boxplot(performance, data = performance, xlab = "Interest Groups", ylab = "Scores", col=brewer.pal(4,"Spectral"))
```


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}

logins = sample(1:25,100,replace=TRUE)
logins
```

7. Plot the relationships between logins and scores. Give the plot a title and color the dots according to interest group.

```{r}
library(ggplot2)
library(reshape2)

df <- data.frame(logins = logins, Sport = s, Music = m, Nature = n, Literature = l)

plotdf <- melt(df, id.vars = "logins")
print(ggplot(plotdf, aes(value, logins, colour = variable)) + geom_point() +
ggtitle(paste0("Plot of Login Times and Scores of Performance")) +
theme(legend.position = "bottom")+
guides(fill = guide_legend(reverse=TRUE)))

```

```{r}
library(hrbrthemes)
ggplot(df, aes(x=logins, y=s, alpha=scut)) +
geom_point(size=1, color = ("Red")) +
theme_ipsum() +
ggtitle("Plot of Login Times and Scores of Sport") +
xlab("Login times") + ylab("Score of Sport")

ggplot(df, aes(x=logins, y=m, alpha=mcut)) +
geom_point(size=1, color="green") +
theme_ipsum() +
ggtitle("Plot of Login Times and Scores of Music") +
xlab("Login times") + ylab("Score of Music")

ggplot(df, aes(x=logins, y=n, alpha=ncut)) +
geom_point(size=1, color="blue") +
theme_ipsum() +
ggtitle("Plot of Login Times and Scores of Nature") +
xlab("Login times") + ylab("Score of Nature")

ggplot(df, aes(x=logins, y=l, alpha=lcut)) +
geom_point(size=1, color="purple") +
theme_ipsum() +
ggtitle("Plot of Login Times and Scores of Literature") +
xlab("Login times") + ylab("Score of Literature")
```


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("AirPassengers")
air <- AirPassengers
plot(air, ylab="Passengers (1000s)", type="o")
```


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?
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}
data("iris")
ir = iris
ir

plot(ir)

pairs(iris[1:4], main="Plot of Iris - 3 Species", pch=21, bg = c("Mediumvioletred", "lightpink", "plum")[unclass(iris$Species)])


panel.pearson <- function(x, y, ...) {
horizontal <- (par("usr")[1] + par("usr")[2]) / 2;
vertical <- (par("usr")[3] + par("usr")[4]) / 2;
text(horizontal, vertical, format(abs(cor(x,y)), digits=2))
}

pairs(iris[1:4], main = "Plot of Iris - Pearson", pch = 21, bg = c("Mediumvioletred", "lightpink", "plum")[unclass(iris$Species)], upper.panel=panel.pearson)
#Thus, The correlation coefficient of Sepal length and Sepal Width is -0.12, which indicate that Sepal length and Sepal Width has negative correlate relationship.

#The correlation coefficient between Petal Length and Petal Width is 0.96, thus, Petal Length and Petal Width have stronger correlation relationship.
```

# Part III - Analyzing Swirl
Expand All @@ -171,6 +284,8 @@ In this repository you will find data describing Swirl activity from the class s
### Instructions

1. Insert a new code block
```{r}
```
2. Create a data frame from the `swirl-data.csv` file called `DF1`

The variables are:
Expand All @@ -183,21 +298,46 @@ The variables are:
`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)
DF1
```
3. Create a new data frame that only includes the variables `hash`, `lesson_name` and `attempt` called `DF2`

```{r}
DF2 <- DF1 %>% select(hash,lesson_name,attempt)
DF2
```
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) %>% summarize(total=sum(attempt)) %>%print
```
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}
DF3 <- DF2 %>% group_by(hash, lesson_name) %>% summarise(attempt = sum(attempt))
DF3S <- spread(DF3, lesson_name, attempt)
DF3S
```
7. Create a new data frame from `DF1` called `DF4` that only includes the variables `hash`, `lesson_name` and `correct`

```{r}
DF4 <- DF1 %>% select(hash, lesson_name, correct)
DF4
```
8. Convert the `correct` variable so that `TRUE` is coded as the **number** `1` and `FALSE` is coded as `0`
```{r}
DF4$correct <- ifelse(DF4$correct == TRUE, 1, 0)
DF4
```

9. Create a new data frame called `DF5` that provides a mean score for each student on each course
```{r}
DF5 <- DF4 %>% group_by(hash, lesson_name) %>% summarise(mean.correct = mean(correct, na.rm = TRUE))
DF5
```

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 <- DF1 %>% select(datetime, correct) %>% mutate(correct=as.logical(correct))%>% mutate(correct=as.numeric(correct))%>% mutate(datetime= format(as.POSIXct(datetime,origin = "1970-01-01"), "%B %d %Y")) %>% group_by(datetime) %>% summarize(mean_correct=mean(correct, na.rm=TRUE)) %>% print
```

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.
Loading