8  headaches

8.1 Machine learning

Another way to study this data is through machine learning. Instead of a straightforward mathematical transformation of all the data, we can take a subset (called the “training” set) and use algorithms to build a model to describe the data. Then we can apply that model to the rest of the data (called the “test” set).

Code
library(tidyverse)
library(caret)
Loading required package: lattice

Attaching package: 'caret'
The following object is masked from 'package:purrr':

    lift

8.2 Generate the sample data

Code
# create a data frame with the generated data
mydata <- tracking_table
mydata$migraine <- if_else(mydata$headache, "yes","no")
mydata$migraine <- factor(mydata$migraine)
mydata$sleep <- mydata$z

8.3 Select a random subset for training

Code
# select 25% of the rows from the data frame for training

mydata_train <- mydata %>% sample_frac(size=0.25)

# use the remaining rows for testing
mydata_test <- mydata %>% anti_join(mydata_train)
Joining, by = c("date", "headache", "stress", "icecream", "z", "wine",
"migraine", "sleep")
Code
response <- "migraine"

8.4 Make a prediction model

Code
# Convert the response variable to a factor
mydata_train$headache <- as.factor(mydata_train$headache)
mydata_test$headache <- as.factor(mydata_test$headache)

# Train the model using the training data
model_caret <- caret::train(migraine ~ stress + icecream + z + wine,
                     data = mydata_train,
                     method = "glm",
                     family = binomial())
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Code
# Make predictions on the test data
predictions_caret <- predict(model_caret, mydata_test)

# Evaluate the model's performance on the test data
performance_caret <- postResample(predictions_caret, mydata_test$migraine)
print(performance_caret)
 Accuracy     Kappa 
0.9768707 0.9121351 

8.5 Plot the result

Code
# Plot the model's predictions against the actual values
ggplot(mydata_test, aes(x = predictions_caret, y = mydata_test[,response])) +
  geom_point() +
  geom_abline(intercept = 0, slope = 1, color = "red") +
  xlab("Predictions") +
  ylab("Actual Values") +
  ggtitle("Prediction vs Actual Values")