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

Assignment7 #129

Open
wants to merge 2 commits 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
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
54 changes: 37 additions & 17 deletions Assignment7.Rmd
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
title: "Assignment 7 - Answers"
author: "Charles Lang"
date: "11/30/2016"
output: html_document
"Assignment 7 - Answers"

---

In the following assignment you will be looking at data from an one level of an online geography tutoring system used by 5th grade students. The game involves a pre-test of geography knowledge (pre.test), a series of assignments for which you have the average score (av.assignment.score), the number of messages sent by each student to other students about the assignments (messages), the number of forum posts students posted asking questions about the assignment (forum.posts), a post test at the end of the level (post.test) and whether or not the system allowed the students to go on to the next level (level.up).
Expand All @@ -11,27 +9,36 @@ In the following assignment you will be looking at data from an one level of an

#Upload data
```{r}

D1 <- read.csv("online.data.csv")
library(ggplot2)
library(tidyr)
library(dplyr)
library(rpart)
library(rpart.plot)
```

#Visualization
```{r}
#Start by creating histograms of the distributions for all variables (#HINT: look up "facet" in the ggplot documentation)

D1$level.up <- ifelse(D1$level.up == "yes", 1,0)
#Then visualize the relationships between variables

D2 <- gather(D1, "level", "score", 2:7)
#Try to capture an intution about the data and the relationships

plot1 <- ggplot(D2, aes(score)) + facet_wrap(~level, scales = "free")
plot1 + geom_histogram(stat = "count")
pairs(D1)
```
#Classification tree
```{r}
#Create a classification tree that predicts whether a student "levels up" in the online course using three variables of your choice (As we did last time, set all controls to their minimums)

c.tree1<-rpart(level.up ~ forum.posts + pre.test.score, method="class", data=D1, control=rpart.control(minsplit=1, minbucket=1, cp=0.001))
printcp(c.tree1)
#Plot and generate a CP table for your tree

plot(c.tree1)
#Generate a probability value that represents the probability that a student levels up based your classification tree

D1$pred <- predict(rp, type = "prob")[,2]#Last class we used type = "class" which predicted the classification for us, this time we are using type = "prob" to see the probability that our classififcation is based on.

D1$pred <- predict(c.tree1, type = "prob")[,2]#Last class we used type = "class" which predicted the classification for us, this time we are using type = "prob" to see the probability that our classififcation is based on.
```
## Part II
#Now you can generate the ROC curve for your model. You will need to install the package ROCR to do this.
Expand All @@ -44,24 +51,37 @@ plot(performance(pred.detail, "tpr", "fpr"))
abline(0, 1, lty = 2)

#Calculate the Area Under the Curve
Pred2 <- pred.detail
unlist(slot(performance(Pred2,"auc"), "y.values"))#Unlist liberates the AUC value from the "performance" object created by ROCR

#Now repeat this process, but using the variables you did not use for the previous model and compare the plots & results of your two models. Which one do you think was the better model? Why?

c.tree2<-rpart(level.up~pre.test.score+post.test.score+forum.posts, method="class",data=D1)
printcp(c.tree2)
post(c.tree2, file = "dtree2.ps", title = "tree2")
rpart.plot(c.tree2, type=3, box.palette = c("green", "red"), fallen.leaves = TRUE)
D1$pred2 <- predict(c.tree2, D1, type="prob")[,2]
pred.detail2 <- prediction(D1$pred2, D1$level.up)
plot(performance(pred.detail2, "tpr", "fpr"))
abline(0, 1, lty = 2)
unlist(slot(performance(pred.detail2, "auc"), "y.values"))
```
## Part III
#Thresholds
```{r}
#Look at the ROC plot for your first model. Based on this plot choose a probability threshold that balances capturing the most correct predictions against false positives. Then generate a new variable in your data set that classifies each student according to your chosen threshold.

threshold.pred1 <-
D1$threshold.pred1 <- ifelse(D1$pred >= 0.8, "yes", "no")
D1$threshold.pred2 <- ifelse(D1$pred >= 0.95, "yes", "no")
D1$threshold.pred3 <- ifelse(D1$pred >= 0.25, "yes", "no")

#Now generate three diagnostics:

D1$accuracy.model1 <-

D1$precision.model1 <-
#Now generate three diagnostics:

D1$recall.model1 <-
accuracy.model <- mean(ifelse(D1$level.up == D1$threshold.pred3, 1, 0))
D1$accuracy.model1 <- ifelse(D1$level.up == "yes" & D1$threshold.pred3 == "yes", 1, 0)
D1$precision.model1 <- ifelse(D1$level.up == "yes" & D1$threshold.pred3 == "yes", 1, 0)
D1$recall.model1 <- ifelse(D1$level.up == "yes" & D1$threshold.pred3 == "no", 1,0)

#Finally, calculate Kappa for your model according to:

Expand Down
567 changes: 567 additions & 0 deletions Assignment7.html

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions assignment7.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
Binary file added dtree2.ps
Binary file not shown.