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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
101 changes: 93 additions & 8 deletions Assignment6.Rmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
---
title: "Assignment 6"
author: "Charles Lang"
<<<<<<< HEAD
author: "Charles Lang, Edited by Hsiao Yang"
=======
author: "Charles Lang" Edited by Hsiao Yang
>>>>>>> 78a66c218b82b823759c202e5ed3dcd0a4bfb079
date: "11/16/2016"
output: html_document
---
Expand All @@ -18,32 +22,44 @@ assignment (numeric) - A student's average grade for the course assignments
#Packages
```{r}
library(rpart)
<<<<<<< HEAD
# install.packages("
library(tidyverse)
=======
install.packages("rpart.plot")
>>>>>>> 78a66c218b82b823759c202e5ed3dcd0a4bfb079
library(rpart.plot)
```

#Data
```{r}
#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(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
rpart.plot(c.tree1)
post(c.tree1, file = "tree1.ps", title = "MOOC")

#This creates a pdf image of the tree

```
##Comment
Variables that we will include is grade and assignment so we can prune the tree. If we include forum posts, there is only two nodes so then we will not be able to prune the tree for part II of this assignment.

##Part II

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

printcp(c.tree2)
rpart.plot(c.tree2)

post(c.tree2, file = "tree2.ps", title = "MOOC") #This creates a pdf image of the tree
```

Expand All @@ -66,19 +85,85 @@ M2$predict1 <- predict(c.tree1, M2, type = "class")

M2$predict2 <- predict(c.tree2, M2, type = "class")

table(M2$certified, M2$predict1)
T1 <- table(M2$certified, M2$predict1)

T2 <- table(M2$certified, M2$predict2)
<<<<<<< HEAD

#Tree 1 True Results
T1TR <- (T1[2,2]+T1[1,1])/sum(T1)

=======

table(M2$certified, M2$predict2)
#Tree 1 True Results
T1TR <- (T1[2,2]+T1[1,1])/sum(T1)

>>>>>>> 78a66c218b82b823759c202e5ed3dcd0a4bfb079
#Tree 2 True Results
T2TR <- (T2[2,2]+T2[1,1])/sum(T2)

```
#Comment
The second tree has a lower error rate. This is because the True Negative and True Positive for tree 2 is 53.63% while tree 1 is 21.86% so that means the second tree has a higher accuracy vesus the first 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}

<<<<<<< HEAD
R.pre <- read.csv("student.record.csv", header = TRUE)
R <- R.pre %>%
filter(HSGPA <= 4)

c.tree3 <- rpart(HSGPA ~ ., data = R)

printcp(c.tree3)

R$predict1 <- predict(c.tree3, R)

T1 <- R %>%
select(HSGPA, predict1) %>%
mutate(
residual = HSGPA - predict1,
squared.residual = residual^2
)

sqrt(mean(T1$squared.residual, na.rm = T))

c.tree3$variable.importance
R.select <- R %>%
select(HSGPA, ADMIT_TERM, MAJOR1_DESCR, MAJOR1_DEPT)

c.tree4 <- rpart(HSGPA ~ ., data = R.select)

R.select$predict1 <- predict(c.tree4, R.select)

T2 <- R.select %>%
select(HSGPA, predict1) %>%
mutate(
residual = HSGPA - predict1,
squared.residual = residual^2
)

sqrt(mean(T2$squared.residual, na.rm = T))


=======
R <- read.csv("student.record.csv", header = TRUE)

c.tree3 <- rpart(as.factor(HSGPA) ~., method = "class", data = R)

printcp(c.tree3)

>>>>>>> 78a66c218b82b823759c202e5ed3dcd0a4bfb079
```
##Comment
First model prediction = 1.322
Second model prediction = 1.296

The second model that selected Admit_Term, Major1_Descr, and Major1_Dept has a better prediction since the prediction is lower than the first model, which is closer to 0.


### To Submit Your Assignment
Expand Down
579 changes: 579 additions & 0 deletions Assignment6.html

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions assignment6.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX
Loading