From 9d16b1b9a46d26bab77926b8f654970222e50585 Mon Sep 17 00:00:00 2001 From: jd3603 Date: Tue, 26 Nov 2019 18:00:55 -0500 Subject: [PATCH] mtest --- .gitignore | 4 ++ Assignment6.Rmd | 106 ++++++++++++++++++++++++++++------------------ assignment6.Rproj | 13 ++++++ tree.ps | Bin 0 -> 5823 bytes tree2.ps | Bin 0 -> 4675 bytes 5 files changed, 82 insertions(+), 41 deletions(-) create mode 100644 .gitignore create mode 100644 assignment6.Rproj create mode 100644 tree.ps create mode 100644 tree2.ps 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/Assignment6.Rmd b/Assignment6.Rmd index 8e65135..959842b 100644 --- a/Assignment6.Rmd +++ b/Assignment6.Rmd @@ -1,19 +1,17 @@ --- -title: "Assignment 6" +title: "Assignment 6 - answers" author: "Charles Lang" -date: "11/16/2016" +date: "11/16/2018" output: html_document --- -#Addignment 6 +#Assignment 6 In this assignment you will be looking at data from a MOOC. It contains the following per-student variables: -certified (yes/no) - Whether or not a student paid for the course -forum.posts (numeric) - How many forum posts a student made throughout the course -grade (numeric) - A student's average grade for the course exam -assignment (numeric) - A student's average grade for the course assignments - -##Part I +certified (yes/no) - Whether or not a student paid for the course +forum.posts (numeric) - How many forum posts a student made throughout the course +grade (numeric) - A student's average grade for the course exam +assignment (numeric) - A student's average grade for the course assignments #Packages ```{r} @@ -24,38 +22,25 @@ library(rpart) ```{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 <- - +#Using the rpart package generate a classification tree predicting certified from the other variables in the data set. +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 - +post(c.tree1, file = "tree.ps", title = "MOOC")#Gnerates a pdf image ``` -##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". +#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 "Cost Complxity" and represents the cost in error for adding a node to the tree. Notice it decreases as we add more nodes to the tree which implies that more nodes are better. 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. +#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. ```{r} -c.tree2 <- prune(c.tree1, cp = )#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 - +c.tree2 <- prune(c.tree1, cp = 0.058182) post(c.tree2, file = "tree2.ps", title = "MOOC") #This creates a pdf image of the tree ``` @@ -63,24 +48,63 @@ post(c.tree2, file = "tree2.ps", title = "MOOC") #This creates a pdf image of th ```{r} M2$predict1 <- predict(c.tree1, M2, type = "class") - M2$predict2 <- predict(c.tree2, M2, type = "class") - table(M2$certified, M2$predict1) - table(M2$certified, M2$predict2) - ``` -##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? +#Data ```{r} - +#Go to https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/26147 and download the data file HMXPC13_DI_v2_5-14-14.csv. This is a large fileso you may want to close other applications to reserve processing power. +#Upload +D1 <- read.csv("HMXPC13_DI_v2_5-14-14.csv", header = TRUE) +#Separate the course_id variable into school, scoure and semester variables using tidyr +D2 <- separate(D1, course_id, c("school", "course", "semester"), sep = "/") +#Create a data frame that only contains the Fall 2012 data +D3 <- filter(D2, semester == "2012_Fall") +D4$humanities <- ifelse(D4$course == "14.73x" | D4$course == "CB22x" | D4$course == "ER22x", 1,0) +D3 <- filter(D3, course != "PH207x") +D3$region <- countrycode(D3$final_cc_cname_DI, "country.name", "region", warn = FALSE) +D4$region <- countrycode(D4$final_cc_cname_DI, "country.name", "region", warn = FALSE) +D3$region <- ifelse(is.na(D3$region), "other", D3$region) +D3 <- na.omit(D3) +D4$region <- ifelse(is.na(D4$region), "other", D3$region) +D3$n.america <- ifelse(D3$region == "Northern America", 1, 0) +D2 <- subset(D1, D1$certified == 1) +D3 <- subset(D1, !is.na(D1$YoB)) +D3b <- select(D3, course, region, nevents) +D3b <- na.omit(D3b) ``` +```{r} +c.tree <- rpart(n.america ~ course + gender + grade, method="class", data=D3) +edx.tree <- rpart(course ~ region, nevents, method="class", data=D3b) +c.tree <- rpart(as.factor(course) ~ ndays_act + nplay_video, method="class", data=D3) +c.tree <- rpart(region ~ as.factor(course) + gender + ndays_act, method="class", data=D3) +c.tree <- rpart(as.factor(certified) ~ final_cc_cname_DI + course_id + gender, method="class", data=D1) +#Look at the error of this tree +printcp(edx.tree) +post(c.tree, file = "tree.ps", title = "EdX") +plot(edx.tree) +text(edx.tree) +#Plot the tree +post(edx.tree, file = "tree2.ps", title = "Session Completion Action: 1 - Ask teacher, 2 - Start new session, 3 - Give up") +``` -### 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. \ No newline at end of file +```{r} +certified <- sample(D6$Kyphosis, 1000, replace = TRUE) +forum.posts <- sample(D6$Age, 1000, replace = TRUE) +grade <- sample(D6$Number, 1000, replace = TRUE) +assignment <- sample(D6$Start, 1000, replace = TRUE) +D7 <- data.frame(certified, forum.posts, grade, assignment) +D7$certified <- ifelse(D7$certified == "absent", "yes", "no") +c.tree <- rpart(certified ~ forum.posts, grade, assignment, method="class", data=D7) +certified <- sample(D6$Kyphosis, 10000, replace = TRUE) +forum.posts <- sample(D6$Age, 10000, replace = TRUE) +grade <- sample(D6$Number, 10000, replace = TRUE) +assignment <- sample(D6$Start, 10000, replace = TRUE) +D8 <- data.frame(certified, forum.posts, grade, assignment) +D8$certified <- ifelse(D8$certified == "absent", "yes", "no") +D8$predict <- predict(c.tree, type = "class") +``` \ No newline at end of file diff --git a/assignment6.Rproj b/assignment6.Rproj new file mode 100644 index 0000000..8e3c2eb --- /dev/null +++ b/assignment6.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 diff --git a/tree.ps b/tree.ps new file mode 100644 index 0000000000000000000000000000000000000000..e71d1b571d4a6f2dfe65bb8b2415f47d22392fdd GIT binary patch literal 5823 zcmd5=ZEu{$7XHq!IQ?L0qiScF0T!^*NVSvnMoQ`^snicqKCm!e)5`)E*u2Qs|Gm!{ z7#7xRH?34Dtz$Sd=ggevyyLSU-o9V{oK+jW^n~l2ojtG8{Z5zNuUcn1d#BrK-=w-d zr|YWh=s&vnq`N##!1?3R((-{fgaSNpEsci6gYbkbD~Z@#atyU$6(I=oG`3}=ya zy3F{5t0o8Dq|2-FoL(no)}~3#oEO!;%<^)3QGGcl8SzgLh7>6un=i`jvf8nwT4vST zynNeKMYVNSo0{mBwk@^qez{+)Ks`pGt(Vq`r0|84Ie?GZbdzu zgH3Zz@=1ehlb4xcU2Pk)-sl$S=3C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