Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment 6 #145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
64 changes: 59 additions & 5 deletions Assignment6.Rmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "Assignment 6"
author: "Charles Lang"
author: "Yi Yang"
date: "11/16/2016"
output: html_document
---
Expand All @@ -25,18 +25,18 @@ library(rpart)
#Upload the data sets MOOC1.csv and MOOC2.csv
M1 <- read.csv("MOOC1.csv", header = TRUE)

M2 <-
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 <-
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
Expand All @@ -45,14 +45,17 @@ post(c.tree1, file = "tree1.ps", title = "MOOC") #This creates a pdf image of th

```

### 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 = )#Set cp to the level at which you want the tree to end
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

Expand All @@ -70,16 +73,67 @@ 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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job! What is the error rate of the original tree?


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the error rates of the models?


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! Keep up the good work.

### To Submit Your Assignment

Expand Down
348 changes: 348 additions & 0 deletions Assignment6.html

Large diffs are not rendered by default.

Binary file added tree1.ps
Binary file not shown.
Binary file added tree2.ps
Binary file not shown.
Binary file added treeM1.ps
Binary file not shown.
Binary file added treeM2.ps
Binary file not shown.