diff --git a/Assignment7.Rmd b/Assignment7.Rmd index 105cbdf..7f0465e 100644 --- a/Assignment7.Rmd +++ b/Assignment7.Rmd @@ -11,27 +11,42 @@ In the following assignment you will be looking at data from an one level of an #Upload data ```{r} +library(ggplot2) +library(dplyr) +library(tidyr) +library(rpart) +library(party) +D1 <- read.csv("online.data.csv") ``` #Visualization ```{r} -#Start by creating histograms of the distributions for all variables (#HINT: look up "facet" in the ggplot documentation) +#Start by creating histograms of the distributions for all variables (#HINT: look up "facet" in the ggplot documentation) +D2 <- gather(D1, "variable", "value", 2:7) +ggplot(D2, mapping = aes(x = id, y = value)) + + geom_histogram(stat="identity", binwidth = 50) + + facet_wrap(~variable, scales = "free_y") #Then visualize the relationships between variables - +pairs(D1) #Try to capture an intution about the data and the relationships - +library(corrplot) +D3 <- D1 %>% + mutate(level.up = ifelse(level.up == "no",0,1)) +COR <- cor(D3) +corrplot(COR, order="AOE", method="color", tl.pos="lt", type="upper") ``` #Classification tree ```{r} #Create a classification tree that predicts whether a student "levels up" in the online course using three variables of your choice (As we did last time, set all controls to their minimums) - +c.tree1 <- rpart(as.factor(level.up) ~ post.test.score + messages + av.assignment.score, method = "class", data = D1) #Plot and generate a CP table for your tree - +printcp(c.tree1) +post(c.tree1, file = "tree1.ps") #Generate a probability value that represents the probability that a student levels up based your classification tree - -D1$pred <- predict(rp, type = "prob")[,2]#Last class we used type = "class" which predicted the classification for us, this time we are using type = "prob" to see the probability that our classififcation is based on. +D3$pred <- predict(c.tree1, type = "prob")[,2] +#Last class we used type = "class" which predicted the classification for us, this time we are using type = "prob" to see the probability that our classififcation is based on. ``` ## Part II #Now you can generate the ROC curve for your model. You will need to install the package ROCR to do this. @@ -39,43 +54,63 @@ D1$pred <- predict(rp, type = "prob")[,2]#Last class we used type = "class" whic library(ROCR) #Plot the curve -pred.detail <- prediction(D1$pred, D1$level.up) +pred.detail <- prediction(D3$pred, D3$level.up) plot(performance(pred.detail, "tpr", "fpr")) abline(0, 1, lty = 2) - #Calculate the Area Under the Curve -unlist(slot(performance(Pred2,"auc"), "y.values"))#Unlist liberates the AUC value from the "performance" object created by ROCR +#Unlist liberates the AUC value from the "performance" object created by ROCR #Now repeat this process, but using the variables you did not use for the previous model and compare the plots & results of your two models. Which one do you think was the better model? Why? +c.tree2 <- rpart(as.factor(level.up) ~ forum.posts + id + pre.test.score, method = "class", data = D1) +printcp(c.tree2) +post(c.tree2, file = "tree2.ps", title = "CP Table2") +D3$pred2 <- predict(c.tree2, type = "prob")[,2] +pred.detail2 <- prediction(D3$pred2, D3$level.up) +plot(performance(pred.detail2, "tpr", "fpr")) +abline(0, 1, lty = 2) +unlist(slot(performance(pred.detail2, "auc"), "y.values")) ``` +##According to the accuracy, the first model has a higher accuracy level than the second model (the area under the curve equals to 1). ## Part III #Thresholds ```{r} #Look at the ROC plot for your first model. Based on this plot choose a probability threshold that balances capturing the most correct predictions against false positives. Then generate a new variable in your data set that classifies each student according to your chosen threshold. -threshold.pred1 <- +cut <- 0.5 +D3$threshold.pred1 <- D3$pred +D3$threshold.pred1[D3$pred < cut] <- 0 +D3$threshold.pred1[D3$pred >= cut] <- 1 #Now generate three diagnostics: -D1$accuracy.model1 <- - -D1$precision.model1 <- - -D1$recall.model1 <- - -#Finally, calculate Kappa for your model according to: - +D3$accuracy.model1 <- mean(ifelse(D3$level.up == D3$threshold.pred1, 1, 0)) +D3$accuracy.model1 <- as.integer(D3$accuracy.model1) +accuracy1 <- sum(D3$accuracy.model1) / length(D3$accuracy.model1) +D3$precision.model1 <- ifelse(D3$level.up == 1 & D3$threshold.pred1 == 1, 1, 0) +precision1 <- sum(D3$precision.model1) / sum (D3$threshold.pred1) +D3$recall.model1 <- ifelse(D3$level.up == 1 & D3$threshold.pred1 == 1, 1, 0) +recall1 <- sum(D3$precision.model1) / sum(D3$level.up) #First generate the table of comparisons -table1 <- table(D1$level.up, D1$threshold.pred1) +table1 <- table(D3$level.up, D3$threshold.pred1) #Convert to matrix matrix1 <- as.matrix(table1) - +matrix1 #Calculate kappa kappa(matrix1, exact = TRUE)/kappa(matrix1) #Now choose a different threshold value and repeat these diagnostics. What conclusions can you draw about your two thresholds? - +cut2 = 1 +D3$threshold.pred2 <- D3$pred +D3$threshold.pred2[D3$pred < cut2] <- 0 +D3$threshold.pred2[D3$pred == cut2] <- 1 +D3$accuracy.model2 <- mean(ifelse(D3$level.up == D3$threshold.pred2, 1, 0)) +D3$accuracy.model2 <- as.integer(D3$accuracy.model2) +accuracy2 <- sum(D3$accuracy.model2) / length(D3$accuracy.model2) +D3$precision.model2 <- ifelse(D3$level.up == 1 & D3$threshold.pred2 == 1, 1, 0) +precision2 <- sum(D3$precision.model2) / sum (D3$threshold.pred2) +D3$recall.model2 <- ifelse(D3$level.up == 1 & D3$threshold.pred2 == 1, 1, 0) +recall2 <- sum(D3$precision.model2) / sum(D3$level.up) ``` ### To Submit Your Assignment diff --git a/Assignment7.html b/Assignment7.html new file mode 100644 index 0000000..4c5904d --- /dev/null +++ b/Assignment7.html @@ -0,0 +1,599 @@ + + + + + + + + + + + + + + + + +Assignment 7 - Answers + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + +

In the following assignment you will be looking at data from an one level of an online geography tutoring system used by 5th grade students. The game involves a pre-test of geography knowledge (pre.test), a series of assignments for which you have the average score (av.assignment.score), the number of messages sent by each student to other students about the assignments (messages), the number of forum posts students posted asking questions about the assignment (forum.posts), a post test at the end of the level (post.test) and whether or not the system allowed the students to go on to the next level (level.up).

+
+

Part I

+

#Upload data

+
library(ggplot2)
+
## Warning: package 'ggplot2' was built under R version 3.5.3
+
library(dplyr)
+
## Warning: package 'dplyr' was built under R version 3.5.3
+
## 
+## Attaching package: 'dplyr'
+
## The following objects are masked from 'package:stats':
+## 
+##     filter, lag
+
## The following objects are masked from 'package:base':
+## 
+##     intersect, setdiff, setequal, union
+
library(tidyr)
+
## Warning: package 'tidyr' was built under R version 3.5.3
+
library(rpart)
+
## Warning: package 'rpart' was built under R version 3.5.3
+
library(party)
+
## Warning: package 'party' was built under R version 3.5.3
+
## Loading required package: grid
+
## Loading required package: mvtnorm
+
## Warning: package 'mvtnorm' was built under R version 3.5.3
+
## Loading required package: modeltools
+
## Loading required package: stats4
+
## Loading required package: strucchange
+
## Warning: package 'strucchange' was built under R version 3.5.3
+
## Loading required package: zoo
+
## Warning: package 'zoo' was built under R version 3.5.3
+
## 
+## Attaching package: 'zoo'
+
## The following objects are masked from 'package:base':
+## 
+##     as.Date, as.Date.numeric
+
## Loading required package: sandwich
+
## Warning: package 'sandwich' was built under R version 3.5.3
+
D1 <- read.csv("online.data.csv")
+

#Visualization

+
#Start by creating histograms of the distributions for all variables (#HINT: look up "facet" in the ggplot documentation)
+D2 <- gather(D1, "variable", "value", 2:7)
+
## Warning: attributes are not identical across measure variables;
+## they will be dropped
+
ggplot(D2, mapping = aes(x = id, y = value)) +
+  geom_histogram(stat="identity", binwidth = 50) +
+  facet_wrap(~variable, scales = "free_y")
+
## Warning: Ignoring unknown parameters: binwidth, bins, pad
+

+
#Then visualize the relationships between variables
+pairs(D1)
+

+
#Try to capture an intution about the data and the relationships
+library(corrplot)
+
## Warning: package 'corrplot' was built under R version 3.5.3
+
## corrplot 0.84 loaded
+
D3 <- D1 %>%
+      mutate(level.up = ifelse(level.up == "no",0,1))
+COR <- cor(D3)
+corrplot(COR, order="AOE", method="color", tl.pos="lt", type="upper")
+

#Classification tree

+
#Create a classification tree that predicts whether a student "levels up" in the online course using three variables of your choice (As we did last time, set all controls to their minimums)
+c.tree1 <- rpart(as.factor(level.up) ~ post.test.score + messages + av.assignment.score, method = "class", data = D1)
+#Plot and generate a CP table for your tree 
+printcp(c.tree1)
+
## 
+## Classification tree:
+## rpart(formula = as.factor(level.up) ~ post.test.score + messages + 
+##     av.assignment.score, data = D1, method = "class")
+## 
+## Variables actually used in tree construction:
+## [1] av.assignment.score post.test.score    
+## 
+## Root node error: 400/1000 = 0.4
+## 
+## n= 1000 
+## 
+##     CP nsplit rel error xerror     xstd
+## 1 0.93      0      1.00   1.00 0.038730
+## 2 0.07      1      0.07   0.07 0.013042
+## 3 0.01      2      0.00   0.00 0.000000
+
post(c.tree1, file = "tree1.ps")
+#Generate a probability value that represents the probability that a student levels up based your classification tree 
+D3$pred <- predict(c.tree1, type = "prob")[,2]
+#Last class we used type = "class" which predicted the classification for us, this time we are using type = "prob" to see the probability that our classififcation is based on.
+
+
+

Part II

+

#Now you can generate the ROC curve for your model. You will need to install the package ROCR to do this.

+
library(ROCR)
+
## Warning: package 'ROCR' was built under R version 3.5.3
+
## Loading required package: gplots
+
## Warning: package 'gplots' was built under R version 3.5.3
+
## 
+## Attaching package: 'gplots'
+
## The following object is masked from 'package:stats':
+## 
+##     lowess
+
#Plot the curve
+pred.detail <- prediction(D3$pred, D3$level.up) 
+plot(performance(pred.detail, "tpr", "fpr"))
+abline(0, 1, lty = 2)
+

+
#Calculate the Area Under the Curve
+#Unlist liberates the AUC value from the "performance" object created by ROCR
+
+#Now repeat this process, but using the variables you did not use for the previous model and compare the plots & results of your two models. Which one do you think was the better model? Why?
+c.tree2 <- rpart(as.factor(level.up) ~ forum.posts + id + pre.test.score, method = "class", data = D1)
+printcp(c.tree2)
+
## 
+## Classification tree:
+## rpart(formula = as.factor(level.up) ~ forum.posts + id + pre.test.score, 
+##     data = D1, method = "class")
+## 
+## Variables actually used in tree construction:
+## [1] id
+## 
+## Root node error: 400/1000 = 0.4
+## 
+## n= 1000 
+## 
+##       CP nsplit rel error xerror     xstd
+## 1 0.5325      0    1.0000  1.000 0.038730
+## 2 0.0100      1    0.4675  0.495 0.031504
+
post(c.tree2, file = "tree2.ps", title = "CP Table2")
+D3$pred2 <- predict(c.tree2, type = "prob")[,2]
+pred.detail2 <- prediction(D3$pred2, D3$level.up) 
+plot(performance(pred.detail2, "tpr", "fpr"))
+abline(0, 1, lty = 2)
+

+
unlist(slot(performance(pred.detail2, "auc"), "y.values"))
+
## [1] 0.8220833
+

##According to the accuracy, the first model has a higher accuracy level than the second model (the area under the curve equals to 1). ## Part III #Thresholds

+
#Look at the ROC plot for your first model. Based on this plot choose a probability threshold that balances capturing the most correct predictions against false positives. Then generate a new variable in your data set that classifies each student according to your chosen threshold.
+
+cut <- 0.5
+D3$threshold.pred1 <- D3$pred
+D3$threshold.pred1[D3$pred < cut] <- 0
+D3$threshold.pred1[D3$pred >= cut] <- 1
+
+#Now generate three diagnostics:
+
+D3$accuracy.model1 <- mean(ifelse(D3$level.up == D3$threshold.pred1, 1, 0))
+D3$accuracy.model1 <- as.integer(D3$accuracy.model1)
+accuracy1 <- sum(D3$accuracy.model1) / length(D3$accuracy.model1)
+D3$precision.model1 <- ifelse(D3$level.up == 1 & D3$threshold.pred1 == 1, 1, 0)
+precision1 <- sum(D3$precision.model1) / sum (D3$threshold.pred1)
+D3$recall.model1 <- ifelse(D3$level.up == 1 & D3$threshold.pred1 == 1, 1, 0)
+recall1 <- sum(D3$precision.model1) / sum(D3$level.up)
+#First generate the table of comparisons
+table1 <- table(D3$level.up, D3$threshold.pred1)
+
+#Convert to matrix
+matrix1 <- as.matrix(table1)
+matrix1
+
##    
+##       0   1
+##   0 600   0
+##   1   0 400
+
#Calculate kappa
+kappa(matrix1, exact = TRUE)/kappa(matrix1)
+
## [1] 1.153846
+
#Now choose a different threshold value and repeat these diagnostics. What conclusions can you draw about your two thresholds?
+cut2 = 1
+D3$threshold.pred2 <- D3$pred
+D3$threshold.pred2[D3$pred < cut2] <- 0
+D3$threshold.pred2[D3$pred == cut2] <- 1
+D3$accuracy.model2 <- mean(ifelse(D3$level.up == D3$threshold.pred2, 1, 0))
+D3$accuracy.model2 <- as.integer(D3$accuracy.model2)
+accuracy2 <- sum(D3$accuracy.model2) / length(D3$accuracy.model2)
+D3$precision.model2 <- ifelse(D3$level.up == 1 & D3$threshold.pred2 == 1, 1, 0)
+precision2 <- sum(D3$precision.model2) / sum (D3$threshold.pred2)
+D3$recall.model2 <- ifelse(D3$level.up == 1 & D3$threshold.pred2 == 1, 1, 0)
+recall2 <- sum(D3$precision.model2) / sum(D3$level.up)
+
+

To Submit Your Assignment

+

Please submit your assignment by first “knitting” your RMarkdown document into an html file and then commit, push and pull request both the RMarkdown file and the html file.

+
+
+ + + + +
+ + + + + + + + + + + + + + +