# 1) Using the tibble above, create a decision tree. Try tuning the minsplit
#     to get a small tree that has relatively high predictive value. 



# Create the model
m_weather <- rpart(formula = outcome ~ temperature + rain + snow + clouds,
                   data = t.weather,
                   minsplit = 5,
                   method = 'class')

# Show results of model
rpart.plot(m_weather)

# Add the predicted results to each row in the tibble as a field.
predicted_outcomes <- predict(m_weather, t.weather, type = 'class')

t.weather <- mutate(t.weather, predicted_outcome = predicted_outcomes)

# Show a confusion matrix
confusion_matrix <- table(t.weather$outcome, t.weather$predicted_outcome)
print(confusion_matrix)
##        
##         hot muggy snow
##   hot    19     1    0
##   muggy   0     6    1
##   snow    0     0    9
# 2) Run k-means on the data. Analyze the temperature and one other numeric column.

# Plot the two variables you chose, colored by the outcome
# Try to pick two that have a good range
set.seed(451)
ggplot(data = t.weather, aes(x = temperature, y = clouds, color = outcome)) + 
  geom_point(size = 3)

# Run k-means algorithm.
# Try to tweak the number of clusters to give nice outcomes.
kresult <- kmeans(
  x = select(t.weather, temperature, clouds),
  centers = 3,
  nstart = 10
)


# Add the new clusters variable to our tibble.
t.weather <- mutate(t.weather, cluster = kresult$cluster)

# Plot the new clusters
ggplot(data = t.weather, aes(x = temperature, y = clouds, color = as.factor(cluster))) +
  geom_point(size = 3)