library(tree)
Predicting whether the organization’s purpose is religious
Data needs to be factor to classify, so I restructure the outcome variable. Set up seperate data frame with the training data.
set.seed(500)
train <- sample(1:nrow(dat2), 1200)
dat2$religiousorg <- ifelse(dat2$Orgpurposereligious==1, "religious", "ungodly")
dat2$religiousorg <- as.factor(dat2$religiousorg)
train.dat <- dat2[(dat2$list %in% train), ]
-Orgpurposereligious prevents use of the outcome variable in a different form for prediction. We can see the tree that is formed with labels.
tree.religion = tree(religiousorg~.-Orgpurposereligious, data=train.dat)
plot(tree.religion)
text(tree.religion, pretty=0, cex=.5)
How well does it classify? We can see how the test data does with the training model.
tree.pred = predict(tree.religion, dat2[-train,], type="class")
with(dat2[-train,], table(tree.pred, religiousorg))
## religiousorg
## tree.pred religious ungodly
## religious 220 84
## ungodly 195 2579