I want to use KNN to model and predict proportions. I would like to use caret for such purpose but I get errors when I try to train a model.
This is an example to ilustrate the error:
library(caret)
# Set the seed for reproducibility
set.seed(100)
# Number of rows in the dataframe
num_rows <- 50
# Explanatory variables
proportions <- matrix(runif(num_rows * 3), nrow = num_rows)
proportions <- proportions / rowSums(proportions)
response_variables <- data.frame(A = proportions[, 1], B = proportions[, 2], C = proportions[, 3])
# Response variables
values <- matrix(sample(1:50, num_rows * 6, replace = TRUE), nrow = num_rows)
explanatory_variables <- data.frame(v1 = values[, 1], v2 = values[, 2], v3 = values[, 3],
v4 = values[, 4], v5 = values[, 5], v6 = values[, 6])
# Combine the response and explanatory variables into one dataframe
data <- cbind(response_variables, explanatory_variables)
# Train and test split
trainIndex <- createDataPartition(data$A,
times=1,
p = .8,
list = FALSE)
train <- data[trainIndex, ]
test <- data[-trainIndex, ]
# Data processing
preProcValues <- preProcess(train, method = c("center", "scale"))
trainTransformed <- predict(preProcValues, train)
testTransformed <- predict(preProcValues, test)
# Model tunimg
y = trainTransformed[,1:3]
knnModel <- train(
y ~ .,
data = trainTransformed,
method = "knn",
trControl = trainControl(method = "cv"),
tuneGrid = data.frame(k = c(3,5,7))
)
> Error in model.frame.default(form = y ~ ., data = trainTransformed, na.action = na.fail) :
> invalid type (list) for variable 'y'