From 6d5e2266098fa09f83419fe0a28f043694fb1a75 Mon Sep 17 00:00:00 2001 From: caroline-555 <54962752+caroline-555@users.noreply.github.com> Date: Sun, 8 Dec 2019 00:46:12 -0500 Subject: [PATCH] Assignment 6!!! --- Assignment6.Rmd | 27 ++- Assignment6.html | 544 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 565 insertions(+), 6 deletions(-) create mode 100644 Assignment6.html diff --git a/Assignment6.Rmd b/Assignment6.Rmd index 8e65135..c340591 100644 --- a/Assignment6.Rmd +++ b/Assignment6.Rmd @@ -1,6 +1,6 @@ --- title: "Assignment 6" -author: "Charles Lang" +author: "Jiali Jin" date: "11/16/2016" output: html_document --- @@ -18,6 +18,7 @@ assignment (numeric) - A student's average grade for the course assignments #Packages ```{r} library(rpart) +library(dplyr) ``` #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,method="class",data = M1) #Check the results from the classifcation tree using the printcp() command - +printcp(c.tree1) #Plot your tree @@ -52,7 +53,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 +78,21 @@ 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("https://github.com/bkoester/PLA/blob/master/data/student.record.csv?raw=true",header = TRUE) +#Model 1 +D2<-select(D1,SEX,MAJOR1_DESCR,HSGPA,LAST_ACT_ENGL_SCORE,LAST_ACT_MATH_SCORE,LAST_ACT_READ_SCORE,LAST_ACT_SCIRE_SCORE,LAST_ACT_COMP_SCORE) +D2<-na.omit(D2) +#MODEL 1 +c.tree3<-rpart(SEX~LAST_ACT_ENGL_SCORE+LAST_ACT_MATH_SCORE+LAST_ACT_READ_SCORE+LAST_ACT_SCIRE_SCORE,method="class",data=D2) +printcp(c.tree3) +post(c.tree3, file = "tree3.ps", title = "MOOC") + +#Model 2 +c.tree4<-rpart(SEX~LAST_ACT_READ_SCORE+LAST_ACT_SCIRE_SCORE+HSGPA,method="class",data=D2) +printcp(c.tree4) +post(c.tree3, file = "tree4.ps", title = "MOOC") + +#Model 1 is better, since Model 1 has lower error rate than Model 2. ``` diff --git a/Assignment6.html b/Assignment6.html new file mode 100644 index 0000000..01928c9 --- /dev/null +++ b/Assignment6.html @@ -0,0 +1,544 @@ + + + + + + + + + + + + + + + + +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)
+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
+

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

+
D1<-read.csv("https://github.com/bkoester/PLA/blob/master/data/student.record.csv?raw=true",header = TRUE)
+#Model 1
+D2<-select(D1,SEX,MAJOR1_DESCR,HSGPA,LAST_ACT_ENGL_SCORE,LAST_ACT_MATH_SCORE,LAST_ACT_READ_SCORE,LAST_ACT_SCIRE_SCORE,LAST_ACT_COMP_SCORE)
+D2<-na.omit(D2)
+#MODEL 1
+c.tree3<-rpart(SEX~LAST_ACT_ENGL_SCORE+LAST_ACT_MATH_SCORE+LAST_ACT_READ_SCORE+LAST_ACT_SCIRE_SCORE,method="class",data=D2)
+printcp(c.tree3)
+
## 
+## Classification tree:
+## rpart(formula = SEX ~ LAST_ACT_ENGL_SCORE + LAST_ACT_MATH_SCORE + 
+##     LAST_ACT_READ_SCORE + LAST_ACT_SCIRE_SCORE, data = D2, method = "class")
+## 
+## Variables actually used in tree construction:
+## [1] LAST_ACT_ENGL_SCORE  LAST_ACT_MATH_SCORE  LAST_ACT_SCIRE_SCORE
+## 
+## Root node error: 19930/42586 = 0.46799
+## 
+## n= 42586 
+## 
+##         CP nsplit rel error  xerror      xstd
+## 1 0.189012      0   1.00000 1.00000 0.0051666
+## 2 0.010261      1   0.81099 0.81099 0.0050247
+## 3 0.010000      3   0.79047 0.80050 0.0050118
+
post(c.tree3, file = "tree3.ps", title = "MOOC")
+
+#Model 2
+c.tree4<-rpart(SEX~LAST_ACT_READ_SCORE+LAST_ACT_SCIRE_SCORE+HSGPA,method="class",data=D2)
+printcp(c.tree4)
+
## 
+## Classification tree:
+## rpart(formula = SEX ~ LAST_ACT_READ_SCORE + LAST_ACT_SCIRE_SCORE + 
+##     HSGPA, data = D2, method = "class")
+## 
+## Variables actually used in tree construction:
+## [1] LAST_ACT_SCIRE_SCORE
+## 
+## Root node error: 19930/42586 = 0.46799
+## 
+## n= 42586 
+## 
+##        CP nsplit rel error  xerror      xstd
+## 1 0.16357      0   1.00000 1.00000 0.0051666
+## 2 0.01000      1   0.83643 0.84235 0.0050600
+
post(c.tree3, file = "tree4.ps", title = "MOOC")
+
+#Model 1 is better, since Model 1 has lower error rate than Model 2.
+
+

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.

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