Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assignment 6 #148

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions Assignment6.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
```

Expand All @@ -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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job! I like the way that you process/deal with the data to prepare for later modeling. This task asks you to compare at least two different models, so you could go further to choose different predictors/features and compare measures from the result.


post(c.tree4, file = "tree4.ps", title = "degree type") #This creates a pdf image of the tree
```


Expand Down
Loading