Recently, I had the following question about supervised classification models (e.g. random forest) for longitudinal data.
Suppose I have the following data about students passing a fitness test - the students (each student has an "id") who enroll in a school take a fitness test each year and record their height and weight (at the start of each school year, before the fitness test). They can either pass (1) or fail (0) the fitness test each year. The school is interested in knowing which students are likely to fail the fitness test, so they can focus more attention on these students. Naturally, some students might have taken the fitness test more times than other students.
I simulated some data (using the R programming language) to show how the historical data might look like:
score <- c("1","0")
score <- as.numeric(sample(score, 1000, replace=TRUE, prob=c(0.3, 0.7)))
id_sample <- 1:140
id <- sample(id_sample, replace = TRUE, 1000)
height <- abs(rnorm(1000, 150,5))
weight <- abs(rnorm(1000, 75,5))
data = data.frame(id, height, weight, score)
data <- data[order(data$id),]
I then added two variables to this data - one to show how many times the fitness test was taken, the another to show the (cumulative) average number of times the test was passed:
library(dplyr)
data = data.frame(data %>% group_by(id) %>% mutate(counter = row_number(id)))
data$csum <- ave(data$score, data$id, FUN=cumsum)
data$average <- data$csum/data$counter
Now, suppose some of the students are about to take this test again and we would like to predict what their score will be - some of these students are existing students, but some of these students are new and have never taken the test before (i.e. they have no historical data):
id_sample <- 1:140
id <- sample(id_sample, replace = FALSE, 23)
height <- abs(rnorm(23, 150,5))
weight <- abs(rnorm(23, 75,5))
new_data = data.frame(id, height, weight)
new_data <- new_data[order(new_data$id),]
id_sample <- 141:200
id <- sample(id_sample, replace = FALSE, 5)
height <- abs(rnorm(5, 150,5))
weight <- abs(rnorm(5, 75,5))
#simulating data for students who never took the test before
n_data = data.frame(id, height, weight)
n_data <- n_data[order(n_data$id),]
test_data = rbind(new_data, n_data)
Now, to this test data, (where applicable) I added "longitudinal variables" that take into account the number of times the students took the test and their most recent average cumulative score:
#counter
max = data.frame(data %>%
group_by(id) %>%
filter(counter == max(counter)))
colnames(max)[5] <- "max_counter"
max$max_counter = max$max_counter + 1
test_with_counter = merge(x = test_data, y = max, by = "id", all.x = TRUE)
test = test_with_counter[, c(1,2,3,7,9)]
test$max_counter[is.na(test$max_counter)] <- 1
test$average[is.na(test$average)] <- 0
#formatting
colnames(test)[2] <- "height"
colnames(test)[3] <- "weight"
colnames(test)[4] <- "counter"
data$csum = NULL
data$score = as.factor(data$score)
At this point, there is nothing stopping me from training a supervised classification model (e.g. random forest) to predict the "score" variable for the test data:
#skip cross validation for brevity of question
library(randomForest)
rf <- randomForest(score~., data=data)
pred = predict(rf, newdata = test)
print(rf)
Call:
randomForest(formula = score ~ ., data = data)
Type of random forest: classification
Number of trees: 500
No. of variables tried at each split: 2
OOB estimate of error rate: 23.4%
Confusion matrix:
0 1 class.error
0 636 79 0.1104895
My Question: Does the approach that I have proposed for supervised classification of longitudinal data sound reasonable (e.g. better than "nothing") - or are there any major statistical flaws on this approach (e.g. structural multicollinearity, variance inflation, etc.) ? Or is it better to use some supervised classification model/software implementation that has been specifically designed for longitudinal data (e.g. https://cran.r-project.org/web/packages/LongituRF/LongituRF.pdf)? Thanks!
Note:
This is a rough sketch of the situation I am dealing with - I am also planning to include variables such as "number of days that elapsed since last fitness test".
The sample data in this stackoveflow question is randomly simulated and obviously wont show any longitudinal trends.
I have heard that models such as Random Forest have the ability to recover/model around complex interactions and correlations within the data that otherwise need to be explicitly specified in standard supervised models (https://ishwaran.org/papers/IKBL.AOAS.pdf).