From 0dca32270b3524d8842b68d81648eb42eb47621d Mon Sep 17 00:00:00 2001 From: Tianyi Wang Date: Wed, 4 Dec 2019 00:01:38 -0500 Subject: [PATCH] ok --- Assignment7.Rmd | 84 ++++++- Assignment7.html | 590 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 666 insertions(+), 8 deletions(-) create mode 100644 Assignment7.html diff --git a/Assignment7.Rmd b/Assignment7.Rmd index 105cbdf..f9b329e 100644 --- a/Assignment7.Rmd +++ b/Assignment7.Rmd @@ -1,7 +1,7 @@ --- title: "Assignment 7 - Answers" -author: "Charles Lang" -date: "11/30/2016" +author: "Qiyang (Minnie) Lin" +date: "11/26/2019" output: html_document --- @@ -11,24 +11,49 @@ 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(rpart) +library(rpart.plot) ``` #Visualization ```{r} #Start by creating histograms of the distributions for all variables (#HINT: look up "facet" in the ggplot documentation) +D2<- gather(D1, key, value) + +ggplot(D2, aes(value,key) )+ + facet_wrap(~ key , scales = "free")+ + geom_histogram(stat = "identity") + + #Then visualize the relationships between variables +pairs(D1, labels = colnames(D1), main = "Relationships Between Variables",upper.panel = NULL, pch = 21, cex = 0.5, bg = c("#00AFBB", "#FC4E07")[D1$level.up]) + #Try to capture an intution about the data and the relationships + +#Answer: About the data, all the numerical variables appear to be normal shape. id is uniform shape. And the propotion of level up students are less than not level up students. +#id seems to show no relationship between any of other variables. The number of messages sent by students are clearly(and highly) postively correltate with post test scores. Other vaiables, such as pre-test scores, form.posts, have relatively postive correlation with other variables except id. Level up seems to be postively related to all the variables except id and forum posts. Those who did level up have higher pre-test, post-test, and average assignment score. + ``` #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) +rp<- rpart(level.up ~ post.test.score + messages + av.assignment.score, data = D1 ) + +# I choose these three variables because (1)they have correlation between level up. (2)Teachers can track their process during the use of online system(av.assignment.score) and their social interatction(messages), and then after the use of system(post.test.score) + + #Plot and generate a CP table for your tree +printcp(rp) +rpart.plot(rp) + #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. @@ -36,6 +61,7 @@ D1$pred <- predict(rp, type = "prob")[,2]#Last class we used type = "class" whic ## Part II #Now you can generate the ROC curve for your model. You will need to install the package ROCR to do this. ```{r} +#install.packages("ROCR") library(ROCR) #Plot the curve @@ -44,24 +70,41 @@ 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 #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? +rp2<- rpart(level.up ~ forum.posts + pre.test.score, data = D1 ) + +printcp(rp2) +rpart.plot(rp2) + +D1$pred2 <- predict(rp2, type = "prob")[,2] +pred.detail2<- prediction(D1$pred2, D1$level.up) +plot(performance(pred.detail2, "tpr", "fpr")) +abline(0,1,lty = 2) + +#The first model is better. Becuase ROC curve in the first model have 100% sensitivy(TPR) and 100% specificy (FPR). The area under the curve is 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 <- +##since my first model is perfect accroding to the ROC curve. I will choose the second model to do this part. +D1$threshold.pred1 <- ifelse(D1$pred2 >= 0.5, 1,0) +D1$truepos1<- ifelse(D1$level.up == "yes" & D1$threshold.pred1 == "1",1,0) +D1$falsepos1<- ifelse(D1$level.up =="no" & D1$threshold.pred1 =="1",1,0) +D1$falseneg1<- ifelse(D1$level.up == "yes"& D1$threshold.pred1 == "0", 1,0) + #Now generate three diagnostics: -D1$accuracy.model1 <- +D1$accuracy.model1 <- mean(ifelse(D1$threshold.pred1 == D1$level.up, 1,0)) -D1$precision.model1 <- +D1$precision.model1 <- sum(D1$truepos1)/(sum(D1$truepos1) + sum(D1$falsepos1)) -D1$recall.model1 <- +D1$recall.model1 <- sum(D1$truepos1)/(sum(D1$truepos1)+sum(D1$falseneg1)) #Finally, calculate Kappa for your model according to: @@ -73,9 +116,34 @@ matrix1 <- as.matrix(table1) #Calculate kappa kappa(matrix1, exact = TRUE)/kappa(matrix1) +#kappa for model 1 is 1.056 #Now choose a different threshold value and repeat these diagnostics. What conclusions can you draw about your two thresholds? +#threshold 2 = 0.7 +D1$threshold.pred2 <- ifelse(D1$pred2 >= 0.7, 1,0) +D1$truepos2<- ifelse(D1$level.up == "yes" & D1$threshold.pred1 == "1",1,0) +D1$falsepos2<- ifelse(D1$level.up =="no" & D1$threshold.pred1 =="1",1,0) +D1$falseneg2<- ifelse(D1$level.up == "yes"& D1$threshold.pred1 == "0", 1,0) + + +D1$accuracy.model2 <- mean(ifelse(D1$threshold.pred2 == D1$level.up, 1,0)) + +D1$precision.model2 <- sum(D1$truepos2)/(sum(D1$truepos2) + sum(D1$falsepos2)) + +D1$recall.model2 <- sum(D1$truepos2)/(sum(D1$truepos2)+sum(D1$falseneg2)) + +table2 <- table(D1$level.up, D1$threshold.pred2) + +#Convert to matrix +matrix2 <- as.matrix(table2) + +#Calculate kappa +kappa(matrix2, exact = TRUE)/kappa(matrix2) +#1.034 + +#1.056>1.034, model 1 has slightly higher kappa. + ``` ### To Submit Your Assignment diff --git a/Assignment7.html b/Assignment7.html new file mode 100644 index 0000000..0b92909 --- /dev/null +++ b/Assignment7.html @@ -0,0 +1,590 @@ + + + + + + + + + + + + + + + +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

+
D1<- read.csv("online.data.csv")
+library(ggplot2)
+library(tidyr)
+library(rpart)
+library(rpart.plot)
+

#Visualization

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

+
#Then visualize the relationships between variables
+
+pairs(D1, labels = colnames(D1), main = "Relationships Between Variables",upper.panel = NULL, pch = 21, cex = 0.5,  bg = c("#00AFBB", "#FC4E07")[D1$level.up])
+

+
#Try to capture an intution about the data and the relationships
+
+
+#Answer: About the data, all the numerical variables appear to be normal shape. id is uniform shape. And the propotion of level up students are less than not level up students. 
+#id seems to show no relationship between any of other variables. The number of messages sent by students are clearly(and highly) postively correltate with post test scores. Other vaiables, such as pre-test scores, form.posts, have relatively postive correlation with other variables except id. Level up seems to be postively related to all the variables except id and forum posts. Those who did level up have higher pre-test, post-test, and average assignment score. 
+

#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)
+
+rp<- rpart(level.up ~ post.test.score + messages + av.assignment.score, data = D1 )
+
+# I choose these three variables because (1)they have correlation between level up. (2)Teachers can track their process during the use of online system(av.assignment.score) and their social interatction(messages), and then after the use of system(post.test.score)
+
+
+#Plot and generate a CP table for your tree 
+
+printcp(rp)
+
## 
+## Classification tree:
+## rpart(formula = level.up ~ post.test.score + messages + av.assignment.score, 
+##     data = D1)
+## 
+## 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
+
rpart.plot(rp)
+

+
#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.
+
+
+

Part II

+

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

+
#install.packages("ROCR")
+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
+
#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?
+rp2<- rpart(level.up ~ forum.posts + pre.test.score, data = D1 )
+
+printcp(rp2)
+
## 
+## Classification tree:
+## rpart(formula = level.up ~ forum.posts + pre.test.score, data = D1)
+## 
+## 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.3925      0    1.0000 1.0000 0.038730
+## 2 0.0300      1    0.6075 0.6075 0.033907
+## 3 0.0200      2    0.5775 0.5975 0.033716
+## 4 0.0150      3    0.5575 0.5850 0.033471
+## 5 0.0100      4    0.5425 0.5750 0.033270
+
rpart.plot(rp2)
+

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

+
#The first model is better. Becuase ROC curve in the first model have 100% sensitivy(TPR) and 100% specificy (FPR). The area under the curve is 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.
+
+##since my first model is perfect accroding to the ROC curve. I will choose the second model to do this part. 
+D1$threshold.pred1 <- ifelse(D1$pred2 >= 0.5, 1,0)
+D1$truepos1<- ifelse(D1$level.up == "yes" & D1$threshold.pred1 == "1",1,0)
+D1$falsepos1<- ifelse(D1$level.up =="no" & D1$threshold.pred1 =="1",1,0)
+D1$falseneg1<- ifelse(D1$level.up == "yes"& D1$threshold.pred1 == "0", 1,0)
+
+
+#Now generate three diagnostics:
+
+D1$accuracy.model1 <- mean(ifelse(D1$threshold.pred1 == D1$level.up, 1,0))
+
+D1$precision.model1 <- sum(D1$truepos1)/(sum(D1$truepos1) + sum(D1$falsepos1))
+
+D1$recall.model1 <- sum(D1$truepos1)/(sum(D1$truepos1)+sum(D1$falseneg1))
+
+#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.056434
+
#kappa for model 1 is 1.056
+
+#Now choose a different threshold value and repeat these diagnostics. What conclusions can you draw about your two thresholds?
+
+#threshold 2 = 0.7
+D1$threshold.pred2 <- ifelse(D1$pred2 >= 0.7, 1,0)
+D1$truepos2<- ifelse(D1$level.up == "yes" & D1$threshold.pred1 == "1",1,0)
+D1$falsepos2<- ifelse(D1$level.up =="no" & D1$threshold.pred1 =="1",1,0)
+D1$falseneg2<- ifelse(D1$level.up == "yes"& D1$threshold.pred1 == "0", 1,0)
+
+
+D1$accuracy.model2 <- mean(ifelse(D1$threshold.pred2 == D1$level.up, 1,0))
+
+D1$precision.model2 <- sum(D1$truepos2)/(sum(D1$truepos2) + sum(D1$falsepos2))
+
+D1$recall.model2 <- sum(D1$truepos2)/(sum(D1$truepos2)+sum(D1$falseneg2))
+
+table2 <- table(D1$level.up, D1$threshold.pred2)
+
+#Convert to matrix
+matrix2 <- as.matrix(table2)
+
+#Calculate kappa
+kappa(matrix2, exact = TRUE)/kappa(matrix2)
+
## [1] 1.034764
+
#1.034
+
+#1.056>1.034, model 1 has slightly higher kappa. 
+
+

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.

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