From 85f15511b6f401a7c8b9fb8980bc4c0cda110398 Mon Sep 17 00:00:00 2001 From: Paris Date: Mon, 2 Dec 2019 14:46:59 -0500 Subject: [PATCH] done! --- Assignment6.Rmd | 28 ++- Assignment6.html | 540 ++++++++++++++++++++++++++++++++++++++++++++++ assignment6.Rproj | 13 ++ 3 files changed, 577 insertions(+), 4 deletions(-) create mode 100644 Assignment6.html create mode 100644 assignment6.Rproj diff --git a/Assignment6.Rmd b/Assignment6.Rmd index 8e65135..ffb1ad2 100644 --- a/Assignment6.Rmd +++ b/Assignment6.Rmd @@ -1,6 +1,6 @@ --- title: "Assignment 6" -author: "Charles Lang" +author: "Bozi Jin" date: "11/16/2016" output: html_document --- @@ -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,7 +33,7 @@ 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,M1) #Check the results from the classifcation tree using the printcp() command @@ -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 @@ -77,6 +77,26 @@ 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} +library(dplyr) +library(tidyverse) +student.course<-read.csv("student.course.csv",header = TRUE) +student.course<-filter(student.course, DIV == "P") +student.course$Grade_Level=ifelse(student.course$GPAO>3.3,"A",ifelse(student.course$GPAO>2.3,"B",ifelse(student.course$GPAO>1,"C","F"))) +#split data into train set and test set +set.seed(19) +trainrow <- sample(1:nrow(student.course), size=0.8*nrow(student.course)) +train_set <- student.course[trainrow,] +test_set <- student.course[-trainrow,] +#classification tree +c.tree3<-rpart(Grade_Level ~ .-GPAO,train_set) +printcp(c.tree3) +post(c.tree3,file = "tree3.ps", title = "course 1") +prediction<-predict(c.tree3,test_set) +#pruned classification tree +c.tree4<-rpart(Grade_Level ~ GRD_PTS_PER_UNIT + SUBJECT,train_set) +printcp(c.tree4) +post(c.tree3,file = "tree4.ps", title = "course 1") + ``` diff --git a/Assignment6.html b/Assignment6.html new file mode 100644 index 0000000..41d54cc --- /dev/null +++ b/Assignment6.html @@ -0,0 +1,540 @@ + + + + + + + + + + + + + + + + +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,M1)
+
+#Check the results from the classifcation tree using the printcp() command
+
+
+
+#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  2056   24
+##   yes 7790  130
+
table(M2$certified, M2$predict2)
+
##      
+##         no  yes
+##   no  2056   24
+##   yes 7790  130
+

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

+
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
+
library(tidyverse)
+
## ─ Attaching packages ───────── tidyverse 1.2.1 ─
+
## ✔ ggplot2 3.2.1     ✔ readr   1.3.1
+## ✔ tibble  2.1.3     ✔ purrr   0.3.2
+## ✔ tidyr   1.0.0     ✔ stringr 1.4.0
+## ✔ ggplot2 3.2.1     ✔ forcats 0.4.0
+
## ─ Conflicts ────────── tidyverse_conflicts() ─
+## ✖ dplyr::filter() masks stats::filter()
+## ✖ dplyr::lag()    masks stats::lag()
+
student.course<-read.csv("student.course.csv",header = TRUE)
+student.course<-filter(student.course, DIV == "P")
+student.course$Grade_Level=ifelse(student.course$GPAO>3.3,"A",ifelse(student.course$GPAO>2.3,"B",ifelse(student.course$GPAO>1,"C","F")))
+#split data into train set and test set
+set.seed(19)
+trainrow <- sample(1:nrow(student.course), size=0.8*nrow(student.course))
+train_set <- student.course[trainrow,]
+test_set <- student.course[-trainrow,]
+#classification tree
+c.tree3<-rpart(Grade_Level ~ .-GPAO,train_set)
+printcp(c.tree3)
+
## 
+## Classification tree:
+## rpart(formula = Grade_Level ~ . - GPAO, data = train_set)
+## 
+## Variables actually used in tree construction:
+## [1] GRD_PTS_PER_UNIT SUBJECT         
+## 
+## Root node error: 21662/62824 = 0.3448
+## 
+## n= 62824 
+## 
+##         CP nsplit rel error  xerror      xstd
+## 1 0.155156      0   1.00000 1.00000 0.0054997
+## 2 0.050111      1   0.84484 0.84484 0.0052574
+## 3 0.010000      3   0.74462 0.74527 0.0050560
+
post(c.tree3,file = "tree3.ps", title = "course 1")
+prediction<-predict(c.tree3,test_set)
+#pruned classification tree
+c.tree4<-rpart(Grade_Level ~ GRD_PTS_PER_UNIT + SUBJECT,train_set)
+printcp(c.tree4)
+
## 
+## Classification tree:
+## rpart(formula = Grade_Level ~ GRD_PTS_PER_UNIT + SUBJECT, data = train_set)
+## 
+## Variables actually used in tree construction:
+## [1] GRD_PTS_PER_UNIT SUBJECT         
+## 
+## Root node error: 21662/62824 = 0.3448
+## 
+## n= 62824 
+## 
+##         CP nsplit rel error  xerror      xstd
+## 1 0.155156      0   1.00000 1.00000 0.0054997
+## 2 0.050111      1   0.84484 0.84484 0.0052574
+## 3 0.010000      3   0.74462 0.74615 0.0050580
+
post(c.tree3,file = "tree4.ps", title = "course 1")
+
+

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.Rproj b/assignment6.Rproj new file mode 100644 index 0000000..8e3c2eb --- /dev/null +++ b/assignment6.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