From 856c5b8ecfe7b8833ba4bfe61de8b1ca517b189c Mon Sep 17 00:00:00 2001 From: Shu-Yi Date: Tue, 17 Dec 2019 11:42:22 -0500 Subject: [PATCH] This is the assignment 6! --- Assignment6.Rmd | 12 +- Assignment6.html | 489 +++++++++++++++++++++++++++++++++++++++++++++++ tree1.ps | Bin 0 -> 4728 bytes tree2.ps | Bin 0 -> 4728 bytes 4 files changed, 495 insertions(+), 6 deletions(-) create mode 100644 Assignment6.html create mode 100644 tree1.ps create mode 100644 tree2.ps 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 0000000000000000000000000000000000000000..1a36db213ca45bef6f87aad75fa66477964dbad7 GIT binary patch literal 4728 zcmd5=-*4MS41U*N!94_UFyyQ|NtP6J1)4P70yIs~6xc)22U)r}+GI(STFA`$l6~u!%PjqCqxZ2`v~yaBVR6bl19fAx#Wc& z1+FBUW{3Dd=0nKT_ zgm<{CQs9l6w8)R?ZImZ<9F;72Qf%@h%~vPI-7(34Kc4SXpjwS@UH{w53%|_4{8>*kxhc@VKd>yv|^jLYt}r7BvgqQ=)II16uMHB+6Qp-9BKN zeWK4AQp+??jOct-ne|H7KsRM+W4gXAzLbDTki=)KyC&9M7sz5UVPw2sWof?4$jz3y z#*WSHL?t$yI4f%Xh<9V`+@x7HQ7O!F-BiV&Q^#Y&=&jh~ENqIZc%ls0@X;lO=waUu zgrvAAzmh{ovR&7A@h!q_9z7rgyn_a*^(Ncp?*~yzHn_*zQZZvyb7qz+GB8(tK6()2 zu$Rs=3i}K!Ci1%Y1{40XDtnAHCY~N0O#|A(Y75kY2>+3U>eF`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