I've checked this question about defining models in R. What I would like to do is swap variables in the model in a for loop so that every variable is one time the target variable and all other variables are the predicting variables for that iteration.
df <- data.frame(customer = c(1:5), product1 = c(1,0,1,1,0), product2 = c(0,1,0,0,1), product3 = c(0,1,1,1,0))
customer product1 product2 product3
1 1 1 0 0
2 2 0 1 1
3 3 1 0 1
4 4 1 0 1
5 5 0 1 0
So then I would like to create a for loop with 3 iterations in this case:
mdl <- product1 ~ product2 + product3
mdl <- product2 ~ product1 + product3
mdl <- product3 ~ product1 + product2
To clarify my question here my attempt to create this for loop:
for(j in 1:ncol(df)){
mdl <- df$[j] ~ df[-j] # include all variables except target variable
print(mdl)
}
Here the output I got returned:
df[j] ~ df[-j]
df[j] ~ df[-j]
df[j] ~ df[-j]
df[j] ~ df[-j]
While I expected something such as desired output:
product1 ~ product2 + product3
product2 ~ product1 + product3
product3 ~ product1 + product2
If you wonder why I would like to know this. I want to use it in a for loop that runs a prediction model as in this example:
naiveBayes(mdl, df, type = "raw")
I hope that my question is clear and hopefully anyone could help me out.