Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions Assignment6 solution yanyi.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
---
title: "Assignment 6"
author: "Charles Lang"
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(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 = "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 = )#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)

```

##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}
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")
```




### 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.
87 changes: 87 additions & 0 deletions Assignment6 solution.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
title: "Assignment 6"
author: "Charles Lang"
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(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 = "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 = )#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)

```

##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}

```


### 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.