forked from core-methods-in-edm/assignment6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment6.Rmd
110 lines (96 loc) · 4.93 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
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
---
title: "Assignment 6 - answers"
author: "Charles Lang"
date: "11/16/2018"
output: html_document
---
#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
#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 data set.
c.tree1 <- rpart(as.factor(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 = "tree.ps", title = "MOOC")#Gnerates a pdf image
```
#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 Complxity" 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 are better. 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.
```{r}
c.tree2 <- prune(c.tree1, cp = 0.058182)
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)
```
#Data
```{r}
#Go to https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/26147 and download the data file HMXPC13_DI_v2_5-14-14.csv. This is a large fileso you may want to close other applications to reserve processing power.
#Upload
D1 <- read.csv("HMXPC13_DI_v2_5-14-14.csv", header = TRUE)
#Separate the course_id variable into school, scoure and semester variables using tidyr
D2 <- separate(D1, course_id, c("school", "course", "semester"), sep = "/")
#Create a data frame that only contains the Fall 2012 data
D3 <- filter(D2, semester == "2012_Fall")
D4$humanities <- ifelse(D4$course == "14.73x" | D4$course == "CB22x" | D4$course == "ER22x", 1,0)
D3 <- filter(D3, course != "PH207x")
D3$region <- countrycode(D3$final_cc_cname_DI, "country.name", "region", warn = FALSE)
D4$region <- countrycode(D4$final_cc_cname_DI, "country.name", "region", warn = FALSE)
D3$region <- ifelse(is.na(D3$region), "other", D3$region)
D3 <- na.omit(D3)
D4$region <- ifelse(is.na(D4$region), "other", D3$region)
D3$n.america <- ifelse(D3$region == "Northern America", 1, 0)
D2 <- subset(D1, D1$certified == 1)
D3 <- subset(D1, !is.na(D1$YoB))
D3b <- select(D3, course, region, nevents)
D3b <- na.omit(D3b)
```
```{r}
c.tree <- rpart(n.america ~ course + gender + grade, method="class", data=D3)
edx.tree <- rpart(course ~ region, nevents, method="class", data=D3b)
c.tree <- rpart(as.factor(course) ~ ndays_act + nplay_video, method="class", data=D3)
c.tree <- rpart(region ~ as.factor(course) + gender + ndays_act, method="class", data=D3)
c.tree <- rpart(as.factor(certified) ~ final_cc_cname_DI + course_id + gender, method="class", data=D1)
#Look at the error of this tree
printcp(edx.tree)
post(c.tree, file = "tree.ps", title = "EdX")
plot(edx.tree)
text(edx.tree)
#Plot the tree
post(edx.tree, file = "tree2.ps", title = "Session Completion Action: 1 - Ask teacher, 2 - Start new session, 3 - Give up")
```
```{r}
certified <- sample(D6$Kyphosis, 1000, replace = TRUE)
forum.posts <- sample(D6$Age, 1000, replace = TRUE)
grade <- sample(D6$Number, 1000, replace = TRUE)
assignment <- sample(D6$Start, 1000, replace = TRUE)
D7 <- data.frame(certified, forum.posts, grade, assignment)
D7$certified <- ifelse(D7$certified == "absent", "yes", "no")
c.tree <- rpart(certified ~ forum.posts, grade, assignment, method="class", data=D7)
certified <- sample(D6$Kyphosis, 10000, replace = TRUE)
forum.posts <- sample(D6$Age, 10000, replace = TRUE)
grade <- sample(D6$Number, 10000, replace = TRUE)
assignment <- sample(D6$Start, 10000, replace = TRUE)
D8 <- data.frame(certified, forum.posts, grade, assignment)
D8$certified <- ifelse(D8$certified == "absent", "yes", "no")
D8$predict <- predict(c.tree, type = "class")
```