The logic is similar to the content-based recommender,
| content | undesirable | desirable | user_1 | ... | user_10 |
|---|---|---|---|---|---|
| 1 | 3.00 | 2.77 | 0.11 | NA | |
| ... | |||||
| 5000 | 2.50 | 2.11 | NA | 0.12 |
I need to run the model for undesirable and desirable as independent values and each user as the dependent value, thus I need run 10 times to fit the model and predict each user's NA value.
This is the code that I hard coding, but I wonder how to use for loop, I just searched for several methods but they do not work for me...
the data as 'test'
hard code
#fit model
fit_1 = lm(user_1 ~ undesirable + desirable, data = test)
...
fit_10 = lm(user_10 ~ undesirable + desirable, data = test)
#prediction
u_1_na = test[is.na(test$user_1), c('user_1', 'undesirable', 'desirable')]
result1 = predict(fit_1, newdata = u_1_na)
which(result1 == max(result1))
max(result1)
...
u_10_na = test[is.na(test$user_10), c('user_10', 'undesirable', 'desirable')]
result10 = predict(fit_10, newdata = u_10_na)
which(result10 == max(result10))
max(result10)
#make to csv file
apply each max predict value to csv.
this is what I try for now(for loop)
mod_summaries <- list()
for(i in 1:10) {
predictors_i <- colnames(data)[1:10]
mod_summaries[[i - 1]] <- summary(
lm(predictors_i ~ ., test[ , c("undesirable", 'desirable')]))
}
formulas <- paste0("user_", 1:10, " ~ undesirable + desirable"use them to iterate and create the regressionsmodels <- lapply(formulas, \(x)lm(as.formula(x), data = test))i - 1, as index 0 languages like Python require. Simply refer toi.