From 17793338e497259a4384c6913461be77dc7f7261 Mon Sep 17 00:00:00 2001 From: Xtine Odie <54963224+xtineodie@users.noreply.github.com> Date: Tue, 3 Dec 2019 13:44:27 -0500 Subject: [PATCH] assignment 6 --- Assignment6.Rmd | 54 ++++- Assignment6.html | 570 +++++++++++++++++++++++++++++++++++++++++++++++ tree4.ps | Bin 0 -> 5956 bytes 3 files changed, 619 insertions(+), 5 deletions(-) create mode 100644 Assignment6.html create mode 100644 tree4.ps diff --git a/Assignment6.Rmd b/Assignment6.Rmd index 8e65135..31eab07 100644 --- a/Assignment6.Rmd +++ b/Assignment6.Rmd @@ -25,19 +25,18 @@ library(rpart) #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(certified ~ grade + assignment, method = "class", data = M1) #Check the results from the classifcation tree using the printcp() command - +printcp(c.tree1) #Plot your tree @@ -52,10 +51,12 @@ 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) + post(c.tree2, file = "tree2.ps", title = "MOOC") #This creates a pdf image of the tree ``` @@ -72,12 +73,55 @@ table(M2$certified, M2$predict2) ``` +I feel like the 2nd tree is better, because despite the 1st tree's accuracy in prediciting those who are not certified, it made a lot of false positives, which is an issue. + ##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} +library(dplyr) +library(tidyr) +library(stringr) +library(rpart) + +M3 <- read.csv("student.record.csv", header = TRUE) +names(M3) <- c("MAJOR_3", "MAJOR_2", "MAJOR_1", "HSGPA", "ACT_ENGL", "ACT_MATH", "ACT_READ", "ACT_SCIRE", "ACT_COMP", "SAT_VERB", "SAT_MATH", "SAT_COMP", "SEX", "STDNT_GROUP1", "STDNT_GROUP2", "MAJOR1_DEPT", "MAJOR2_DEPT", "MAJOR3_DEPT", "ANONID", "ADMIT_TERM", "MAJOR1_TERM", "MAJOR2_TERM", "MAJOR3_TERM") + +#find students with BA in DEGREE_1 title & label accordingly +majors2 <- select(M3, 3:13, 16, 19) + +majors_ART <- majors2 + +majors_ART$MAJOR_1 <- as.character(majors_ART$MAJOR_1) + +majors_ART <- majors_ART %>% + select(MAJOR_1:SEX, ANONID) %>% + filter_all(any_vars(str_detect(MAJOR_1, pattern = "BA"))) + +majors_ART$DEG_TYPE <- "art" + +#find students with BS in DEGREE_1 title & label accordingly +majors_SCIENCE <- majors2 +majors_SCIENCE$MAJOR_1 <- as.character(majors_SCIENCE$MAJOR_1) + +majors_SCIENCE <- majors_SCIENCE %>% + select(MAJOR_1:SEX, ANONID) %>% + filter_all(any_vars(str_detect(MAJOR_1, pattern = "BS"))) + +majors_SCIENCE$DEG_TYPE <- "science" + +#Bind BA and BS students together +majors_ALL <- bind_rows(majors_ART, majors_SCIENCE) +majors_ALL_math <- majors_ALL %>% + select(ANONID, MAJOR_1, DEG_TYPE, ACT_MATH, SAT_MATH, HSGPA) + +#Do math scores predict BA or BS? spoiler alert, yes. +c.tree4 <- rpart(as.factor(DEG_TYPE) ~ ACT_MATH + SAT_MATH, method = "class", data = majors_ALL_math) + +printcp(c.tree4) +post(c.tree4, file = "tree4.ps", title = "degree type") #This creates a pdf image of the tree ``` diff --git a/Assignment6.html b/Assignment6.html new file mode 100644 index 0000000..1621562 --- /dev/null +++ b/Assignment6.html @@ -0,0 +1,570 @@ + + + + +
+ + + + + + + + + + + +#Addignment 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
+#Packages
+library(rpart)
+#Data
+#Upload the data sets MOOC1.csv and MOOC2.csv
+M1 <- read.csv("MOOC1.csv", header = TRUE)
+
+M2 <- read.csv("MOOC2.csv", header = TRUE)
+#Decision tree
+#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 <- rpart(certified ~ grade + assignment, method = "class", data = M1)
+
+#Check the results from the classifcation tree using the printcp() command
+
+printcp(c.tree1)
+##
+## Classification tree:
+## rpart(formula = certified ~ grade + assignment, data = M1, method = "class")
+##
+## Variables actually used in tree construction:
+## [1] assignment grade
+##
+## Root node error: 275/1000 = 0.275
+##
+## n= 1000
+##
+## CP nsplit rel error xerror xstd
+## 1 0.923636 0 1.000000 1.000000 0.0513455
+## 2 0.058182 1 0.076364 0.076364 0.0164880
+## 3 0.010000 2 0.018182 0.018182 0.0081108
+#Plot your tree
+
+post(c.tree1, file = "tree1.ps", title = "MOOC") #This creates a pdf image of the tree
+##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”.
+#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.
+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)
+##
+## Classification tree:
+## rpart(formula = certified ~ grade + assignment, data = M1, method = "class")
+##
+## Variables actually used in tree construction:
+## [1] assignment
+##
+## Root node error: 275/1000 = 0.275
+##
+## n= 1000
+##
+## CP nsplit rel error xerror xstd
+## 1 0.923636 0 1.000000 1.000000 0.051346
+## 2 0.058182 1 0.076364 0.076364 0.016488
+post(c.tree2, file = "tree2.ps", title = "MOOC") #This creates a pdf image of the tree
+#Now use both the original tree and the pruned tree to make predictions about the the students in the second data set. Which tree has a lower error rate?
+M2$predict1 <- predict(c.tree1, M2, type = "class")
+
+M2$predict2 <- predict(c.tree2, M2, type = "class")
+
+table(M2$certified, M2$predict1)
+##
+## no yes
+## no 2056 24
+## yes 7790 130
+table(M2$certified, M2$predict2)
+##
+## no yes
+## no 896 1184
+## yes 3453 4467
+I feel like the 2nd tree is better, because despite the 1st tree’s accuracy in prediciting those who are not certified, it made a lot of false positives, which is an issue.
+##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?
+library(dplyr)
+##
+## Attaching package: 'dplyr'
+## The following objects are masked from 'package:stats':
+##
+## filter, lag
+## The following objects are masked from 'package:base':
+##
+## intersect, setdiff, setequal, union
+library(tidyr)
+library(stringr)
+library(rpart)
+
+M3 <- read.csv("student.record.csv", header = TRUE)
+names(M3) <- c("MAJOR_3", "MAJOR_2", "MAJOR_1", "HSGPA", "ACT_ENGL", "ACT_MATH", "ACT_READ", "ACT_SCIRE", "ACT_COMP", "SAT_VERB", "SAT_MATH", "SAT_COMP", "SEX", "STDNT_GROUP1", "STDNT_GROUP2", "MAJOR1_DEPT", "MAJOR2_DEPT", "MAJOR3_DEPT", "ANONID", "ADMIT_TERM", "MAJOR1_TERM", "MAJOR2_TERM", "MAJOR3_TERM")
+
+#find students with BA in DEGREE_1 title & label accordingly
+majors2 <- select(M3, 3:13, 16, 19)
+
+majors_ART <- majors2
+
+majors_ART$MAJOR_1 <- as.character(majors_ART$MAJOR_1)
+
+majors_ART <- majors_ART %>%
+ select(MAJOR_1:SEX, ANONID) %>%
+ filter_all(any_vars(str_detect(MAJOR_1, pattern = "BA")))
+
+majors_ART$DEG_TYPE <- "art"
+
+#find students with BS in DEGREE_1 title & label accordingly
+majors_SCIENCE <- majors2
+majors_SCIENCE$MAJOR_1 <- as.character(majors_SCIENCE$MAJOR_1)
+
+majors_SCIENCE <- majors_SCIENCE %>%
+ select(MAJOR_1:SEX, ANONID) %>%
+ filter_all(any_vars(str_detect(MAJOR_1, pattern = "BS")))
+
+majors_SCIENCE$DEG_TYPE <- "science"
+
+#Bind BA and BS students together
+majors_ALL <- bind_rows(majors_ART, majors_SCIENCE)
+majors_ALL_math <- majors_ALL %>%
+ select(ANONID, MAJOR_1, DEG_TYPE, ACT_MATH, SAT_MATH, HSGPA)
+
+#Do math scores predict BA or BS? spoiler alert, yes.
+c.tree4 <- rpart(as.factor(DEG_TYPE) ~ ACT_MATH + SAT_MATH, method = "class", data = majors_ALL_math)
+
+printcp(c.tree4)
+##
+## Classification tree:
+## rpart(formula = as.factor(DEG_TYPE) ~ ACT_MATH + SAT_MATH, data = majors_ALL_math,
+## method = "class")
+##
+## Variables actually used in tree construction:
+## [1] ACT_MATH SAT_MATH
+##
+## Root node error: 25978/53008 = 0.49008
+##
+## n=53008 (41980 observations deleted due to missingness)
+##
+## CP nsplit rel error xerror xstd
+## 1 0.154477 0 1.00000 1.00000 0.0044305
+## 2 0.061937 1 0.84552 0.84552 0.0043659
+## 3 0.010000 2 0.78359 0.78659 0.0043136
+post(c.tree4, file = "tree4.ps", title = "degree type") #This creates a pdf image of the tree
+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.
+