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
120 lines (81 loc) · 3.85 KB
/
Assignment6.Rmd
File metadata and controls
120 lines (81 loc) · 3.85 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
---
title: "Assignment 6"
author: "Yigao Liu"
date: "11/16/2016"
output:
html_document: default
pdf_document: default
---
#Assignment 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)
library(rpart.plot)
```
#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?
#I choose to use certified variable in data M1
c.tree1 <- rpart(certified~grade+assignment, data = M1)
#Check the results from the classifcation tree using the printcp() command
printcp(c.tree1)
c.tree1
#Plot your tree
library(rpart.plot)
rpart.plot(c.tree1)
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.
```{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
rpart.plot(c.tree2)
```
#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?
###Answer: Second tree has a better accuracy.
Table 2
```{r}
M2$predict1 <- predict(c.tree1, M2, type = "class")
M2$predict2 <- predict(c.tree2, M2, type = "class")
T1<-table(M2$certified, M2$predict1)
T2<-table(M2$certified, M2$predict2)
E1<-(T1[2,2] + T1[1,1])/sum (T1)
E2<-(T2[2,2] + T2[1,1])/sum (T2)
```
##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?
###I choose to use the model2, becuase the rel error is smaller.
```{r}
S2 <- read.csv("student.course.csv", header = TRUE)
#model1
c.tree3 <- rpart(GPAO ~ GRD_PTS_PER_UNIT+TERM, data = S2)
printcp(c.tree3)
```
```{r}
#model2
library(dplyr)
D2 <- S2 %>% select(-SUBJECT, -DIV)
D2 <- scale(D2, center = TRUE)
pca <- prcomp(D2, scale = TRUE)
D3 <-data.frame(pca$x)
D4 <- bind_cols(S2, D3)
c.tree4 <- rpart(GPAO ~ PC1+PC2, data = D4)
printcp(c.tree4)
```
### 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.