From 2ce4f6456be13096a83f8e011fd026602d2dd5f7 Mon Sep 17 00:00:00 2001 From: shao260 Date: Thu, 19 Dec 2019 17:11:11 -0500 Subject: [PATCH] done --- Assignment7.Rmd | 71 ++++-- Assignment7.html | 585 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 633 insertions(+), 23 deletions(-) create mode 100644 Assignment7.html diff --git a/Assignment7.Rmd b/Assignment7.Rmd index 105cbdf..320887c 100644 --- a/Assignment7.Rmd +++ b/Assignment7.Rmd @@ -1,6 +1,6 @@ --- title: "Assignment 7 - Answers" -author: "Charles Lang" +author: "Shijie Shao" date: "11/30/2016" output: html_document --- @@ -11,26 +11,32 @@ In the following assignment you will be looking at data from an one level of an #Upload data ```{r} - +library(readr) +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) - -#Then visualize the relationships between variables - -#Try to capture an intution about the data and the relationships - +library(ggplot2) +library(dplyr) +library(tidyr) +D1$level.up <- ifelse(D1$level.up == "yes", 1,0) +D2<- gather(D1, "measure", "score", 2:7) +p1<- ggplot(D2, aes(score)) + facet_wrap(~measure, scales = "free") +p1 + geom_histogram(stat = "count") +pairs(D1) ``` #Classification tree ```{r} +library(rpart) #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) #Plot and generate a CP table for your tree #Generate a probability value that represents the probability that a student levels up based your classification tree - +rp <- rpart(level.up ~ post.test.score + forum.posts + pre.test.score + av.assignment.score + messages, method = "class", data = D1,control=rpart.control(minsplit=1, minbucket=1, cp=0.001)) +post(rp, file = "rp", title = "MOOC") +printcp(rp) 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. ``` ## Part II @@ -44,38 +50,57 @@ 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"))#Unlist liberates the AUC value from the "performance" object created by ROCR +pred.detail2 <- prediction(D1$messages, D1$level.up) +plot(performance(pred.detail2, "tpr", "fpr")) +abline(0, 1, lty = 2) +unlist(slot(performance(pred.detail2,"auc"), "y.values")) #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? + +#The second model better. the measurement 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. +D1 <- read.csv("online.data.csv", header = TRUE) -threshold.pred1 <- +c.tree1 <- rpart(level.up ~ pre.test.score + forum.posts, method = "class", data = D1, control=rpart.control(minsplit=1, minbucket=1, cp=0.001)) -#Now generate three diagnostics: +printcp(c.tree1) + +D1$pred1 <- predict(c.tree1, type = "prob")[,2] +threshold.pred1 <- ifelse(D1$pred1 >= 0.5, "yes", "no") -D1$accuracy.model1 <- +#Now generate three diagnostics: -D1$precision.model1 <- +D1$accuracy.model1 <-mean(ifelse(D1$level.up == D1$threshold.pred1, 1, 0)) +D1$truepos.model1 <- ifelse(D1$level.up == "yes" & threshold.pred1 == "yes", 1, 0) -D1$recall.model1 <- +D1$falsepos.model1 <- ifelse(D1$level.up == "no" & threshold.pred1 == "yes", 1, 0) -#Finally, calculate Kappa for your model according to: +D1$falseneg.model1 <- ifelse(D1$level.up == "yes" & threshold.pred1 == "no", 1, 0) -#First generate the table of comparisons -table1 <- table(D1$level.up, D1$threshold.pred1) +D1$precision.model1 <- sum(D1$truepos.model1)/(sum(D1$truepos.model1) + sum(D1$falsepos.model1)) -#Convert to matrix -matrix1 <- as.matrix(table1) +D1$recall.model1 <- sum(D1$truepos.model1)/(sum(D1$truepos.model1) + sum(D1$falseneg.model1)) -#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? +D1$threshold.pred2 <- ifelse(D1$pred1 >= 0.8, "yes", "no") + +D1$accuracy.model2 <- mean(ifelse(D1$level.up == D1$threshold.pred1, 1, 0)) + +D1$truepos.model2 <- ifelse(D1$level.up == "yes" & D1$threshold.pred2 == "yes", 1, 0) + +D1$falsepos.model2 <- ifelse(D1$level.up == "no" & D1$threshold.pred2 == "yes", 1,0) + +D1$falseneg.model2 <- ifelse(D1$level.up == "yes" & D1$threshold.pred2 == "no", 1,0) + +D1$precision.model2 <- sum(D1$truepos.model2)/(sum(D1$truepos.model2) + sum(D1$falsepos.model2)) +D1$recall.model2 <- sum(D1$truepos.model2)/(sum(D1$truepos.model2) + sum(D1$falseneg.model2)) ``` ### To Submit Your Assignment diff --git a/Assignment7.html b/Assignment7.html new file mode 100644 index 0000000..f5c58e2 --- /dev/null +++ b/Assignment7.html @@ -0,0 +1,585 @@ + + + + + + + + + + + + + + + +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(readr)
+D1 <- read_csv("online.data.csv")
+
## Parsed with column specification:
+## cols(
+##   id = col_double(),
+##   post.test.score = col_double(),
+##   pre.test.score = col_double(),
+##   messages = col_double(),
+##   forum.posts = col_double(),
+##   av.assignment.score = col_double(),
+##   level.up = col_character()
+## )
+

#Visualization

+
library(ggplot2)
+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(tidyr)
+D1$level.up <- ifelse(D1$level.up == "yes", 1,0)
+D2<- gather(D1, "measure", "score", 2:7)
+p1<- ggplot(D2, aes(score)) + facet_wrap(~measure, scales = "free")
+p1 + geom_histogram(stat = "count")
+
## Warning: Ignoring unknown parameters: binwidth, bins, pad
+

+
pairs(D1)
+

#Classification tree

+
library(rpart)
+#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)
+
+#Plot and generate a CP table for your tree 
+
+#Generate a probability value that represents the probability that a student levels up based your classification tree 
+rp <- rpart(level.up ~ post.test.score + forum.posts + pre.test.score + av.assignment.score + messages, method = "class", data = D1,control=rpart.control(minsplit=1, minbucket=1, cp=0.001))
+post(rp, file = "rp", title = "MOOC")
+printcp(rp)
+
## 
+## Classification tree:
+## rpart(formula = level.up ~ post.test.score + forum.posts + pre.test.score + 
+##     av.assignment.score + messages, data = D1, method = "class", 
+##     control = rpart.control(minsplit = 1, minbucket = 1, cp = 0.001))
+## 
+## 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.930      0      1.00   1.00 0.038730
+## 2 0.070      1      0.07   0.07 0.013042
+## 3 0.001      2      0.00   0.00 0.000000
+
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.
+
+
+

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
+unlist(slot(performance(pred.detail,"auc"), "y.values"))#Unlist liberates the AUC value from the "performance" object created by ROCR
+
## [1] 1
+
pred.detail2 <- prediction(D1$messages, D1$level.up) 
+plot(performance(pred.detail2, "tpr", "fpr"))
+abline(0, 1, lty = 2)
+

+
unlist(slot(performance(pred.detail2,"auc"), "y.values"))
+
## [1] 0.8978
+
#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?
+
+#The second model better. the measurement 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.
+D1 <- read.csv("online.data.csv", header = TRUE)
+
+c.tree1 <- rpart(level.up ~ pre.test.score + forum.posts, method = "class", data = D1, control=rpart.control(minsplit=1, minbucket=1, cp=0.001))
+
+printcp(c.tree1)
+
## 
+## Classification tree:
+## rpart(formula = level.up ~ pre.test.score + forum.posts, 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.6200 0.034141
+## 4  0.0150000      3    0.5575 0.6225 0.034187
+## 5  0.0050000      4    0.5425 0.6100 0.033954
+## 6  0.0037500      8    0.5225 0.6225 0.034187
+## 7  0.0031250     14    0.4975 0.6325 0.034368
+## 8  0.0025000     25    0.4575 0.6450 0.034590
+## 9  0.0017857     47    0.4025 0.6600 0.034848
+## 10 0.0016667     61    0.3700 0.6450 0.034590
+## 11 0.0015000     67    0.3600 0.6550 0.034763
+## 12 0.0012500     75    0.3475 0.6625 0.034890
+## 13 0.0010714    121    0.2900 0.6625 0.034890
+## 14 0.0010000    128    0.2825 0.6725 0.035057
+
D1$pred1 <- predict(c.tree1, type = "prob")[,2]
+threshold.pred1 <- ifelse(D1$pred1 >= 0.5, "yes", "no")
+
+#Now generate three diagnostics:
+
+D1$accuracy.model1 <-mean(ifelse(D1$level.up == D1$threshold.pred1, 1, 0))
+D1$truepos.model1 <- ifelse(D1$level.up == "yes" & threshold.pred1 == "yes", 1, 0)
+
+D1$falsepos.model1 <- ifelse(D1$level.up == "no" & threshold.pred1 == "yes", 1, 0)
+
+D1$falseneg.model1 <- ifelse(D1$level.up == "yes" & threshold.pred1 == "no", 1, 0)
+
+D1$precision.model1 <- sum(D1$truepos.model1)/(sum(D1$truepos.model1) + sum(D1$falsepos.model1))
+
+D1$recall.model1 <- sum(D1$truepos.model1)/(sum(D1$truepos.model1) + sum(D1$falseneg.model1))
+
+
+#Now choose a different threshold value and repeat these diagnostics. What conclusions can you draw about your two thresholds?
+D1$threshold.pred2 <- ifelse(D1$pred1 >= 0.8, "yes", "no")
+
+D1$accuracy.model2 <- mean(ifelse(D1$level.up == D1$threshold.pred1, 1, 0))
+
+D1$truepos.model2 <- ifelse(D1$level.up == "yes" & D1$threshold.pred2 == "yes", 1, 0)
+
+D1$falsepos.model2 <- ifelse(D1$level.up == "no" & D1$threshold.pred2 == "yes", 1,0)
+
+D1$falseneg.model2 <- ifelse(D1$level.up == "yes" & D1$threshold.pred2 == "no", 1,0)
+
+D1$precision.model2 <- sum(D1$truepos.model2)/(sum(D1$truepos.model2) + sum(D1$falsepos.model2))
+
+D1$recall.model2 <- sum(D1$truepos.model2)/(sum(D1$truepos.model2) + sum(D1$falseneg.model2))
+
+

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.

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