-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultipleTests.R
More file actions
145 lines (110 loc) · 6.04 KB
/
MultipleTests.R
File metadata and controls
145 lines (110 loc) · 6.04 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
136
137
138
139
140
141
142
143
144
145
#Performs multiple tests on a given data type.
# dataFun - function generating the data, should return the data as specified in "DataTypes.R".
# dataParams - parameters fed to dataFun
# testFuns - vector containing functions for the tests to be applied
# testParams - nested lists containing parameters for testFuns
# nPerm - number of permutations preformed
#Multivariate permutation test
multPermMultTest <- function(dataFun, dataParams, testFuns, testParams, nPerm) {
obs <- do.call(dataFun, as.list(dataParams)) #Generates data, "odd" function call to be able to pass arguments as vector
nCols <- ncol(obs)
yObs <- obs[,nCols] #Response variable
xObs <- obs[,(nCols-1)] #Explanatory variable of interest
zObs <- obs[,1:(nCols-2)] #Nuisance variables
#Fit of original observation
obsFit <- lm(yObs ~ xObs + zObs, model=FALSE)
#Fit of reduced model (Freedman-Lane)
redYZFit <- lm(yObs ~ zObs, model=FALSE)
#Permute reduced model and fit full model using permutations
permYZRes <- replicate(nPerm, sample(redYZFit$residuals))
permY <- permYZRes + zObs*redYZFit$coefficients["zObs"]
permFit <- lm(permY ~ xObs + zObs)
#Summaries of fits for extracting value of test statistics
obsSum <- summary(obsFit)
permSum <- summary(permFit)
#Convert strings to variable names
testParams <- lapply(testParams, function(x) lapply(x, as.name))
#Perform the tests
res <- sapply(1:length(testFuns), function(x) do.call(testFuns[[x]], testParams[[x]]))
return(c(res, kurtosis(obsFit$residuals), skewness(obsFit$residuals)))
}
#Function used to flip signs randomly for the sign-flip permutation test
flipfun <- function(len){
temp <- rbinom(len, 1, prob=0.5)
temp[temp==0] <- -1
return(temp)
}
#Multivariate sign flip and permutation test
multSignAndPermMultTest <- function(dataFun, dataParams, testFuns, testParams, nPerm) {
obs <- do.call(dataFun, as.list(dataParams)) #Generates data, "odd" function call to be able to pass arguments as vector
nCols <- ncol(obs)
yObs <- obs[,nCols] #Response variable
xObs <- obs[,(nCols-1)] #Explanatory variable of interest
zObs <- obs[,1:(nCols-2)] #Nuisance variables
#Fit of original observation
obsFit <- lm(yObs ~ xObs + zObs, model=FALSE)
#Fit of reduced model (Freedman-Lane)
redYZFit <- lm(yObs ~ zObs, model=FALSE)
#Permute residuals of reduced model and fit full model using permuted residuals
permYZRes <- replicate(nPerm, sample(redYZFit$residuals))
permY <- permYZRes + zObs*redYZFit$coefficients["zObs"]
permFit <- lm(permY ~ xObs + zObs)
#Sign flip residuals of reduced model and fit full model using new residuals
signYZRes <- redYZFit$residuals*replicate(nPerm, sample(flipfun(length(redYZFit$residuals))))
signY <- signYZRes + zObs*redYZFit$coefficients["zObs"]
signFit <- lm(signY ~ xObs + zObs)
#Summaries of fits for extracting value of test statistics
obsSum <- summary(obsFit)
permSum <- summary(permFit)
signSum <- summary(signFit)
#For sign test, has to be changed manually
signTestParams <- list(c("xObs", "zObs", "redYZFit", "signYZRes"), c("obsSum", "signSum"))
signTestParams <- lapply(signTestParams, function(x) lapply(x, as.name))
signTestFuns <- c("multRSquaredTest", "tTest")
#Convert strings to variable names
testParams <- lapply(testParams, function(x) lapply(x, as.name))
#Perform the tests
permRes <- sapply(1:length(testFuns), function(x) do.call(testFuns[[x]], testParams[[x]]))
signRes <- sapply(1:length(signTestFuns), function(x) do.call(signTestFuns[[x]], signTestParams[[x]]))
return(c(signRes, permRes, kurtosis(obsFit$residuals), skewness(obsFit$residuals)))
}
#Simple permutation test
simplePermMultTest <- function(dataFun, dataParams, testFuns, testParams, nPerm) {
obs <- do.call(dataFun, as.list(dataParams)) #Generates data, "odd" function call to be able to pass arguments as vector
#Fit of original observation
obsFit <- lm(obs[,2]~obs[,1], model=FALSE)
permutations <- replicate(nPerm, sample(obs[,2])) #Permuted y-values
permFit <- lm(permutations~obs[,1]) #Fits of permuted dependent variables
#Summaries of fits for extracting value of test statistics
permSum <- summary(permFit)
obsSum <- summary(obsFit)
#Convert strings to variable names
testParams <- lapply(testParams, function(x) lapply(x, as.name))
#Perform the tests
res <- sapply(1:length(testFuns), function(x) do.call(testFuns[[x]], testParams[[x]]))
return(c(res, kurtosis(obsFit$residuals), skewness(obsFit$residuals)))
}
#Simple sign flip and permutation test
simpleSignAndPermMultTest <- function(dataFun, dataParams, testFuns, testParams, nPerm) {
obs <- do.call(dataFun, as.list(dataParams)) #Generates data, "odd" function call to be able to pass arguments as vector
#Fit of original observation
obsFit <- lm(obs[,2]~obs[,1], model=FALSE)
permutations <- replicate(nPerm, sample(obs[,2])) #Permuted y-values
permFit <- lm(permutations~obs[,1]) #Fits of permuted dependent variables
signfliped <- obs[,2]*replicate(nPerm, flipfun(length(obs[,2]))) #Sign fliped y-values
signFit <- lm(signfliped~obs[,1]) #Fits of permuted dependent variables
#Summaries of fits for extracting value of test statistics
permSum <- summary(permFit)
signSum <- summary(signFit)
obsSum <- summary(obsFit)
#For sign test, has to be changed manually
signTestParams <- list(c("obsSum", "signSum"), c("obsSum", "signSum"))
signTestParams <- lapply(signTestParams, function(x) lapply(x, as.name))
signTestFuns <- c("simpleRSquaredTest", "tTest")
#Convert strings to variable names
testParams <- lapply(testParams, function(x) lapply(x, as.name))
#Perform the tests
permRes <- sapply(1:length(testFuns), function(x) do.call(testFuns[[x]], testParams[[x]]))
signRes <- sapply(1:length(signTestFuns), function(x) do.call(signTestFuns[[x]], signTestParams[[x]]))
return(c(signRes, permRes, kurtosis(obsFit$residuals), skewness(obsFit$residuals)))
}