-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHighstatLibV6_correlation_functions.R
346 lines (296 loc) · 11.6 KB
/
HighstatLibV6_correlation_functions.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#Library files for courses provided by: Highland Statistics Ltd.
#To cite these functions, use:
#Mixed effects models and extensions in ecology with R. (2009).
#Zuur, AF, Ieno, EN, Walker, N, Saveliev, AA, and Smith, GM. Springer.
#Copyright Highland Statistics LTD.
#####################################################################
#VIF FUNCTION.
#To use: corvif(YourDataFile)
corvif <- function(dataz) {
dataz <- as.data.frame(dataz)
#correlation part
#cat("Correlations of the variables\n\n")
#tmp_cor <- cor(dataz,use="complete.obs")
#print(tmp_cor)
#vif part
form <- formula(paste("fooy ~ ",paste(strsplit(names(dataz)," "),collapse=" + ")))
dataz <- data.frame(fooy=1,dataz)
lm_mod <- lm(form,dataz)
cat("\n\nVariance inflation factors\n\n")
print(myvif(lm_mod))
}
#Support function for corvif. Will not be called by the user
myvif <- function(mod) {
v <- vcov(mod)
assign <- attributes(model.matrix(mod))$assign
if (names(coefficients(mod)[1]) == "(Intercept)") {
v <- v[-1, -1]
assign <- assign[-1]
} else warning("No intercept: vifs may not be sensible.")
terms <- labels(terms(mod))
n.terms <- length(terms)
if (n.terms < 2) stop("The model contains fewer than 2 terms")
if (length(assign) > dim(v)[1] ) {
diag(tmp_cor)<-0
if (any(tmp_cor==1.0)){
return("Sample size is too small, 100% collinearity is present")
} else {
return("Sample size is too small")
}
}
R <- cov2cor(v)
detR <- det(R)
result <- matrix(0, n.terms, 3)
rownames(result) <- terms
colnames(result) <- c("GVIF", "Df", "GVIF^(1/2Df)")
for (term in 1:n.terms) {
subs <- which(assign == term)
result[term, 1] <- det(as.matrix(R[subs, subs])) * det(as.matrix(R[-subs, -subs])) / detR
result[term, 2] <- length(subs)
}
if (all(result[, 2] == 1)) {
result <- data.frame(GVIF=result[, 1])
} else {
result[, 3] <- result[, 1]^(1/(2 * result[, 2]))
}
invisible(result)
}
#END VIF FUNCTIONS
##################################################################
##################################################################
#Here are some functions that we took from the pairs help file and
#modified, or wrote ourselves. To cite these, use the r citation: citation()
panel.cor <- function(x, y, digits=1, prefix="", cex.cor = 6)
{
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
r1=cor(x,y,use="pairwise.complete.obs")
r <- abs(cor(x, y,use="pairwise.complete.obs"))
txt <- format(c(r1, 0.123456789), digits=digits)[1]
txt <- paste(prefix, txt, sep="")
if(missing(cex.cor)) { cex <- 0.9/strwidth(txt) } else {
cex = cex.cor}
text(0.5, 0.5, txt, cex = cex * r)
}
##################################################################
panel.smooth2=function (x, y, col = par("col"), bg = NA, pch = par("pch"),
cex = 1, col.smooth = "black", span = 2/3, iter = 3, ...)
{
points(x, y, pch = pch, col = col, bg = bg, cex = cex)
ok <- is.finite(x) & is.finite(y)
if (any(ok))
lines(stats::lowess(x[ok], y[ok], f = span, iter = iter),
col = 1, ...)
}
##################################################################
panel.lines2=function (x, y, col = par("col"), bg = NA, pch = par("pch"),
cex = 1, ...)
{
points(x, y, pch = pch, col = col, bg = bg, cex = cex)
ok <- is.finite(x) & is.finite(y)
if (any(ok)){
tmp=lm(y[ok]~x[ok])
abline(tmp)}
}
##################################################################
panel.hist <- function(x, ...)
{
usr <- par("usr"); on.exit(par(usr))
par(usr = c(usr[1:2], 0, 1.5) )
h <- hist(x, plot = FALSE)
breaks <- h$breaks; nB <- length(breaks)
y <- h$counts; y <- y/max(y)
rect(breaks[-nB], 0, breaks[-1], y, col="white", ...)
}
##################################################################
##################################################################
##################################################################
##################################################################
#Functions for variograms
#To cite these functions, use:
#Mixed effects models and extensions in ecology with R. (2009).
#Zuur, AF, Ieno, EN, Walker, N, Saveliev, AA, and Smith, GM. Springer.
#Make a variogram for one variable
#To use, type: MyVariogram(XUTM, YUTM, E , MyDistance=10)
# XUTM is x coordinates
# XUTM is y coordinates
# E is variable used in sample variogram
# MyDistance is the cutoff value for the distances
MyVariogram <- function(x,y,z, MyDistance) {
library(gstat)
mydata <- data.frame(z, x, y)
coordinates(mydata) <- c("x", "y")
Var <- variogram(z ~ 1, mydata, cutoff = MyDistance)
data.frame(Var$np, Var$dist, Var$gamma)
}
#Function for making multiple variograms in an xyplot
#To use, type: MultiVariogram(Z, MyVar,XUTM, YUTM, MyDistance=10)
# Z is a data frame with all the data
# Character string with variable names that will be used in the xyplot
# XUTM is x coordinates
# XUTM is y coordinates
# MyDistance is the cutoff value for the distances
MultiVariogram <- function(Z, MyVar, x, y, MyDistance) {
#Z is the data frame with data
#MyVar is a list of variables for for which variograms are calculated
#x, y: spatial coordinates
#MyDistance: limit for distances in the variogram
library(lattice)
VarAll<- c(NA,NA,NA,NA)
for (i in MyVar){
vi <- MyVariogram(x,y,Z[,i], MyDistance)
vii <- cbind(vi, i)
VarAll <- rbind(VarAll,vii)
}
VarAll <- VarAll[-1,]
P <- xyplot(Var.gamma ~ Var.dist | factor(i), col = 1, type = "p", pch = 16,
data = VarAll,
xlab = "Distance",
ylab = "Semi-variogram",
strip = function(bg='white', ...)
strip.default(bg='white', ...),
scales = list(alternating = T,
x = list(relation = "same"),
y = list(relation = "same"))
)
print(P)
}
#End variogram code
##########################################################
#Function for multi-panel Cleveland dotplot.
#The input file must contain no categorical variables
Mydotplot <- function(DataSelected){
P <- dotplot(as.matrix(as.matrix(DataSelected)),
groups=FALSE,
strip = strip.custom(bg = 'white',
par.strip.text = list(cex = 1.2)),
scales = list(x = list(relation = "free", draw = TRUE),
y = list(relation = "free", draw = FALSE)),
col=1, cex = 0.5, pch = 16,
xlab = list(label = "Value of the variable", cex = 1.5),
ylab = list(label = "Order of the data from text file", cex = 1.5))
print(P)
}
#Add more code here:
Mybwplot <- function(Z, MyVar, TargetVar){
#Multipanel boxplots
#Z: data set
#MyVar: character string
#TargetVar: variable for the x-axis..must be a factor
AllY <- as.vector(as.matrix(Z[,MyVar]))
AllX <- rep(Z[,TargetVar], length(MyVar))
ID <- rep(MyVar, each = nrow(Z))
P <- bwplot(AllY ~ factor(AllX) | ID, horizontal = FALSE,
ylab = "", xlab = "",
scales = list(alternating = T,cex.lab = 1.5,
x = list(relation = "same",rot =90, abbreviate = TRUE, cex = 1.5),
y = list(relation = "free", draw = FALSE)),
strip = strip.custom(bg = 'white',
par.strip.text = list(cex = 1.2)),
cex = .5,
par.settings = list(
box.rectangle = list(col = 1),
box.umbrella = list(col = 1),
plot.symbol = list(cex = .5, col = 1)))
print(P)
}
#######################################################
MyxyplotBin <- function(Z, MyV, NameY1) {
AllX <- as.vector(as.matrix(Z[,MyV]))
AllY <- rep(Z[,NameY1] , length(MyV))
AllID <- rep(MyV, each = nrow(Z))
library(mgcv)
library(lattice)
P <- xyplot(AllY ~ AllX | factor(AllID), col = 1,
strip = function(bg='white', ...) strip.default(bg='white', ...),
scales = list(alternating = T,
x = list(relation = "free"),
y = list(relation = "same")),
xlab = "Covariate",
ylab = "Probability of presence",
panel=function(x,y){
panel.grid(h=-1, v= 2)
panel.points(x,y,col=1)
tmp<-gam(y~s(x, k = 4), family = binomial)
MyData <- data.frame(x = seq(min(x), max(x), length = 25))
p1 <- predict(tmp, newdata = MyData, type ="response")
panel.lines(MyData$x,p1, col = 1, lwd = 3)
})
print(P)
}
#######################################################
#######################################################
Myxyplot <- function(Z, MyV, NameY1,MyYlab="") {
AllX <- as.vector(as.matrix(Z[,MyV]))
AllY <- rep(Z[,NameY1] , length(MyV))
AllID <- rep(MyV, each = nrow(Z))
library(mgcv)
library(lattice)
P <- xyplot(AllY ~ AllX|factor(AllID), col = 1,
xlab = list("Explanatory variables", cex = 1.5),
#ylab = list("Response variable", cex = 1.5),
#ylab = list("Pearson residuals", cex = 1.5),
ylab = list(MyYlab, cex = 1.5),
#layout = c(2,2), #Modify
strip = function(bg='white', ...)
strip.default(bg='white', ...),
scales = list(alternating = T,
x = list(relation = "free"),
y = list(relation = "same")),
panel=function(x, y){
panel.grid(h=-1, v= 2)
panel.points(x, y, col = 1)
panel.loess(x, y, span = 0.8,col = 1, lwd = 2)})
print(P)
}
#######################################################
MyxyplotPolygon <- function(Z, MyV, NameY1) {
AllX <- as.vector(as.matrix(Z[,MyV]))
AllY <- rep(Z[,NameY1] , length(MyV))
AllID <- rep(MyV, each = nrow(Z))
library(mgcv)
library(lattice)
Z <- xyplot(AllY ~ AllX|factor(AllID), col = 1,
xlab = list(label = "Explanatory variables", cex = 1.5),
ylab = "",
strip = function(bg='white',cex.lab = 1.5,...)
strip.default(bg='white', ...),
scales = list(alternating = T,
x = list(relation = "free"),
y = list(relation = "same")),
panel=function(x, y){
t1 <- gam(y~s(x))
MD1 <- data.frame(x=seq(from = min(x, na.rm = TRUE),
to = max(x, na.rm = TRUE),
length = 100))
P1 <- predict(t1, se.fit = TRUE)
I1 <- order(x)
xs <- sort(x)
panel.lines(xs, P1$fit[I1], col = 1)
panel.polygon(c(xs, rev(xs)),
c(P1$fit[I1]-2*P1$se.fit[I1],
rev(P1$fit[I1]+2*P1$se.fit[I1])),
col = gray(0.7),
density = 10 )
panel.grid(h=-1, v= 2)
panel.abline(0,0)
panel.points(x, y, col = 1)
})
#Because the xyplot is inside a function you need to print
#construction below
print(Z)
}
################################################
#Mypairs
#Make fancy pair plots
Mypairs <- function(Z) {
MyVarx <- colnames(Z)
pairs(Z, labels = MyVarx,
cex.labels = 2,
lower.panel = function(x, y, digits=2, prefix="", cex.cor = 7) {
panel.cor(x, y, digits, prefix, cex.cor)},
upper.panel = function(x, y) points(x, y,
pch = 16, cex = 0.8,
col = gray(0.1)))
#print(P)
}