forked from core-methods-in-edm/assignment6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment6.Rmd
83 lines (60 loc) · 3.83 KB
/
Assignment6.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
---
title: "Assignment 6"
author: "Charles Lang"
date: "11/27/2018"
output: html_document
---
#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
#Packages
```{r}
library(rpart)
```
#Data
```{r}
#Upload the data sets MOOC1.csv and MOOC2.csv
M1 <- read.csv("MOOC1.csv", header = TRUE)
View(M1)
M2 <- read.csv("MOOC2.csv", header = TRUE)
View(M2)
```
#Decision tree
```{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?
# Below I use ctree to construct a model of certification as a function of all other variables, including forum posts, grades and assignments. Optional parameters are integrated for controlling tree growth. control=rpart.control(minsplit=1, cp=0.001) requires that the minimum number of observations in a node be 1 before attempting a split and that a split must decrease the overall lack of fit by a cost complexity factor of 0.001 before being attempted.
c.tree1 <- rpart(certified ~ forum.posts+grade+assignment, data=M1, method = "class", control=rpart.control(minsplit=1, minbucket = 1, cp=0.001))
#Check the results from the classifcation tree using the printcp() command
printcp(c.tree1)
#Plot your tree
plot(c.tree1)
summary(c.tree1)
post(c.tree1, file = "tree1.ps", title = "MOOC Certification Status") #This creates a pdf image of the tree
```
#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 "Cost Complexity" and represents the cost in 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.
```{r}
# Selected the complexity parameter associated with minimum error (xerror), and placed it into the prune( ) function. To check for this, I used the "cpvalue" code fragment.
c.tree2 <- prune(c.tree1, cp = 0.015)
cpvalue<- c.tree1$cptable[which.min(c.tree1$cptable[,"xerror"]),"CP"]
cpvalue
printcp(c.tree2)
#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
plot(c.tree2, uniform = TRUE, main = "Pruned Classification Tree for Certification Status")
post(c.tree2, file = "tree2.ps", title = "Pruned Classification Tree for Certification Status") #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?
```{r}
M2$predict1 <- predict(c.tree1, M2, type = "class")
M2$predict2 <- predict(c.tree2, M2, type = "class")
Prediction1 <- table(M2$certified, M2$predict1)
Prediction2 <- table(M2$certified, M2$predict2)
#To calculate the rate of accuracy, we take the sum of the diagnols and divide it by the sum of the table.
sum(diag(Prediction1))/sum(Prediction1)
sum(diag(Prediction2))/sum(Prediction2)
#Prediction 1 has a rate of accuracy of 21.71% and prediction 2 has a rate of accuracy of 52.73%, and thus prediction 2 has a higher rate of accuracy and a lower error rate.
```