From 9533243821db743ff196b26779925ecbb9285697 Mon Sep 17 00:00:00 2001 From: k-david-pearce <54962664+k-david-pearce@users.noreply.github.com> Date: Sun, 15 Dec 2019 01:33:19 -0500 Subject: [PATCH] Completed Parts I and II. Still trying to understand the comparison of M2$predict1 and M2$predict2 --- Assignment6.Rmd | 11 +- Assignment6.html | 319 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 324 insertions(+), 6 deletions(-) create mode 100644 Assignment6.html diff --git a/Assignment6.Rmd b/Assignment6.Rmd index 8e65135..ab027a8 100644 --- a/Assignment6.Rmd +++ b/Assignment6.Rmd @@ -24,8 +24,7 @@ library(rpart) ```{r} #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 +32,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 ~ grade + assignment, method="class", data=M1) #Check the results from the classifcation tree using the printcp() command - +printcp(c.tree1) #Plot your tree @@ -52,7 +51,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.058182) #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 @@ -77,7 +76,7 @@ table(M2$certified, M2$predict2) 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? ```{r} - +A1 <- read.csv("https://github.com/bkoester/PLA/blob/master/data/student.course.csv") ``` diff --git a/Assignment6.html b/Assignment6.html new file mode 100644 index 0000000..3657c86 --- /dev/null +++ b/Assignment6.html @@ -0,0 +1,319 @@ + + + + + + + + + + + + + + + +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 ~ grade + assignment, method="class", data=M1)
+
+#Check the results from the classifcation tree using the printcp() command
+
+printcp(c.tree1)
+
## 
+## Classification tree:
+## rpart(formula = certified ~ grade + assignment, data = M1, method = "class")
+## 
+## Variables actually used in tree construction:
+## [1] assignment grade     
+## 
+## Root node error: 275/1000 = 0.275
+## 
+## n= 1000 
+## 
+##         CP nsplit rel error   xerror      xstd
+## 1 0.923636      0  1.000000 1.000000 0.0513455
+## 2 0.058182      1  0.076364 0.076364 0.0164880
+## 3 0.010000      2  0.018182 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.058182) #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  2056   24
+##   yes 7790  130
+
table(M2$certified, M2$predict2)
+
##      
+##         no  yes
+##   no   896 1184
+##   yes 3453 4467
+
+

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?

+
A1 <- read.csv("https://github.com/bkoester/PLA/blob/master/data/student.course.csv")
+
+

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.

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