-
Notifications
You must be signed in to change notification settings - Fork 0
/
mvpaAnalysisRandom_cross_subEst.R
executable file
·118 lines (89 loc) · 3.65 KB
/
mvpaAnalysisRandom_cross_subEst.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
rm(list=ls(all=TRUE))
setwd("G:/MVPA")
filePathGestalt <- "G:/MVPA/betaFilesGestalt"
filesGestalt <- list.files(filePathGestalt)
pids <- unique(sapply(strsplit(filesGestalt, "_"), function(x) x[[1]][1]))
filesGestalt <- list.files(filePathGestalt, full.names = TRUE)
filePathSubitizing <- "G:/MVPA/betaFilesSubitizing"
filesSubitizing <- list.files(filePathSubitizing, full.names = TRUE)
i <- 1
pid <- pids[2]
acc <- NULL
predDf <- NULL
for (pid in pids){
print(pid)
pidFilesGestalt <- filesGestalt[grep(pid, filesGestalt)]
pidFilesSubitizing <- filesSubitizing[grep(pid, filesSubitizing)]
df1 <- read.table(pidFilesGestalt[1], sep="\t", header=TRUE)
head(df1)
colnames(df1) <- c(paste0("condition_",df1$Var2[1]),
paste0("hemisphere_",df1$Var2[1]),
paste0("trial_",df1$Var2[1]),
paste0("voxel_", df1$Var2[1], "_", 1:(ncol(df1)-3)))
df2 <- NULL
if(length(pidFilesGestalt) == 2){
df2 <- read.table(pidFilesGestalt[2], sep="\t", header=TRUE)
colnames(df2) <- c(paste0("condition_",df2$Var2[1]),
paste0("hemisphere_",df2$Var2[1]),
paste0("trial_",df2$Var2[1]),
paste0("voxel_", df2$Var2[1], "_", 1:(ncol(df2)-3)))
df2 <- df2[order(df2[,3]),]
}
df1 <- df1[order(df1[,3]),]
if(!is.null(df2)){
dfGestalt <- cbind(df1, df2)
} else {
dfGestalt <- df1
}
df1 <- read.table(pidFilesSubitizing[1], sep="\t", header=TRUE)
head(df1)
colnames(df1) <- c(paste0("condition_",df1$Var2[1]),
paste0("hemisphere_",df1$Var2[1]),
paste0("trial_",df1$Var2[1]),
paste0("voxel_", df1$Var2[1], "_", 1:(ncol(df1)-3)))
df2 <- NULL
if(length(pidFilesSubitizing) == 2){
df2 <- read.table(pidFilesSubitizing[2], sep="\t", header=TRUE)
colnames(df2) <- c(paste0("condition_",df2$Var2[1]),
paste0("hemisphere_",df2$Var2[1]),
paste0("trial_",df2$Var2[1]),
paste0("voxel_", df2$Var2[1], "_", 1:(ncol(df2)-3)))
df2 <- df2[order(df2[,3]),]
}
df1 <- df1[order(df1[,3]),]
if(!is.null(df2)){
dfSubitizing<- cbind(df1, df2)
} else {
dfSubitizing <- df1
}
colnames(dfGestalt)
colnames(dfSubitizing)
yGestalt <- ifelse(dfGestalt[,1] == "gestalt", 1, 0) # 1 = gestalt, 0 = no_gestalt
XGestalt <- dfGestalt[,grep("voxel", colnames(dfGestalt))]
num <- as.numeric(sapply(dfSubitizing[,1], function(c) strsplit(as.character(c), "can")[[1]][2]))
ySubitizing <- ifelse(num <= 4, 1, 0) # 1 = sub, 0 = est
XSubitizing <- dfSubitizing[,grep("voxel", colnames(dfSubitizing))]
print(all(colnames(XGestalt) == colnames(XSubitizing)))
XGestalt <- XGestalt[colSums(!is.na(XGestalt)) > 0]
XSubitizing <- XSubitizing[colSums(!is.na(XSubitizing)) > 0]
require(e1071)
colGestalt <- colnames(XGestalt)[colnames(XGestalt) %in% colnames(XSubitizing)]
cols <- colnames(XSubitizing)[colnames(XSubitizing) %in% colGestalt]
XGestalt <- XGestalt[,cols]
XSubitizing <- XSubitizing[,cols]
print(all(colnames(XGestalt) == colnames(XSubitizing)))
XGestalt <- as.matrix(XGestalt)
XSubitizing <- as.matrix(XSubitizing)
yGestalt <- factor(yGestalt)
ySubitizing <- factor(ySubitizing)
pred <- rbinom(nrow(XSubitizing),1,0.5)
#pred <- (predict(xgb, dtest) > 0.5) * 1
acc <- c(acc, mean(ySubitizing == pred))
print(mean(ySubitizing == pred))
predDf <- rbind(predDf,data.frame(pid=rep(pid,length(pred)),pred= (ySubitizing == pred)*1))
}
acc
t.test(acc, mu=0.5)
require(lme4)
glmer1 <- glmer(pred ~ 1 + (1|pid), predDf, family=binomial)
summary(glmer1)