diff --git a/Assignment6.Rmd b/Assignment6.Rmd index 8e65135..e8bfd4d 100644 --- a/Assignment6.Rmd +++ b/Assignment6.Rmd @@ -1,7 +1,7 @@ --- title: "Assignment 6" -author: "Charles Lang" -date: "11/16/2016" +author: "Shu-Yi Hsu" +date: "12/17/2016" output: html_document --- #Addignment 6 @@ -25,7 +25,7 @@ library(rpart) #Upload the data sets MOOC1.csv and MOOC2.csv M1 <- read.csv("MOOC1.csv", header = TRUE) -M2 <- +M2 <- read.csv("MOOC2.csv", header = TRUE) ``` @@ -33,11 +33,11 @@ M2 <- ```{r} #Using the rpart package generate a classification tree predicting certified from the other variables in the M1 data frame. Which variables should you use? -c.tree1 <- +c.tree1 <- rpart(certified~ forum.posts+grade, method ="class", data = M1) #Check the results from the classifcation tree using the printcp() command - +printcp(c.tree1) #Plot your tree @@ -52,7 +52,7 @@ post(c.tree1, file = "tree1.ps", title = "MOOC") #This creates a pdf image of th #If we are worried about overfitting we can remove nodes form our tree using the prune() command, setting cp to the CP value from the table that corresponds to the number of nodes we want the tree to terminate at. Let's set it to two nodes. ```{r} -c.tree2 <- prune(c.tree1, cp = )#Set cp to the level at which you want the tree to end +c.tree2 <- prune(c.tree1, cp = 0.01)#Set cp to the level at which you want the tree to end #Visualize this tree and compare it to the one you generated earlier diff --git a/Assignment6.html b/Assignment6.html new file mode 100644 index 0000000..0cd4fce --- /dev/null +++ b/Assignment6.html @@ -0,0 +1,489 @@ + + + + + + + + + + + + + + + + +Assignment 6 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + +

#Addignment 6

+

In this assignment you will be looking at data from a MOOC. It contains the following per-student variables:

+

certified (yes/no) - Whether or not a student paid for the course
+forum.posts (numeric) - How many forum posts a student made throughout the course
+grade (numeric) - A student’s average grade for the course exam
+assignment (numeric) - A student’s average grade for the course assignments

+

##Part I

+

#Packages

+
library(rpart)
+

#Data

+
#Upload the data sets MOOC1.csv and MOOC2.csv
+M1 <- read.csv("MOOC1.csv", header = TRUE)
+
+M2 <- read.csv("MOOC2.csv", header = TRUE)
+

#Decision tree

+
#Using the rpart package generate a classification tree predicting certified from the other variables in the M1 data frame. Which variables should you use?
+
+c.tree1 <- rpart(certified~ forum.posts+grade, method ="class", data = M1)
+
+#Check the results from the classifcation tree using the printcp() command
+
+printcp(c.tree1)
+
## 
+## Classification tree:
+## rpart(formula = certified ~ forum.posts + grade, data = M1, method = "class")
+## 
+## Variables actually used in tree construction:
+## [1] forum.posts
+## 
+## Root node error: 275/1000 = 0.275
+## 
+## n= 1000 
+## 
+##        CP nsplit rel error   xerror      xstd
+## 1 0.98545      0  1.000000 1.000000 0.0513455
+## 2 0.01000      1  0.014545 0.018182 0.0081108
+
#Plot your tree
+
+post(c.tree1, file = "tree1.ps", title = "MOOC") #This creates a pdf image of the tree
+

##Part II

+

#The heading “xerror” in the printcp table stands for “cross validation error”, it is the error rate of assigning students to certified/uncertified of the model averaged over 10-fold cross validation. CP stands for “Complexity Parameter” and represents the cost to error for adding a node to the tree. Notice it decreases as we add more nodes to the tree which implies that more nodes make better predictions. However, more nodes also mean that we may be making the model less generalizable, this is known as “overfitting”.

+

#If we are worried about overfitting we can remove nodes form our tree using the prune() command, setting cp to the CP value from the table that corresponds to the number of nodes we want the tree to terminate at. Let’s set it to two nodes.

+
c.tree2 <- prune(c.tree1, cp = 0.01)#Set cp to the level at which you want the tree to end
+
+#Visualize this tree and compare it to the one you generated earlier
+
+post(c.tree2, file = "tree2.ps", title = "MOOC") #This creates a pdf image of the tree
+

#Now use both the original tree and the pruned tree to make predictions about the the students in the second data set. Which tree has a lower error rate?

+
M2$predict1 <- predict(c.tree1, M2, type = "class")
+
+M2$predict2 <- predict(c.tree2, M2, type = "class")
+
+table(M2$certified, M2$predict1)
+
##      
+##         no  yes
+##   no   943 1137
+##   yes 3590 4330
+
table(M2$certified, M2$predict2)
+
##      
+##         no  yes
+##   no   943 1137
+##   yes 3590 4330
+

##Part III

+

Choose a data file from the (University of Michigan Open Data Set)[https://github.com/bkoester/PLA/tree/master/data]. Choose an outcome variable that you would like to predict. Build two models that predict that outcome from the other variables. The first model should use raw variables, the second should feature select or feature extract variables from the data. Which model is better according to the cross validation metrics?

+
+

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/tree1.ps b/tree1.ps new file mode 100644 index 0000000..1a36db2 Binary files /dev/null and b/tree1.ps differ diff --git a/tree2.ps b/tree2.ps new file mode 100644 index 0000000..1a36db2 Binary files /dev/null and b/tree2.ps differ