-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScript.r
194 lines (128 loc) · 4.61 KB
/
Script.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
#imports
require(MASS)
#Datei einlesen
data = read.csv2("arab.csv", sep=",")
#data = read.csv2("normed_arab.csv", sep=";")
"
verallgemeinertes lineares Modell, p-values
- init Einflussgroessen treatment, time als data frame designmatrix
- init p-value-Vektor
"
treatment = c(rep(0,3), rep(1,3))
time = c(rep(1:3,2))
designmatrix = data.frame(treatment, time)
pvals = c(rep(0, 16529-4))
"
- iteriere ueber alle Gene
- erstelle data frame mit designmatrix und readcounts des aktuellen Gens
- currentglm: verallgemeinertes lineares Modell mit glm.nb fuer aktuelles Gen
"
for (i in 4:length(data[1,]))
{
currentcounts = data[i]
currentvals = data.frame(designmatrix, currentcounts)
colnames(currentvals)[3] = "counts"
currentglm = glm(counts ~ 1 + treatment + as.factor(time), data = currentvals, family=poisson)
pvalue = summary(aov(currentglm))[[1]][2,5]
pvals[i-3] = pvalue
}
"
- Einschub Residuenplots für ausgewählte Gene
"
currentvals = data.frame(designmatrix, data[1243])
currentvals
colnames(currentvals)[3] = "counts"
nbmodel = glm.nb(counts ~ 1 + treatment + as.factor(time), data = currentvals)
poissmodel = glm(counts ~ 1 + treatment + as.factor(time), data = currentvals, family=poisson)
p_res <- resid(poissmodel)
nb_res = resid(nbmodel)
plot(fitted(poissmodel), p_res, col='steelblue', pch=16,
xlab='Predicted Offers', ylab='Standardized Residuals', main='Poisson')
plot(fitted(nbmodel), nb_res, col='steelblue', pch=16,
xlab='Predicted Offers', ylab='Standardized Residuals', main='Negative Binomial')
"
Statistische Tests
- initialisiere DEgenes und NDEgenes
- iteriere über alle p-values
- sortiere alle groesser 0.05 aus
- fuer alle kleiner 0.05 berechne fold change
- falls fold change größer 2 oder kleiner 0.5, Gen ist DE Gen, sonst NDE Gen
"
DEgenes = data.frame(data[1], data[2], data[3])
NDEgenes = data.frame(data[1], data[2], data[3])
for(i in 4:(length(data[1,]))){
if(pvals[i-3] < 0.05){
mean.mock = mean(cbind(data[1, i], data[2, i], data[3, i]))
mean.hrcc = mean(cbind(data[4, i], data[5, i], data[6, i]))
if(mean.mock/mean.hrcc > 2 | mean.mock/mean.hrcc < 0.5){
DEgenes = cbind(DEgenes, data[i])
}else{
NDEgenes = cbind(NDEgenes, data[i])
}
} else{
NDEgenes = cbind(NDEgenes, data[i])
}
}
#Histogramm für die p-values
hist(pvals,breaks=seq(from=0, to=1, by=0.05),main="Histogram of p-values",xlab="p-values",ylim=c(0,3000))
"
Simulation
- init Vektoren für fitted values
"
NDEmock = rep(0, length(NDEgenes)-4)
NDEhrcc = rep(0, length(NDEgenes)-4)
"
- iteriere über NDE Gene
- berechne glm(...,family=poisson) ohne zeitlichen Aspekt
- speichere fitted values für mock und hrcc in Vektoren (sind jetzt gleich für alle mock/hrcc)
"
for (i in 4:length(NDEgenes)) {
currentvals = data.frame(designmatrix, NDEgenes[i])
colnames(currentvals)[3] = "counts"
currentglm = glm(counts ~ 1 + treatment, data = currentvals,family=poisson)
NDEmock[i-3] = fitted(currentglm)[1]
NDEhrcc[i-3] = fitted(currentglm)[4]
}
#berechne mean der fitted value Vektoren
NDEmean.mock = mean(NDEmock)
NDEmean.hrcc = mean(NDEhrcc)
#berechne daraus ein mean
NDEmean = mean(cbind(NDEmean.hrcc, NDEmean.mock))
"
Simuliere Daten fuer NDE Gene
- init leerer Vektor NDEsim.values
- erzeuge für alle NDE Gene poissonverteilte Zufallswerte
- fuer alle NDE Gene selber Erwartungswert NDEmean
- cbind() an NDEsim.values
- konvertiere Vektor zu data frame
"
NDEsim.values = c()
for (i in 4:length(NDEgenes)){
randpois = rpois(1000, NDEmean)
NDEsim.values = cbind(NDEsim.values, randpois)
colnames(NDEsim.values)[i-3] = colnames(NDEgenes[i])
}
NDEsim.values = data.frame(NDEsim.values)
"
Simuliere Daten für DE Gene
- init leerer Vektor
- berechne fuer alle DE Gene Mittelwerte mit altem Modell
- berechne damit fuer jedes Gen mit rpois() Zufallswerte
- cbind() an DEsim.values
- konvertiere DEsim.values zu data frame
"
DEsim.values = c()
for (i in 4:length(DEgenes)) {
currentvals = data.frame(designmatrix, DEgenes[i])
colnames(currentvals)[3] = "counts"
currentglm = glm(counts ~ 1 + treatment + as.factor(time), data = currentvals,family=poisson)
DEmean.mock = mean(fitted(currentglm)[1:3])
DEmean.hrcc = mean(fitted(currentglm)[4:6])
randpois.mock = rpois(500, DEmean.mock)
randpois.hrcc = rpois(500, DEmean.hrcc)
DEsim.values = cbind(DEsim.values, c(randpois.mock, randpois.hrcc))
colnames(DEsim.values)[i-3] = colnames(DEgenes[i])
}
DEsim.values = data.frame(DEsim.values)
#kombiniere NDE und DE simulierte Werte
sim.values = cbind(NDEsim.values, DEsim.values)