I have a data set with 8 variables. I need all possible two way interaction terms along with the seven predictors in each model. So, in my case there will be total 7C2 = 21 models, each of them containing the 7 predictors and a two way interaction term at a time.
I have tried to produce the 21 models using for loop but the code seems to fail at the lm() function when I try to use that inside the for loop. In my problem return is the response variable at the 5-th column of my data.
colnames(dt) = c("assets","turnover_ratio","SD","sharpe_ratio","return",
"expense_ratio","fund_dummy","risk_dummy")
vars=colnames(dt)[-5]
for (i in vars) {
for (j in vars) {
if (i != j) {
factor= paste(i,j,sep='*')}
lm.fit <- lm(paste("return ~", factor), data=dt)
print(summary(lm.fit))
}}
The error message is given below for the code:
Error in paste("return ~", factor) : cannot coerce type 'closure' to vector of type 'character'
This is my data set:
The output below should be the desired output and 20 more such models are needed with other possible two way interaction terms. All the 7 predictors should be present in each model. The only thing that should change is the two way interaction term.
This is my desired output among the 21 required: