diff --git a/Assignment7.Rmd b/Assignment7.Rmd index 105cbdf..0ccaace 100644 --- a/Assignment7.Rmd +++ b/Assignment7.Rmd @@ -1,7 +1,7 @@ --- title: "Assignment 7 - Answers" -author: "Charles Lang" -date: "11/30/2016" +author: "Zifan Cao" +date: "12/03/2019" output: html_document --- @@ -11,7 +11,7 @@ In the following assignment you will be looking at data from an one level of an #Upload data ```{r} - +A1 <- read.csv("online.data.csv") ``` #Visualization @@ -21,61 +21,104 @@ In the following assignment you will be looking at data from an one level of an #Then visualize the relationships between variables #Try to capture an intution about the data and the relationships - +library(ggplot2) +library(tidyr) +library(dplyr) +A1$level.up <- ifelse(A1$level.up == "yes", 1,0) +A2 <- gather(A1, "level", "score", 2:7) +plot1 <- ggplot(A2, aes(score)) + facet_wrap(~level, scales = "free") +plot1 + geom_histogram(stat = "count") +pairs(A1) ``` #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) - +library(rpart.plot) +c.treea <- rpart(level.up ~ forum.posts + pre.test.score, method = "class", data = A1, control=rpart.control(minsplit=1, minbucket=1, cp=0.001)) #Plot and generate a CP table for your tree +printcp(c.treea) +plot(c.treea) #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. +A1$pred <- predict(c.treea, A1, 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. ```{r} + library(ROCR) #Plot the curve -pred.detail <- prediction(D1$pred, D1$level.up) +pred.detail <- prediction(A1$pred, A1$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(slot(performance(pred.detail,"auc"), "y.values")) +#as this question, you need to run both plot and bline together #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(level.up~pre.test.score+post.test.score+forum.posts, method="class",data=A1) +printcp(c.tree2) +post(c.tree2, file = "dtree2.ps", title = "tree2") +rpart.plot(c.tree2, type=3, box.palette = c("red", "green"), fallen.leaves = TRUE) +A1$pred2 <- predict(c.tree2, A1, type="prob")[,2] +pred.detail2 <- prediction(A1$pred2, A1$level.up) +plot(performance(pred.detail2, "tpr", "fpr")) +abline(0, 1, lty = 2) +unlist(slot(performance(pred.detail2, "auc"), "y.values")) +# from the ROC curve, we think the first model is better because the auc value is higher. ``` ## 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 <- - -#Now generate three diagnostics: +A1$threshold.pred1 <- ifelse(A1$pred >= 0.8, "yes", "no") +A1$threshold.pred2 <- ifelse(A1$pred >= 0.95, "yes", "no") +A1$threshold.pred3 <- ifelse(A1$pred >= 0.25, "yes", "no") -D1$accuracy.model1 <- -D1$precision.model1 <- +#Now generate three diagnostics: +accuracy.model1 <- mean(ifelse(A1$level.up == A1$threshold.pred1, 1, 0)) +A1$truepos.model1 <- ifelse(A1$level.up == "yes" & A1$threshold.pred1 == "yes", 1, 0) +A1$falsepos.model1 <- ifelse(A1$level.up == "no" & A1$threshold.pred1 == "yes", 1,0) +A1$falseneg.model1 <- ifelse(A1$level.up == "yes" & A1$threshold.pred1 == "no", 1,0) +precision.model1 <- sum(A1$truepos.model1)/(sum(A1$truepos.model1) + sum(A1$falsepos.model1)) +recall.model1 <- sum(A1$truepos.model1)/(sum(A1$truepos.model1) + sum(A1$falseneg.model1)) -D1$recall.model1 <- #Finally, calculate Kappa for your model according to: #First generate the table of comparisons -table1 <- table(D1$level.up, D1$threshold.pred1) +table1 <- table(A1$level.up, A1$threshold.pred1) +table1 #Convert to matrix matrix1 <- as.matrix(table1) #Calculate kappa kappa(matrix1, exact = TRUE)/kappa(matrix1) - +#1.087797 #Now choose a different threshold value and repeat these diagnostics. What conclusions can you draw about your two thresholds? +accuracy.model3 <- mean(ifelse(A1$level.up == A1$threshold.pred3, 1, 0)) +A1$truepos.model3 <- ifelse(A1$level.up == "yes" & A1$threshold.pred3 == "yes", 1, 0) +A1$falsepos.model3 <- ifelse(A1$level.up == "no" & A1$threshold.pred3 == "yes", 1,0) +A1$falseneg.model3 <- ifelse(A1$level.up == "yes" & A1$threshold.pred3 == "no", 1,0) +precision.model3 <- sum(A1$truepos.model3)/(sum(A1$truepos.model3) + sum(A1$falsepos.model3)) +recall.model3 <- sum(A1$truepos.model3)/(sum(A1$truepos.model3) + sum(A1$falseneg.model3)) + +#First generate the table of comparisons +table3 <- table(A1$level.up, A1$threshold.pred3) +table3 + +#Convert to matrix +matrix3 <- as.matrix(table3) + +#Calculate kappa +kappa(matrix3, exact = TRUE)/kappa(matrix3) +#1.030205 +#with higher threshold, the kappa value is higher and we can conclude that with higher threshold, our prediction is better ``` ### To Submit Your Assignment diff --git a/Assignment7.html b/Assignment7.html new file mode 100644 index 0000000..a9aea2b --- /dev/null +++ b/Assignment7.html @@ -0,0 +1,603 @@ + + + + + + + + + + + + + + + + +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

+
A1 <- read.csv("online.data.csv")
+

#Visualization

+
#Start by creating histograms of the distributions for all variables (#HINT: look up "facet" in the ggplot documentation)
+
+#Then visualize the relationships between variables
+
+#Try to capture an intution about the data and the relationships
+library(ggplot2)
+library(tidyr)
+library(dplyr)
+
## 
+## 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
+
A1$level.up <- ifelse(A1$level.up == "yes", 1,0)
+A2 <- gather(A1, "level", "score", 2:7)
+plot1 <- ggplot(A2, aes(score)) + facet_wrap(~level, scales = "free")
+plot1 + geom_histogram(stat = "count")
+
## Warning: Ignoring unknown parameters: binwidth, bins, pad
+

+
pairs(A1)
+

#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)
+library(rpart.plot)
+
## Loading required package: rpart
+
c.treea <- rpart(level.up ~ forum.posts + pre.test.score, method = "class", data = A1, control=rpart.control(minsplit=1, minbucket=1, cp=0.001))
+#Plot and generate a CP table for your tree 
+printcp(c.treea)
+
## 
+## Classification tree:
+## rpart(formula = level.up ~ forum.posts + pre.test.score, data = A1, 
+##     method = "class", control = rpart.control(minsplit = 1, minbucket = 1, 
+##         cp = 0.001))
+## 
+## Variables actually used in tree construction:
+## [1] forum.posts    pre.test.score
+## 
+## Root node error: 400/1000 = 0.4
+## 
+## n= 1000 
+## 
+##           CP nsplit rel error xerror     xstd
+## 1  0.3925000      0    1.0000 1.0000 0.038730
+## 2  0.0300000      1    0.6075 0.6075 0.033907
+## 3  0.0200000      2    0.5775 0.6250 0.034233
+## 4  0.0150000      3    0.5575 0.6125 0.034001
+## 5  0.0050000      4    0.5425 0.5825 0.033421
+## 6  0.0037500      8    0.5225 0.6350 0.034413
+## 7  0.0031250     14    0.4975 0.6375 0.034458
+## 8  0.0025000     25    0.4575 0.6425 0.034546
+## 9  0.0017857     47    0.4025 0.6425 0.034546
+## 10 0.0016667     61    0.3700 0.6425 0.034546
+## 11 0.0015000     67    0.3600 0.6450 0.034590
+## 12 0.0012500     75    0.3475 0.6650 0.034932
+## 13 0.0010714    121    0.2900 0.6775 0.035139
+## 14 0.0010000    128    0.2825 0.6900 0.035340
+
plot(c.treea)
+

+
#Generate a probability value that represents the probability that a student levels up based your classification tree 
+A1$pred <- predict(c.treea, A1, 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)
+
## Loading required package: gplots
+
## 
+## Attaching package: 'gplots'
+
## The following object is masked from 'package:stats':
+## 
+##     lowess
+
#Plot the curve
+pred.detail <- prediction(A1$pred, A1$level.up) 
+plot(performance(pred.detail, "tpr", "fpr"))
+abline(0, 1, lty = 2)
+

+
#Calculate the Area Under the Curve
+unlist(slot(performance(pred.detail,"auc"), "y.values"))
+
## [1] 0.9339042
+
#as this question, you need to run both plot and bline together
+
+#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(level.up~pre.test.score+post.test.score+forum.posts, method="class",data=A1)
+printcp(c.tree2)
+
## 
+## Classification tree:
+## rpart(formula = level.up ~ pre.test.score + post.test.score + 
+##     forum.posts, data = A1, method = "class")
+## 
+## Variables actually used in tree construction:
+## [1] post.test.score
+## 
+## Root node error: 400/1000 = 0.4
+## 
+## n= 1000 
+## 
+##       CP nsplit rel error xerror     xstd
+## 1 0.6025      0    1.0000  1.000 0.038730
+## 2 0.0100      1    0.3975  0.465 0.030762
+
post(c.tree2, file = "dtree2.ps", title = "tree2")
+rpart.plot(c.tree2, type=3, box.palette = c("red", "green"), fallen.leaves = TRUE)
+

+
A1$pred2 <- predict(c.tree2, A1, type="prob")[,2]
+pred.detail2 <- prediction(A1$pred2, A1$level.up) 
+plot(performance(pred.detail2, "tpr", "fpr"))
+abline(0, 1, lty = 2)
+

+
unlist(slot(performance(pred.detail2, "auc"), "y.values"))
+
## [1] 0.8545833
+
# from the ROC curve, we think the first model is better because the auc value is higher.  
+
+
+

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.
+
+A1$threshold.pred1 <- ifelse(A1$pred >= 0.8, "yes", "no")
+A1$threshold.pred2 <- ifelse(A1$pred >= 0.95, "yes", "no")
+A1$threshold.pred3 <- ifelse(A1$pred >= 0.25, "yes", "no")
+
+
+#Now generate three diagnostics:
+accuracy.model1 <- mean(ifelse(A1$level.up == A1$threshold.pred1, 1, 0))
+A1$truepos.model1 <- ifelse(A1$level.up == "yes" & A1$threshold.pred1 == "yes", 1, 0)
+A1$falsepos.model1 <- ifelse(A1$level.up == "no" & A1$threshold.pred1 == "yes", 1,0)
+A1$falseneg.model1 <- ifelse(A1$level.up == "yes" & A1$threshold.pred1 == "no", 1,0)
+precision.model1 <- sum(A1$truepos.model1)/(sum(A1$truepos.model1) + sum(A1$falsepos.model1))
+recall.model1 <- sum(A1$truepos.model1)/(sum(A1$truepos.model1) + sum(A1$falseneg.model1))
+
+
+#Finally, calculate Kappa for your model according to:
+
+#First generate the table of comparisons
+table1 <- table(A1$level.up, A1$threshold.pred1)
+table1 
+
##    
+##      no yes
+##   0 586  14
+##   1 153 247
+
#Convert to matrix
+matrix1 <- as.matrix(table1)
+
+#Calculate kappa
+kappa(matrix1, exact = TRUE)/kappa(matrix1)
+
## [1] 1.087797
+
#1.087797
+#Now choose a different threshold value and repeat these diagnostics. What conclusions can you draw about your two thresholds?
+
+accuracy.model3 <- mean(ifelse(A1$level.up == A1$threshold.pred3, 1, 0))
+A1$truepos.model3 <- ifelse(A1$level.up == "yes" & A1$threshold.pred3 == "yes", 1, 0)
+A1$falsepos.model3 <- ifelse(A1$level.up == "no" & A1$threshold.pred3 == "yes", 1,0)
+A1$falseneg.model3 <- ifelse(A1$level.up == "yes" & A1$threshold.pred3 == "no", 1,0)
+precision.model3 <- sum(A1$truepos.model3)/(sum(A1$truepos.model3) + sum(A1$falsepos.model3))
+recall.model3 <- sum(A1$truepos.model3)/(sum(A1$truepos.model3) + sum(A1$falseneg.model3))
+
+#First generate the table of comparisons
+table3 <- table(A1$level.up, A1$threshold.pred3)
+table3 
+
##    
+##      no yes
+##   0 482 118
+##   1  35 365
+
#Convert to matrix
+matrix3 <- as.matrix(table3)
+
+#Calculate kappa
+kappa(matrix3, exact = TRUE)/kappa(matrix3)
+
## [1] 1.030205
+
#1.030205
+#with higher threshold, the kappa value is higher and we can conclude that with higher threshold, our prediction is better 
+
+

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.

+
+
+ + + + +
+ + + + + + + + + + + + + + + diff --git a/Assignment7_files/figure-html/unnamed-chunk-2-1.png b/Assignment7_files/figure-html/unnamed-chunk-2-1.png new file mode 100644 index 0000000..cbc59e1 Binary files /dev/null and b/Assignment7_files/figure-html/unnamed-chunk-2-1.png differ diff --git a/Assignment7_files/figure-html/unnamed-chunk-2-2.png b/Assignment7_files/figure-html/unnamed-chunk-2-2.png new file mode 100644 index 0000000..09b6f4f Binary files /dev/null and b/Assignment7_files/figure-html/unnamed-chunk-2-2.png differ diff --git a/Assignment7_files/figure-html/unnamed-chunk-3-1.png b/Assignment7_files/figure-html/unnamed-chunk-3-1.png new file mode 100644 index 0000000..0a52734 Binary files /dev/null and b/Assignment7_files/figure-html/unnamed-chunk-3-1.png differ diff --git a/assignment7.Rproj b/assignment7.Rproj new file mode 100644 index 0000000..8e3c2eb --- /dev/null +++ b/assignment7.Rproj @@ -0,0 +1,13 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX diff --git a/treea.ps b/treea.ps new file mode 100644 index 0000000..00d8c62 Binary files /dev/null and b/treea.ps differ