-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRmdInit.R
More file actions
136 lines (110 loc) · 3.69 KB
/
RmdInit.R
File metadata and controls
136 lines (110 loc) · 3.69 KB
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
#
# This R script is included in R markdown to initialize the environment with
# libraries and commonly used objects, functions.
#
library(ggplot2)
library(gridExtra)
library(knitr)
library(car)
library(knitcitations)
library(RefManageR)
library(caret)
library(data.table)
library(reshape2)
library(stargazer) ## I'm guessing you might need to install this one.
cleanbib()
cite_options(style="markdown")
myTheme <- theme(axis.ticks=element_blank(),
panel.border = element_rect(color="gray", fill=NA),
panel.background=element_rect(fill="#FBFBFB"),
panel.grid.major.y=element_line(color="white", size=0.5),
panel.grid.major.x=element_line(color="white", size=0.5),
plot.title=element_text(size="8"))
rotateXaxisLabels <- theme(axis.text.x = element_text(angle = 90, hjust = 1))
# Setup for KNITR Table Captions
tn = local({
i = 0
function(x) {
i <<- i + 1
paste('\n\n:', ' ', x, sep = '')
# The : before Table tells pandoc to wrap your caption in <caption></caption>
}
})
knit_hooks$set(tab.cap = function(before, options, envir) {
if(!before)
tn(options$tab.cap)
})
default_output_hook = knit_hooks$get("output")
knit_hooks$set(output = function(x, options) {
if (is.null(options$tab.cap) == F)
x
else
default_output_hook(x,options)
})
# Disable scientific notation
options(scipen=999)
# Function that allows for easy multi column layouts of ggplot2 plots
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
require(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots = length(plots)
# If layout is NULL, then use 'cols' to determine layout
if (is.null(layout)) {
# Make the panel
# ncol: Number of columns of plots
# nrow: Number of rows needed, calculated from # of cols
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
ncol = cols, nrow = ceiling(numPlots/cols))
}
if (numPlots==1) {
print(plots[[1]])
} else {
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
# Make each plot, in the correct location
for (i in 1:numPlots) {
# Get the i,j matrix positions of the regions that contain this subplot
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
layout.pos.col = matchidx$col))
}
}
}
## Generates a dataframe summarizing VIF for all predictors of a model
generateVifSummary <- function(m){
vif4 <- vif(m)
problem <- sqrt(vif(m)) > 2
vifDf4 <- data.frame(cbind(vif4, problem))
vifDf4$problem <- as.logical(vifDf4$problem)
return <- vifDf4
}
# Function that returns Root Mean Squared Error
rmse <- function(error)
{
sqrt(mean(error^2))
}
# Function that returns Mean Absolute Error
mae <- function(error)
{
mean(abs(error))
}
# Generates a dataframe of all correlations of variables in a dataset
# ordered by magnitude of correlation
# filter the data as appropriate before calling this function
generateCorrelationDF <- function(filteredTrainData) {
d<-na.omit(filteredTrainData)
d_cor <- as.matrix(round(cor(d),2))
d_cor_melt <- arrange(melt(d_cor), -abs(value))
c <- ncol(d)
r <- nrow(d_cor_melt)
numTail <- r- c
d_cor_melt<- tail(d_cor_melt, numTail)
Nth.delete<-function(dataframe, n)dataframe[-(seq(n,to=nrow(dataframe),by=n)),]
corFrame <- data.frame(d_cor_melt)
row.names(corFrame) <- seq(nrow(corFrame))
corFrame <- Nth.delete(corFrame, 2)
row.names(corFrame) <- NULL
return <- corFrame
}