From dc034230657575fcd4637b7d5ac7a20735506621 Mon Sep 17 00:00:00 2001 From: Maho Hayashi Date: Tue, 26 Nov 2019 17:01:04 -0500 Subject: [PATCH] Assignment 6 submission --- Assignment6.Rmd | 46 ++- Assignment6.html | 547 +++++++++++++++++++++++++++++++++ assignment6_Maho Hayashi.Rproj | 13 + tree1.ps | Bin 0 -> 5823 bytes tree2.ps | Bin 0 -> 4675 bytes 5 files changed, 600 insertions(+), 6 deletions(-) create mode 100644 Assignment6.html create mode 100644 assignment6_Maho Hayashi.Rproj create mode 100644 tree1.ps create mode 100644 tree2.ps diff --git a/Assignment6.Rmd b/Assignment6.Rmd index 8e65135..5e5184c 100644 --- a/Assignment6.Rmd +++ b/Assignment6.Rmd @@ -1,7 +1,7 @@ --- title: "Assignment 6" -author: "Charles Lang" -date: "11/16/2016" +author: "Maho Hayashi" +date: "11/24/2019" output: html_document --- #Addignment 6 @@ -18,6 +18,7 @@ assignment (numeric) - A student's average grade for the course assignments #Packages ```{r} library(rpart) + ``` #Data @@ -25,7 +26,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 +34,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, data = M1) #Check the results from the classifcation tree using the printcp() command - +printcp(c.tree1) #Plot your tree @@ -52,10 +53,12 @@ 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 +printcp(c.tree2) + post(c.tree2, file = "tree2.ps", title = "MOOC") #This creates a pdf image of the tree ``` @@ -68,8 +71,21 @@ M2$predict2 <- predict(c.tree2, M2, type = "class") table(M2$certified, M2$predict1) +sum(diag(table(M2$certified, M2$predict1)))/sum(table(M2$certified, M2$predict1)) + + table(M2$certified, M2$predict2) +sum(diag(table(M2$certified, M2$predict2)))/sum(table(M2$certified, M2$predict2)) + +#The first model has an accuracy rate of 21.8% whereas the second model has the accuracy rate of 53.6%. This tells us that the second model probably has the lower error rate... + + +``` + + +```{r} + ``` ##Part III @@ -77,6 +93,24 @@ 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} +D1 <- read.csv("student.course.csv", header = TRUE) +library(dplyr) + +#Outcome variable <- grade point per unit + +c.tree3 <- rpart(GRD_PTS_PER_UNIT~ANONID + GPAO, data = D1) + +printcp(c.tree3) + +D2 <- D1 %>% select (-SUBJECT, -DIV) +D2 <- scale (D2, center = TRUE) + +pca <- prcomp (D2, scale = TRUE) + + +D3 <- data.frame(pca$x) + + ``` diff --git a/Assignment6.html b/Assignment6.html new file mode 100644 index 0000000..05e6c82 --- /dev/null +++ b/Assignment6.html @@ -0,0 +1,547 @@ + + + + + + + + + + + + + + + + +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, 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)
+## 
+## 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
+
+printcp(c.tree2)
+
## 
+## Classification tree:
+## rpart(formula = certified ~ grade + assignment, data = M1)
+## 
+## Variables actually used in tree construction:
+## [1] assignment
+## 
+## Root node error: 275/1000 = 0.275
+## 
+## n= 1000 
+## 
+##         CP nsplit rel error   xerror     xstd
+## 1 0.923636      0  1.000000 1.000000 0.051346
+## 2 0.058182      1  0.076364 0.076364 0.016488
+
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
+
sum(diag(table(M2$certified, M2$predict1)))/sum(table(M2$certified, M2$predict1))
+
## [1] 0.2186
+
table(M2$certified, M2$predict2)
+
##      
+##         no  yes
+##   no   896 1184
+##   yes 3453 4467
+
sum(diag(table(M2$certified, M2$predict2)))/sum(table(M2$certified, M2$predict2))
+
## [1] 0.5363
+
#The first model has an accuracy rate of 21.8% whereas the second model has the accuracy rate of 53.6%. This tells us that the second model probably has the lower error rate... 
+

##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?

+
D1 <- read.csv("student.course.csv", header = TRUE)
+library(dplyr)
+
## 
+## Attaching package: 'dplyr'
+
## The following objects are masked from 'package:stats':
+## 
+##     filter, lag
+
## The following objects are masked from 'package:base':
+## 
+##     intersect, setdiff, setequal, union
+
#Outcome variable <- grade point per unit 
+
+c.tree3 <- rpart(GRD_PTS_PER_UNIT~ANONID + GPAO, data = D1)
+
+printcp(c.tree3)
+
## 
+## Regression tree:
+## rpart(formula = GRD_PTS_PER_UNIT ~ ANONID + GPAO, data = D1)
+## 
+## Variables actually used in tree construction:
+## [1] GPAO
+## 
+## Root node error: 1200199/1327065 = 0.9044
+## 
+## n= 1327065 
+## 
+##         CP nsplit rel error  xerror      xstd
+## 1 0.199996      0   1.00000 1.00000 0.0018315
+## 2 0.036346      1   0.80000 0.80014 0.0015552
+## 3 0.034488      2   0.76366 0.76364 0.0015132
+## 4 0.010000      3   0.72917 0.72939 0.0014954
+
D2 <- D1 %>% select (-SUBJECT, -DIV)
+D2 <- scale (D2, center = TRUE)
+
+pca <- prcomp (D2, scale = TRUE)
+
+
+D3 <- data.frame(pca$x)
+
+

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/assignment6_Maho Hayashi.Rproj b/assignment6_Maho Hayashi.Rproj new file mode 100644 index 0000000..8e3c2eb --- /dev/null +++ b/assignment6_Maho Hayashi.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/tree1.ps b/tree1.ps new file mode 100644 index 0000000000000000000000000000000000000000..e71d1b571d4a6f2dfe65bb8b2415f47d22392fdd GIT binary patch literal 5823 zcmd5=ZEu{$7XHq!IQ?L0qiScF0T!^*NVSvnMoQ`^snicqKCm!e)5`)E*u2Qs|Gm!{ z7#7xRH?34Dtz$Sd=ggevyyLSU-o9V{oK+jW^n~l2ojtG8{Z5zNuUcn1d#BrK-=w-d zr|YWh=s&vnq`N##!1?3R((-{fgaSNpEsci6gYbkbD~Z@#atyU$6(I=oG`3}=ya zy3F{5t0o8Dq|2-FoL(no)}~3#oEO!;%<^)3QGGcl8SzgLh7>6un=i`jvf8nwT4vST zynNeKMYVNSo0{mBwk@^qez{+)Ks`pGt(Vq`r0|84Ie?GZbdzu zgH3Zz@=1ehlb4xcU2Pk)-sl$S=3Cla-QHB6YrrIM;%6$mBbMD&@M1OFD0sao^7442 zG)I;iCw4b8k<{#@Mb+wid)MZk>%1stB9&QgyQcc@T=CRw*jrrk)-EMYIuizL=;)Jz z^>FS0LQ-1QU&*7T?AP^|^akcO4+{teAE1G2w=a(OPlKo>UwA^>TJgrVWzTH3WMFQs zKP-r;*=z3+fm0W(XZ*VQhAYBHMNVAO8G8n_oEx-<)d#2p7XD8XqECNeTOc7m=NUqw zu4=o@7A^G_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