diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b6a065 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.Rproj.user +.Rhistory +.RData +.Ruserdata diff --git a/Assignment7.Rmd b/Assignment7.Rmd index 105cbdf..18bc3bb 100644 --- a/Assignment7.Rmd +++ b/Assignment7.Rmd @@ -11,15 +11,24 @@ 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", head = TRUE) ``` #Visualization ```{r} +library(ggplot2) +library(dplyr) +library(tidyr) #Start by creating histograms of the distributions for all variables (#HINT: look up "facet" in the ggplot documentation) +D2 <- select(D1, 1:7) +D2$level.up <- ifelse(D2$level.up == "yes", 1,0) +D3 <- gather(D2, "measure", "score", 2:7) -#Then visualize the relationships between variables +p1 <- ggplot(D3, aes(score)) + facet_wrap(~measure, scales = "free") +p1 + geom_histogram(stat = "count") +#Then visualize the relationships between variables +pairs(D2) #Try to capture an intution about the data and the relationships ``` @@ -27,11 +36,18 @@ In the following assignment you will be looking at data from an one level of an ```{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) +library(rpart) +c.tree1 <- rpart(level.up ~ forum.posts + pre.test.score, method = "class", data = D1, control=rpart.control(minsplit=1, minbucket=1, cp=0.001)) + #Plot and generate a CP table for your tree +printcp(c.tree1) +plot(c.tree1) +text(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. @@ -44,7 +60,7 @@ plot(performance(pred.detail, "tpr", "fpr")) abline(0, 1, lty = 2) #Calculate the Area Under the Curve -unlist(slot(performance(Pred2,"auc"), "y.values"))#Unlist liberates the AUC value from the "performance" object created by ROCR +unlist(slot(performance(pred.detail,"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? ``` @@ -53,15 +69,15 @@ unlist(slot(performance(Pred2,"auc"), "y.values"))#Unlist liberates the AUC valu ```{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 <- +threshold.pred1 <- ifelse(D1$pred >= 0.8, "yes", "no") #Now generate three diagnostics: -D1$accuracy.model1 <- +D1$accuracy.model1 <-mean(ifelse(D1$level.up == D1$threshold.pred1, 1, 0)) -D1$precision.model1 <- +D1$precision.model1 <- sum(D1$truepos.model1)/(sum(D1$truepos.model1) + sum(D1$falsepos.model1)) -D1$recall.model1 <- +D1$recall.model1 <- sum(D1$truepos.model1)/(sum(D1$truepos.model1) + sum(D1$falseneg.model1)) #Finally, calculate Kappa for your model according to: diff --git a/assignment7.Rproj b/assignment7.Rproj new file mode 100644 index 0000000..8e3c2eb --- /dev/null +++ b/assignment7.Rproj @@ -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