-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathols_summary.R
167 lines (148 loc) · 6.68 KB
/
ols_summary.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#====================================================================#
# Author: Damian Gwozdz (DG)
# Function: ols
# Creation date: 15JUN2017
# Last modified: 14MAY2018
# Description: Function to build multiple Ordinary
# Least Squares models in a parallelized way
# Required functions: ols
#
#====================================================================#
source("ols.r")
source("ncomb.r")
ols_summary <- function(dset.sum, target.sum, vars.sum, alpha.sum = .05,
intercept.sum = TRUE, do.parallel = FALSE, n.cores = 2,
visualize.sum = FALSE, time.var.sum = NULL,
progress.bar = TRUE, pred.R2.sum = FALSE){
#====================================================================
# PARAMETERS:
#
# 1) dset.sum - input data set
# 2) target.sum - vector of target variables declared as strings
# 3) vars.sum - vector of independent variables declared as strings
# and separated by blanks in each string
# 4) alpha.sum - significance level
# 5) intercept.sum - a boolean value indicating whether the built model
# should have an intercept
# 6) do.parallel - a boolean value indicating whether parallelization
# should be used
# 7) n.cores - number of utilized cores (active only if do.parallel == T)
# 6) visualize.sum - a boolean value indicating whether the built model
# should be visualized [CURRENTLY INACTIVE]
# 7) time.var.sum - variable identifying time
# 8) progress.bar - a boolean value indicating whether a progress bar
# for non-parallelized computations should be displayed
# 9) pred.R2.sum - a boolean value indicating whether predicted R-squared
# should be computed; this option is turned off by
# default due to computation time
#====================================================================
# Parameters
# dset.sum <- iris
# target.sum <- rep("Sepal.Length", 3)
# vars.sum <- c("Sepal.Width Petal.Length Petal.Width", "Petal.Length Petal.Width",
# "Sepal.Width Petal.Width")
# alpha.sum <- .05
# intercept.sum <- T
# do.parallel <- F
# visualize.sum <- F
# n.cores <- 2
# Check if all variables exist in the data set:
vars.check <- unique(c(unlist(strsplit(vars.sum, " ")), unique(target.sum)))
if(sum(vars.check %in% names(dset.sum)) != length(vars.check)){
lack.vars.index <- which(!(vars.check %in% names(dset.sum)))
lack.vars <- paste(vars.check[lack.vars.index], collapse = ", ")
stop(paste0("Variable(s):\n ", lack.vars, "\n is/are not in the data set."))
}
#
num.models <- length(vars.sum)
num.NA <- rep(NA, num.models)
model.stats <- data.frame(target = num.NA, vars = num.NA, R2 = num.NA,
adjusted.R2 = num.NA, RMSE = num.NA,
pred.R2 = num.NA,
AIC = num.NA, BIC = num.NA, F.stat = num.NA,
F.p.value = num.NA, bp.stat = num.NA,
bp.p.value = num.NA, bg.stat = num.NA,
bg.p.value = num.NA, reset.stat = num.NA,
reset.p.value = num.NA, ad.stat = num.NA,
ad.p.value = num.NA, sw.stat = num.NA,
sw.p.value = num.NA, chow.stat = num.NA,
chow.p.value = num.NA,
significance = num.NA, max.p.value = num.NA,
max.vif = num.NA,
tests = num.NA, n = num.NA, equation = num.NA)
if(intercept.sum == FALSE){
model.stats$bp.stat <- NULL
model.stats$bp.p.value <- NULL
}
model.vars <- vector(mode = "list", length = num.models)
# Non-parallelized version
if(do.parallel == FALSE){
if(progress.bar){
pb <- winProgressBar(title="Number of built models", label="0% done",
min=0, max=100, initial=0)
for(i in 1:length(vars.sum)){
ols.i <- ols(dset = dset.sum,
target = target.sum[i],
vars = vars.sum[i],
alpha = alpha.sum,
intercept = intercept.sum,
visualize = visualize.sum,
time.var = time.var.sum,
pred.R2 = pred.R2.sum)
model.stats[i,] <- ols.i[["stats"]]
model.vars[[i]] <- ols.i[["var.stats"]]
models <- list("stats" = model.stats, "vars.stats" = model.vars)
info <- sprintf("%d%% done", round((i/length(vars.sum))*100))
setWinProgressBar(pb, i/(length(vars.sum))*100, label=info)
}
close(pb)
}else{
for(i in 1:length(vars.sum)){
ols.i <- ols(dset = dset.sum,
target = target.sum[i],
vars = vars.sum[i],
alpha = alpha.sum,
intercept = intercept.sum,
visualize = visualize.sum,
time.var = time.var.sum,
pred.R2 = pred.R2.sum)
model.stats[i,] <- ols.i[["stats"]]
model.vars[[i]] <- ols.i[["var.stats"]]
models <- list("stats" = model.stats, "vars.stats" = model.vars)
}
}
# Parallelized version
}else{
library(parallel)
cores <- n.cores
cl <- makeCluster(cores)
clusterEvalQ(cl, library("caret"))
clusterEvalQ(cl, library("lmtest"))
clusterEvalQ(cl, library("nortest"))
clusterEvalQ(cl, library("car"))
clusterEvalQ(cl, library("strucchange"))
clusterExport(cl = cl,
varlist = c("ols",
"dset.sum", "vars.sum", "target.sum",
"alpha.sum", "intercept.sum", "visualize.sum"),
envir = environment())
models <- parLapply(cl = cl, X = seq_len(num.models),
function(i) ols(dset = dset.sum,
target = target.sum[i],
vars = vars.sum[i],
alpha = alpha.sum,
intercept = intercept.sum,
visualize = visualize.sum,
pred.R2 = pred.R2.sum)
)
stopCluster(cl)
for(i in 1:num.models){
model.stats[i,] <- models[[i]]$stats
model.vars[[i]] <- models[[i]]$var.stats
}
# Adding model nuber to the flat table with all the model stats
models <- list("stats" = model.stats, "vars.stats" = model.vars)
}
models$stats$model.num <- 1:num.models
return(models)
}