From 4a0674a182172611bd60b982a818fb54d17b10d0 Mon Sep 17 00:00:00 2001 From: Yanyi Chen Date: Tue, 3 Dec 2019 21:14:24 -0500 Subject: [PATCH 1/2] update --- Assignment6 solution.Rmd | 87 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 Assignment6 solution.Rmd diff --git a/Assignment6 solution.Rmd b/Assignment6 solution.Rmd new file mode 100644 index 0000000..c5f3aac --- /dev/null +++ b/Assignment6 solution.Rmd @@ -0,0 +1,87 @@ +--- +title: "Assignment 6" +author: "Charles Lang" +date: "11/16/2016" +output: html_document +--- +#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 +```{r} +library(rpart) +``` + +#Data +```{r} +#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 +```{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 <- 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 + +``` + +##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. + +```{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 + +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? + +```{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? + +```{r} + +``` + + +### 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 From a7687d94518bbba67c4d3c6ab69de335441ec9f5 Mon Sep 17 00:00:00 2001 From: Yanyi Chen Date: Fri, 6 Dec 2019 15:27:33 -0500 Subject: [PATCH 2/2] aaa --- Assignment6 solution yanyi.Rmd | 136 +++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 Assignment6 solution yanyi.Rmd diff --git a/Assignment6 solution yanyi.Rmd b/Assignment6 solution yanyi.Rmd new file mode 100644 index 0000000..bf198cd --- /dev/null +++ b/Assignment6 solution yanyi.Rmd @@ -0,0 +1,136 @@ +--- +title: "Assignment 6" +author: "Charles Lang" +date: "11/16/2016" +output: html_document +--- +#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 +```{r} +library(rpart) +``` + +#Data +```{r} +#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 +```{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 <- 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 + +``` + +##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. + +```{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 + +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? + +```{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? + +```{r} +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") +``` + +```{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") +``` + + + + +### 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