Professional Documents
Culture Documents
# Crime Data#
crime_data<-crime_data[,-2]
crime_data<-na.omit(crime_data)
crime<-data.matrix(crime_data)
#find the optimal value of k
#optimal value of k is which gives us concerned clusters with minimum dist
#distortion can be calculated from withinss
kmeans.wss.k<-function(crime2,k){
km=kmeans(crime2,k)
return(km$tot.withinss)
}
# plotting the elbow graph with different values of km$tot.withinss
kmeans.dis<-function(crime,maxk){
dis=(nrow(crime)-1)*sum(apply(crime,2,var))
dis[2:maxk]=sapply(2:maxk,kmeans.wss.k,crime2=crime)
return(dis)
}
maxk=10
dis=kmeans.dis(crime,maxk)
plot(1:maxk,dis,type = 'b',xlab = "Number of clusters",ylab = "Distortion",col="
blue")
cl<-kmeans(crime,4)
cl
## Decision Trees##
# Load the party package. It will automatically load other dependent packages.
library(party)
# Create the input data frame.
input.dat <- readingSkills[c(1:105),]
# Give the chart file a name.
png(file = "decision_tree.png")
# Create the tree.
output.tree <- ctree(nativeSpeaker ~ age + shoeSize + score, data = input.dat, c
ontrols = ctree_control(maxdepth = 3))
# Plot the tree.
plot(output.tree)