From 6de075efe26594dc8273f7206ba5ea1c39ad1b15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=90=AC=E8=8B=A5?= Date: Wed, 4 Dec 2019 15:05:19 -0500 Subject: [PATCH 1/2] Assignment7 --- .gitignore | 4 ++++ Assignment7.Rmd | 6 ++---- assignment7.Rproj | 13 +++++++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 assignment7.Rproj diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b6a065 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.Rproj.user +.Rhistory +.RData +.Ruserdata diff --git a/Assignment7.Rmd b/Assignment7.Rmd index 105cbdf..846bab1 100644 --- a/Assignment7.Rmd +++ b/Assignment7.Rmd @@ -1,8 +1,6 @@ --- -title: "Assignment 7 - Answers" -author: "Charles Lang" -date: "11/30/2016" -output: html_document +"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). 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 From 178f4da72437242a087e3d255e8379cde4ee99c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=90=AC=E8=8B=A5?= Date: Wed, 18 Dec 2019 22:21:24 -0500 Subject: [PATCH 2/2] final --- Assignment7.Rmd | 48 ++-- Assignment7.html | 567 +++++++++++++++++++++++++++++++++++++++++++++++ dtree2.ps | Bin 0 -> 4656 bytes 3 files changed, 602 insertions(+), 13 deletions(-) create mode 100644 Assignment7.html create mode 100644 dtree2.ps diff --git a/Assignment7.Rmd b/Assignment7.Rmd index 846bab1..b6575d6 100644 --- a/Assignment7.Rmd +++ b/Assignment7.Rmd @@ -9,27 +9,36 @@ In the following assignment you will be looking at data from an one level of an #Upload data ```{r} - +D1 <- read.csv("online.data.csv") +library(ggplot2) +library(tidyr) +library(dplyr) +library(rpart) +library(rpart.plot) ``` #Visualization ```{r} #Start by creating histograms of the distributions for all variables (#HINT: look up "facet" in the ggplot documentation) - +D1$level.up <- ifelse(D1$level.up == "yes", 1,0) #Then visualize the relationships between variables - +D2 <- gather(D1, "level", "score", 2:7) #Try to capture an intution about the data and the relationships - +plot1 <- ggplot(D2, aes(score)) + facet_wrap(~level, scales = "free") +plot1 + geom_histogram(stat = "count") +pairs(D1) ``` #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(level.up ~ forum.posts + pre.test.score, method="class", data=D1, control=rpart.control(minsplit=1, minbucket=1, cp=0.001)) +printcp(c.tree1) #Plot and generate a CP table for your tree - +plot(c.tree1) #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. + +D1$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. @@ -42,24 +51,37 @@ plot(performance(pred.detail, "tpr", "fpr")) abline(0, 1, lty = 2) #Calculate the Area Under the Curve +Pred2 <- pred.detail unlist(slot(performance(Pred2,"auc"), "y.values"))#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(level.up~pre.test.score+post.test.score+forum.posts, method="class",data=D1) +printcp(c.tree2) +post(c.tree2, file = "dtree2.ps", title = "tree2") +rpart.plot(c.tree2, type=3, box.palette = c("green", "red"), fallen.leaves = TRUE) +D1$pred2 <- predict(c.tree2, D1, type="prob")[,2] +pred.detail2 <- prediction(D1$pred2, D1$level.up) +plot(performance(pred.detail2, "tpr", "fpr")) +abline(0, 1, lty = 2) +unlist(slot(performance(pred.detail2, "auc"), "y.values")) ``` ## 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 <- +D1$threshold.pred1 <- ifelse(D1$pred >= 0.8, "yes", "no") +D1$threshold.pred2 <- ifelse(D1$pred >= 0.95, "yes", "no") +D1$threshold.pred3 <- ifelse(D1$pred >= 0.25, "yes", "no") -#Now generate three diagnostics: - -D1$accuracy.model1 <- -D1$precision.model1 <- +#Now generate three diagnostics: -D1$recall.model1 <- +accuracy.model <- mean(ifelse(D1$level.up == D1$threshold.pred3, 1, 0)) +D1$accuracy.model1 <- ifelse(D1$level.up == "yes" & D1$threshold.pred3 == "yes", 1, 0) +D1$precision.model1 <- ifelse(D1$level.up == "yes" & D1$threshold.pred3 == "yes", 1, 0) +D1$recall.model1 <- ifelse(D1$level.up == "yes" & D1$threshold.pred3 == "no", 1,0) #Finally, calculate Kappa for your model according to: diff --git a/Assignment7.html b/Assignment7.html new file mode 100644 index 0000000..c5d8364 --- /dev/null +++ b/Assignment7.html @@ -0,0 +1,567 @@ + + + + + + + + + + + + + + +Assignment7.utf8.md + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + +

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

+
D1 <- read.csv("online.data.csv")
+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
+
library(rpart)
+library(rpart.plot)
+

#Visualization

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

+
pairs(D1)
+

#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(level.up ~ forum.posts + pre.test.score, method="class", data=D1, control=rpart.control(minsplit=1, minbucket=1, cp=0.001))
+printcp(c.tree1)
+
## 
+## Classification tree:
+## rpart(formula = level.up ~ forum.posts + pre.test.score, data = D1, 
+##     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.6325 0.034368
+## 4  0.0150000      3    0.5575 0.6050 0.033860
+## 5  0.0050000      4    0.5425 0.6025 0.033812
+## 6  0.0037500      8    0.5225 0.6250 0.034233
+## 7  0.0031250     14    0.4975 0.6325 0.034368
+## 8  0.0025000     25    0.4575 0.6350 0.034413
+## 9  0.0017857     47    0.4025 0.6050 0.033860
+## 10 0.0016667     61    0.3700 0.6175 0.034095
+## 11 0.0015000     67    0.3600 0.6325 0.034368
+## 12 0.0012500     75    0.3475 0.6900 0.035340
+## 13 0.0010714    121    0.2900 0.6850 0.035260
+## 14 0.0010000    128    0.2825 0.6975 0.035458
+
#Plot and generate a CP table for your tree 
+plot(c.tree1)
+

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

+
#Calculate the Area Under the Curve
+Pred2 <- pred.detail
+unlist(slot(performance(Pred2,"auc"), "y.values"))#Unlist liberates the AUC value from the "performance" object created by ROCR
+
## [1] 0.9339042
+
#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=D1)
+printcp(c.tree2)
+
## 
+## Classification tree:
+## rpart(formula = level.up ~ pre.test.score + post.test.score + 
+##     forum.posts, data = D1, 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.00 0.038730
+## 2 0.0100      1    0.3975   0.44 0.030106
+
post(c.tree2, file = "dtree2.ps", title = "tree2")
+rpart.plot(c.tree2, type=3, box.palette = c("green", "red"), fallen.leaves = TRUE)
+

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

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

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.
+
+D1$threshold.pred1 <- ifelse(D1$pred >= 0.8, "yes", "no")
+D1$threshold.pred2 <- ifelse(D1$pred >= 0.95, "yes", "no")
+D1$threshold.pred3 <- ifelse(D1$pred >= 0.25, "yes", "no")
+
+
+#Now generate three diagnostics:
+
+accuracy.model <- mean(ifelse(D1$level.up == D1$threshold.pred3, 1, 0))
+D1$accuracy.model1 <- ifelse(D1$level.up == "yes" & D1$threshold.pred3 == "yes", 1, 0)
+D1$precision.model1 <- ifelse(D1$level.up == "yes" & D1$threshold.pred3 == "yes", 1, 0)
+D1$recall.model1 <- ifelse(D1$level.up == "yes" & D1$threshold.pred3 == "no", 1,0)
+
+#Finally, calculate Kappa for your model according to:
+
+#First generate the table of comparisons
+table1 <- table(D1$level.up, D1$threshold.pred1)
+
+#Convert to matrix
+matrix1 <- as.matrix(table1)
+
+#Calculate kappa
+kappa(matrix1, exact = TRUE)/kappa(matrix1)
+
## [1] 1.087797
+
#Now choose a different threshold value and repeat these diagnostics. What conclusions can you draw about your two thresholds?
+
+

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/dtree2.ps b/dtree2.ps new file mode 100644 index 0000000000000000000000000000000000000000..c0a6c783a364444db6ec95b4ce5adb44d08e418e GIT binary patch literal 4656 zcmd5=QE!|^5Ps)ZOdl*cs=UW8aDbIYiXEr5l*CpXsSi;eI9Q))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