forked from la-process-and-theory/class-activity4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment6.Rmd
More file actions
executable file
·77 lines (56 loc) · 3.08 KB
/
Assignment6.Rmd
File metadata and controls
executable file
·77 lines (56 loc) · 3.08 KB
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
---
title: "Assignment 6"
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.table("MOOC1.csv", sep = ",", header = TRUE)
M2 <- read.table("MOOC2.csv", sep = ",", header = TRUE)
```
#Decision tree
```{r}
#Using the rpart package generate a classification tree predicting certified from the other variables in the M1 data frame.
#"control=rpart.control(minsplit = 1,minbucket = 1,cp=0.001" is just making the algorithm less conservative.
c.tree1 <- rpart(certified ~ forum.posts + grade+assignment, method="class", data=M1, control=rpart.control(minsplit = 1,minbucket = 1,cp=0.001))
post(c.tree1, file = "tree1.ps", title = "student performance: 1 - yes, 2 - no, 3 - ?")
#Check the results from the classifcation tree using the printcp() command
printcp(c.tree1)
#cp stands for complixity cost , it is the cost to the split. So we can find the cp values in by printcp command and if we want it less complicated we should choose a cp bigger than 0.001
##mthd 1: c.tree2 <- rpart(certified ~ forum.posts + grade+assignment, method="class", data=M1, control=rpart.control(minsplit = 1,minbucket = 1,cp=0.0032680 ))
```
#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}
##mthd2:
c.tree2 <- prune(c.tree1, cp = 0.0039216)
#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?
```{r}
M2$predict1 <- predict(c.tree1, M2, type = "class")
M2$predict2 <- predict(c.tree2, M2, type = "class")
table(M2$certified, M2$predict1)
# no yes
# no 432 1648
# yes 1680 6240
table(M2$certified, M2$predict2)
# no yes
# no 64 2016
# yes 239 7681
errorrate1=(1680+1648)/(1680+1648+432+6240)
#0.3328
errorrate2=(2016+239)/(2016+239+64+7681)
#0.2255
```