forked from core-methods-in-edm/assignment6
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment6.Rmd
More file actions
140 lines (85 loc) · 5.56 KB
/
Assignment6.Rmd
File metadata and controls
140 lines (85 loc) · 5.56 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
---
title: "Assignment 6"
author: "Yi Yang"
date: "11/16/2016"
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
##Part I
#Packages
```{r}
library(rpart)
```
#Data
```{r}
#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
```{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 <- 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
post(c.tree1, file = "tree1.ps", title = "MOOC") #This creates a pdf image of the tree
```
### I chose the grade variable and the assignment variable. According to the plot, if a student's average grade for the course is less than 12.5, or the average grade for the course assignments is less than 7.5, he or she is more likely to be uncertified (unpaid for the course).
##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.
```{r}
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?
```{r}
M2$predict1 <- predict(c.tree1, M2, type = "class")
M2$predict2 <- predict(c.tree2, M2, type = "class")
table(M2$certified, M2$predict1)
table(M2$certified, M2$predict2)
# error rate
mean(M2$certified!=M2$predict1)
mean(M2$certified!=M2$predict2)
```
### According to the table, the pruned tree does a better job in making predictions about the the students in the second data set than the original tree. The pruned tree has a lower error rate (0.4637). However, both of the trees have high error rates thus the model is not ideal for our data. Thus we might need to modify the model by including other variables.
##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?
```{r}
record <- read.csv("student.record.csv", header = TRUE)
## Model 1 (raw variable: SAT total and high school GPA)
D1 <- dplyr::select(record, LAST_SATI_TOTAL_SCORE, HSGPA, SEX)
D1[D1 == 0.00] <- NA
D1 <- na.omit(D1)
D1sample <- dplyr::sample_n(D1, 5000, replace = TRUE)
c.treeM1 <- rpart(SEX ~ HSGPA + LAST_SATI_TOTAL_SCORE, method="class", data=D1sample)
printcp(c.treeM1)
post(c.treeM1, file = "treeM1.ps", title = "Model 1")
D1$predict <- predict(c.treeM1, D1, type = "class")
table(D1$SEX, D1$predict)
## Model 2 (extract variable: ACT total)
D2 <- dplyr::mutate(record, LAST_ACT_TOTAL_SCORE = LAST_ACT_ENGL_SCORE + LAST_ACT_MATH_SCORE + LAST_ACT_READ_SCORE + LAST_ACT_SCIRE_SCORE + LAST_ACT_COMP_SCORE)
D2 <- dplyr::select(D2, HSGPA, SEX, LAST_ACT_TOTAL_SCORE)
D2[D2 == 0.00] <- NA
D2 <- na.omit(D2)
D2sample <- dplyr::sample_n(D2, 5000, replace = TRUE)
c.treeM2 <- rpart(SEX ~ HSGPA + LAST_ACT_TOTAL_SCORE, method="class", data=D2sample)
printcp(c.treeM2)
post(c.treeM2, file = "treeM2.ps", title = "Model 2")
D2$predict <- predict(c.treeM2, D2, type = "class")
table(D2$SEX, D2$predict)
# error rate
mean(D1$SEX!=D1$predict)
mean(D2$SEX!=D2$predict)
```
### The first model uses raw variables (SAT total score and high school GPA) to predict the outcome variable (gender), while the second model features an extract variable from the data (ACT total score) instead of SAT.
### Model 1 has a slightly lower error rate comparing to Model 2, which indicates that SAT total score has higher accuracy of predicting the student's gender than ACT total score. However, both of the trees have high error rates thus the model is not ideal for our data. Thus we might need to modify the model by taking other variables into consideration.
### 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.