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
<- tracking_table
mydata $migraine <- if_else(mydata$headache, "yes","no")
mydata$migraine <- factor(mydata$migraine)
mydata$sleep <- mydata$z mydata
8.3 Select a random subset for training
Code
# select 25% of the rows from the data frame for training
<- mydata %>% sample_frac(size=0.25)
mydata_train
# use the remaining rows for testing
<- mydata %>% anti_join(mydata_train) mydata_test
Joining, by = c("date", "headache", "stress", "icecream", "z", "wine",
"migraine", "sleep")
Code
<- "migraine" response
8.4 Make a prediction model
Code
# Convert the response variable to a factor
$headache <- as.factor(mydata_train$headache)
mydata_train$headache <- as.factor(mydata_test$headache)
mydata_test
# Train the model using the training data
<- caret::train(migraine ~ stress + icecream + z + wine,
model_caret 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
<- predict(model_caret, mydata_test)
predictions_caret
# Evaluate the model's performance on the test data
<- postResample(predictions_caret, mydata_test$migraine)
performance_caret 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")