-
Notifications
You must be signed in to change notification settings - Fork 18
/
s8.1.GLM.R
53 lines (47 loc) · 1.63 KB
/
s8.1.GLM.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
# ==========================================
# GLM stepwise - based on AIC (caret)
# Generalized Linear Model with Stepwise Feature Selection
# ==========================================
# contact: Cristian R Munteanu | BiGCaT - UM | muntisa@gmail.com
#
# inputs:
# - my.datf, my.datf.train,my.datf.test = full, train and test dataset frames
# - fDet = flag for detais (TRUE/FALSE)
# - outFile = output file
# output = statistics
# ------------------------------------------
GLMreg <- function(my.datf,my.datf.train,my.datf.test,fDet=FALSE,outFile="") {
my.stats<- list() # create empty result
# specify CV parameters
ctrl<- trainControl(method = 'repeatedcv', number = 10,repeats = 10,
summaryFunction = defaultSummary)
# Training the model using only training set
set.seed(2)
attach(my.datf.train)
lm.fit<- train(net.c~.,data=my.datf.train,
method = 'glmStepAIC', tuneLength = 10, trControl = ctrl,
metric = 'RMSE')
# RESULTS
#--------------
# get statistics
RMSE = lm.fit$results[,2]
Rsquared = lm.fit$results[,3]
RMSE_SD = lm.fit$results[,4]
RsquaredSD = lm.fit$results[,5]
# RMSE & R^2, for train/ test respectively
lm.train.res<- getTrainPerf(lm.fit)
lm.test.res <- postResample(predict(lm.fit,my.datf.test),my.datf.test[,1])
if (fDet==TRUE) {
# write RESULTS
sink(outFile)
summary(my.datf)
lm.fit
predictors(lm.fit)
lm.train.res
lm.test.res
sink()
# file.show(outFile)
}
my.stats = list("RMSE"= RMSE,"Rsquared" = Rsquared,"RMSE_SD" = RMSE_SD,"RsquaredSD" = RsquaredSD)
return(my.stats) # return statistics
}