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..966081a 100644 --- a/Assignment7.Rmd +++ b/Assignment7.Rmd @@ -1,83 +1,93 @@ --- -title: "Assignment 7 - Answers" +title: "Assignment 7" author: "Charles Lang" -date: "11/30/2016" +date: "12/1/2016" output: html_document --- - -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). - -## Part I - -#Upload data +#Create Data ```{r} - +id <- seq(1,1000,1) +D1 <- data.frame(id) +post.test1 <- rnorm(500, 0.4,0.07) +post.test1 <- ifelse(post.test1 < 0, 0, post.test1) +post.test1 <- ifelse(post.test1 > 1, 1, post.test1) +post.test2 <- rnorm(500, 0.6,0.07) +post.test2 <- ifelse(post.test2 < 0, 0, post.test2) +post.test2 <- ifelse(post.test2 > 1, 1, post.test2) +#post.test3 <- rnorm(300, 0.75,0.07) +#post.test3 <- ifelse(post.test3 > 1, 1, post.test3) +D1$post.test.score <- round(c(post.test1,post.test2),2) +D1$pre.test.score <- round(c(post.test1,post.test2), 2) +D1$messages <- round(sample(c(150:200),1000, replace = TRUE)*D1$post.test.score) +D1$forum.posts <- round(sample(c(0:40),1000, replace = TRUE)*(D1$post.test.score) + 2) +D1$av.assignment.score <- round(D1$post.test.score*rnorm(1000, 0.5,0.1),2) +D1$level.up <- ifelse(D1$post.test.score > 0.45 & D1$av.assignment.score > 0.25, "yes", "no") ``` -#Visualization +#Visualize the distributions ```{r} -#Start by creating histograms of the distributions for all variables (#HINT: look up "facet" in the ggplot documentation) - -#Then visualize the relationships between variables - -#Try to capture an intution about the data and the relationships - +library(ggplot2) +library(dplyr) +library(tidyr) +#HINT: look up "facet" in the ggplot documentation +D2 <- select(D1, 1:7) +#Convert yes/no to 1/0 to avoid mixing variable types +D2$level.up <- ifelse(D2$level.up == "yes", 1,0) +D3 <- gather(D2, "measure", "score", 2:7) +p1 <- ggplot(D3, aes(score)) + facet_wrap(~measure, scales = "free") +p1 + geom_histogram(stat = "count") +#Visualize the relationships between variables +pairs(D2) ``` + #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) - -#Plot and generate a CP table for your tree - +#Create a classification tree that predicts whether a student "levels up" in the online course using three variables of your choice (remember to 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)) +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. -``` -## Part II +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 previous classififcation was based on. #Now you can generate the ROC curve for your model. You will need to install the package ROCR to do this. -```{r} library(ROCR) - #Plot the curve pred.detail <- prediction(D1$pred, D1$level.up) 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 - -#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? +unlist(slot(performance(pred.detail,"auc"), "y.values"))#unlist liberates 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? ``` -## 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 <- - -D1$recall.model1 <- - +accuracy.model1 <- mean(ifelse(D1$level.up == D1$threshold.pred1, 1, 0)) +D1$truepos.model1 <- ifelse(D1$level.up == "yes" & D1$threshold.pred1 == "yes", 1, 0) +D1$falsepos.model1 <- ifelse(D1$level.up == "no" & D1$threshold.pred1 == "yes", 1,0) +D1$falseneg.model1 <- ifelse(D1$level.up == "yes" & D1$threshold.pred1 == "no", 1,0) +precision.model1 <- sum(D1$truepos.model1)/(sum(D1$truepos.model1) + sum(D1$falsepos.model1)) +recall.model1 <- sum(D1$truepos.model1)/(sum(D1$truepos.model1) + sum(D1$falseneg.model1)) #Finally, calculate Kappa for your model according to: - #First generate the table of comparisons table1 <- table(D1$level.up, D1$threshold.pred1) - -#Convert to matrix -matrix1 <- as.matrix(table1) - -#Calculate kappa -kappa(matrix1, exact = TRUE)/kappa(matrix1) - +table1 +#Calculate kappa manually +po <- (586+247)/(586+247+14+153) +pe <- ((586 + 14)/(586+247+14+153))*((586 + 153)/(586+247+14+153)) + ((14 + 247)/(586+247+14+153))*((153+247)/(586+247+14+153)) +kappa <- (po - pe)/(1 - pe) +#Calculate OOB +library(psych) #You could also use the "irr" or "vcd" library versions +cohen.kappa(table1) #Now choose a different threshold value and repeat these diagnostics. What conclusions can you draw about your two thresholds? - -``` - -### 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. +#Alternate kappa value +library(irr) +kappa2(D1[,c(7,9)], "unweighted") +kappa2(D1[,c(7,10)], "unweighted") +kappa2(D1[,c(7,11)], "unweighted") +``` \ No newline at end of file diff --git a/Assignment7_files/figure-html/unnamed-chunk-2-1.png b/Assignment7_files/figure-html/unnamed-chunk-2-1.png new file mode 100644 index 0000000..dcb6dba Binary files /dev/null and b/Assignment7_files/figure-html/unnamed-chunk-2-1.png differ diff --git a/Assignment7_files/figure-html/unnamed-chunk-2-2.png b/Assignment7_files/figure-html/unnamed-chunk-2-2.png new file mode 100644 index 0000000..519587b Binary files /dev/null and b/Assignment7_files/figure-html/unnamed-chunk-2-2.png differ 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