Creation of all combinations of selected variables that will go into models as predictors
setwd('~/R/DynamicRegressionModel/')
msleep <- read.table(file = "DataAuto.txt",sep="\t",header=TRUE,dec=".",row.names=1)
colnames(msleep) <- c("Price","Cylinder","Power","Weight","Consumption")
#Omitting NA in the data frame
msleep <- na.omit(msleep)
#Setting the response variable
response <- "Consumption"
#Number of colums of the data frame
getLen <- dim(msleep)[2]
for (i in 1:getLen){
#Removing the response variable in the temporary data frame
if (names(msleep)[i] == response){
expVariable <- msleep [,-i]
break;
}
}
#Number of colums of the temporary data frame of explanatory variables
getLenExpVar <- dim(expVariable)[2]
#Initializing a Matrix which will contain all combinations of predictor model
ExpVarMatrix <- matrix( ncol = getLenExpVar)
#Creating combination
for (i in 1:getLenExpVar){
#Combination function
comb <- t(combn(names(expVariable),i))
numbRow <- nrow(comb)
numbCol <- length(names(expVariable))
numbRowNA <- numbRow
numbColNA <- numbCol-ncol(comb)
naMatr <- matrix(rep(NA, numbRowNA*numbColNA), nrow = numbRowNA, ncol = numbColNA)
result <- cbind(comb, naMatr)
ExpVarMatrix <- rbind(ExpVarMatrix, result)
}
#Removing all NA
ExpVarMatrix <- ExpVarMatrix[-1,]
#Final result of combination between response and explanatory variables
#Setting an empty data frame
dynamicRegression <- data.frame()
for (i in 1:nrow(ExpVarMatrix)){
getVal <- na.omit (ExpVarMatrix[i, ])
mdRegComb <- paste (response, " ~ ", paste (getVal, collapse = " + "), sep = "")
#print(mdRegComb)
mdLM <- lm(as.formula(mdRegComb), data = msleep)
SMry <- summary(mdLM)
#Diagnostic parameters
FStats <- SMry$fstatistic[1] #Fstatistic
f <- SMry$fstatistic #p-value
modelPValue <- pf( f[1], f[2], f[3],lower = FALSE ) #model p-Value
RSqrt <- SMry[8] #R-Squared
AdjRSqrt <- SMry[9] #adj R-Squared
AIC <- AIC(mdLM)#AIC
BIC <- BIC(mdLM)#BIC
#Assembling diagnostic parameters per model predictor in Matrix of all combinations
dFrame <- data.frame(modelReg = mdRegComb, RSquared = RSqrt, AdjustedRSquared = AdjRSqrt, AIC = AIC, BIC = BIC, modelPValue = modelPValue, FStats = f[1])
#Loading data framme
dynamicRegression <- rbind(dynamicRegression, dFrame)
}
#Writing dynamicRegression in a csv file
write.csv(dynamicRegression, "dynamicRegression.csv", row.names = F)
out <- dynamicRegression[,c(1,2,3,4,5)]
colnames(out) <- c("modelReg","RQqrt","adjRSqrt","AIC","BIC")
print(out,width = getOption("width"),justify = "none")
## modelReg RQqrt
## value Consumption ~ Price 0.7940777
## value1 Consumption ~ Cylinder 0.8854660
## value2 Consumption ~ Power 0.9074941
## value3 Consumption ~ Weight 0.7462581
## value4 Consumption ~ Price + Cylinder 0.8965199
## value5 Consumption ~ Price + Power 0.9074946
## value6 Consumption ~ Price + Weight 0.9429137
## value7 Consumption ~ Cylinder + Power 0.9153745
## value8 Consumption ~ Cylinder + Weight 0.9045631
## value9 Consumption ~ Power + Weight 0.9448453
## value10 Consumption ~ Price + Cylinder + Power 0.9153826
## value11 Consumption ~ Price + Cylinder + Weight 0.9436222
## value12 Consumption ~ Price + Power + Weight 0.9532331
## value13 Consumption ~ Cylinder + Power + Weight 0.9449972
## value14 Consumption ~ Price + Cylinder + Power + Weight 0.9545586
## adjRSqrt AIC BIC
## value 0.7869770 122.85177 127.15373
## value1 0.8815165 104.66633 108.96829
## value2 0.9043043 98.04474 102.34671
## value3 0.7375083 129.32516 133.62712
## value4 0.8891285 103.52006 109.25600
## value5 0.9008871 100.04459 105.78053
## value6 0.9388361 85.08080 90.81675
## value7 0.9093298 97.28462 103.02057
## value8 0.8977462 101.01172 106.74767
## value9 0.9409057 84.01368 89.74963
## value10 0.9059807 99.28165 106.45159
## value11 0.9373579 86.69367 93.86361
## value12 0.9480367 80.89978 88.06972
## value13 0.9388858 85.92819 93.09813
## value14 0.9475676 82.00846 90.61239