From 54855068a6d8f53b636118b3a288d91b70d6054e Mon Sep 17 00:00:00 2001 From: hsiaotingyang Date: Sun, 17 Nov 2019 22:48:30 -0500 Subject: [PATCH] Assignment 5 completed --- Assignment 5.Rmd | 41 +++- Assignment-5.html | 508 ++++++++++++++++++++++++++++++++++++++++++++++ assignment5.Rproj | 13 ++ tree.ps | Bin 0 -> 8044 bytes 4 files changed, 554 insertions(+), 8 deletions(-) create mode 100644 Assignment-5.html create mode 100644 assignment5.Rproj create mode 100644 tree.ps diff --git a/Assignment 5.Rmd b/Assignment 5.Rmd index 8838dc9..ac38ba6 100644 --- a/Assignment 5.Rmd +++ b/Assignment 5.Rmd @@ -1,6 +1,6 @@ --- title: "Assignment 5 - Decision Trees" -author: "Charles Lang" +author: "Hsiao Ting Yang" date: "November 9, 2016" output: html_document --- @@ -8,7 +8,6 @@ For this assignment we will be using data from the Assistments Intelligent Tutor #Install & call libraries ```{r} -install.packages("party", "rpart") library(rpart) library(party) @@ -16,7 +15,7 @@ library(party) ## Part I ```{r} -D1 <- +D1 <- read.csv("intelligent_tutor.csv", header = TRUE) ``` ##Classification Tree @@ -40,42 +39,68 @@ We want to see if we can build a decision tree to help teachers decide which stu #Visualize our outcome variable "score" ```{r} - +hist(D1$score) ``` #Create a categorical outcome variable based on student score to advise the teacher using an "ifelse" statement ```{r} -D1$advice <- + +D1$advice <- ifelse(D1$score <= 0.4, "intervene", ifelse(D1$score >0.4 & D1$score <=0.8, "monitor", "no action")) + ``` #Build a decision tree that predicts "advice" based on how many problems students have answered before, the percentage of those problems they got correct and how many hints they required ```{r} -score_ctree <- + +score_ctree <- ctree(as.factor(D1$advice) ~ prior_prob_count + prior_percent_correct + hints, data = D1) + ``` #Plot tree ```{r} +plot(score_ctree) + ``` Please interpret the tree, which two behaviors do you think the teacher should most closely pay attemtion to? +##Comment +I think the teacher should focus on node 7 and node 9 since those nodes have the higher percentage intervention based on the categorical outcome I setted. For node 7, these group of students have asked for less than 12 hints and answered less than 62.9% correctly. For node 9, these group of students have asked for more than 12 hints. These two behaviors should be what teachers should closely pay attention to since they've asked the most hints and answered the least amount of questions correctly. + #Test Tree Upload the data "intelligent_tutor_new.csv". This is a data set of a differnt sample of students doing the same problems in the same system. We can use the tree we built for the previous data set to try to predict the "advice" we should give the teacher about these new students. ```{r} #Upload new data -D2 <- +D2 <- read.csv("intelligent_tutor_new.csv", header = TRUE) #Generate predicted advice using the predict() command for new students based on tree generated from old students -D2$prediction <- +D2$prediction <- predict(score_ctree, D2) ``` ## Part III Compare the predicted advice with the actual advice that these students recieved. What is the difference between the observed and predicted results? +```{r} + +D2$advice <- ifelse(D2$score <= 0.4, "intervene", ifelse(D2$score >0.4 & D2$score <=0.8, "monitor", "no action")) + +T1 <- table(D2$prediction) + +#monitor +T1[2]/sum(T1) + +#no action +T1[3]/sum(T1) + +``` +##Comment +Our prediction is off but we are still able to capture some patterns. Most of the people are in the "no action" category, so that's why it has the highest percentage with 58%. The next group is the "monitor", which has 42%. + + ### 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/Assignment-5.html b/Assignment-5.html new file mode 100644 index 0000000..06a90f8 --- /dev/null +++ b/Assignment-5.html @@ -0,0 +1,508 @@ + + + + + + + + + + + + + + + + +Assignment 5 - Decision Trees + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + +

For this assignment we will be using data from the Assistments Intelligent Tutoring system. This system gives students hints based on how they perform on math problems.

+

#Install & call libraries

+
library(rpart)
+library(party)
+
## Loading required package: grid
+
## Loading required package: mvtnorm
+
## Loading required package: modeltools
+
## Loading required package: stats4
+
## Loading required package: strucchange
+
## Loading required package: zoo
+
## 
+## Attaching package: 'zoo'
+
## The following objects are masked from 'package:base':
+## 
+##     as.Date, as.Date.numeric
+
## Loading required package: sandwich
+
+

Part I

+
D1 <- read.csv("intelligent_tutor.csv", header = TRUE)
+

##Classification Tree First we will build a classification tree to predict which students ask a teacher for help, which start a new session, or which give up, based on whether or not the student completed a session (D1\(complete) and whether or not they asked for hints (D1\)hint.y).

+
c.tree <- rpart(action ~ hint.y + complete, method="class", data=D1) #Notice the standard R notion for a formula X ~ Y
+
+#Look at the error of this tree
+printcp(c.tree)
+
## 
+## Classification tree:
+## rpart(formula = action ~ hint.y + complete, data = D1, method = "class")
+## 
+## Variables actually used in tree construction:
+## [1] complete hint.y  
+## 
+## Root node error: 250/378 = 0.66138
+## 
+## n= 378 
+## 
+##      CP nsplit rel error xerror     xstd
+## 1 0.052      0     1.000  1.104 0.034520
+## 2 0.012      1     0.948  1.064 0.035511
+## 3 0.010      2     0.936  1.072 0.035325
+
#Plot the tree
+post(c.tree, file = "tree.ps", title = "Session Completion Action: 1 - Ask teacher, 2 - Start new session, 3 - Give up")
+
+
+

Part II

+

#Regression Tree

+

We want to see if we can build a decision tree to help teachers decide which students to follow up with, based on students’ performance in Assistments. We will create three groups (“teacher should intervene”, “teacher should monitor student progress” and “no action”) based on students’ previous use of the system and how many hints they use. To do this we will be building a decision tree using the “party” package. The party package builds decision trees based on a set of statistical stopping rules.

+

#Visualize our outcome variable “score”

+
hist(D1$score)
+

+

#Create a categorical outcome variable based on student score to advise the teacher using an “ifelse” statement

+
D1$advice <- ifelse(D1$score <= 0.4, "intervene", ifelse(D1$score >0.4 & D1$score <=0.8, "monitor", "no action"))
+

#Build a decision tree that predicts “advice” based on how many problems students have answered before, the percentage of those problems they got correct and how many hints they required

+
score_ctree <- ctree(as.factor(D1$advice) ~ prior_prob_count + prior_percent_correct + hints, data = D1)
+

#Plot tree

+
plot(score_ctree)
+

+

Please interpret the tree, which two behaviors do you think the teacher should most closely pay attemtion to?

+

##Comment I think the teacher should focus on node 7 and node 9 since those nodes have the higher percentage intervention based on the categorical outcome I setted. For node 7, these group of students have asked for less than 12 hints and answered less than 62.9% correctly. For node 9, these group of students have asked for more than 12 hints. These two behaviors should be what teachers should closely pay attention to since they’ve asked the most hints and answered the least amount of questions correctly.

+

#Test Tree Upload the data “intelligent_tutor_new.csv”. This is a data set of a differnt sample of students doing the same problems in the same system. We can use the tree we built for the previous data set to try to predict the “advice” we should give the teacher about these new students.

+
#Upload new data
+
+D2 <- read.csv("intelligent_tutor_new.csv", header = TRUE)
+
+#Generate predicted advice using the predict() command for new students based on tree generated from old students
+
+D2$prediction <- predict(score_ctree, D2)
+
+
+

Part III

+

Compare the predicted advice with the actual advice that these students recieved. What is the difference between the observed and predicted results?

+
D2$advice <- ifelse(D2$score <= 0.4, "intervene", ifelse(D2$score >0.4 & D2$score <=0.8, "monitor", "no action"))
+
+T1 <- table(D2$prediction)
+
+#monitor
+T1[2]/sum(T1)
+
## monitor 
+##    0.42
+
#no action
+T1[3]/sum(T1)
+
## no action 
+##      0.58
+

##Comment Our prediction is off but we are still able to capture some patterns. Most of the people are in the “no action” category, so that’s why it has the highest percentage with 58%. The next group is the “monitor”, which has 42%.

+
+

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/assignment5.Rproj b/assignment5.Rproj new file mode 100644 index 0000000..8e3c2eb --- /dev/null +++ b/assignment5.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/tree.ps b/tree.ps new file mode 100644 index 0000000000000000000000000000000000000000..2fd2b6f8a73870ef0429354d165eaeb7d9089d20 GIT binary patch literal 8044 zcmds6TXWmS6@JIBIDOEJJPonLjUYW~W+a<-)W();*>w|-A3$J5A_f5j7t5;Z|K9KH z1q5W-j?-x;Z6cFcocnF}?BUTLo?XmNvT7q|fjf7OjviKNzn5kARLV?d&t+TnO)A^_ zbXk=heJ_i5vdhy1n18(WYIa%`*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