annotate tools/stats/lda_analy.xml @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 <tool id="lda_analy1" name="Perform LDA" version="1.0.1">
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 <description>Linear Discriminant Analysis</description>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 <command interpreter="sh">r_wrapper.sh $script_file</command>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 <inputs>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 <param format="tabular" name="input" type="data" label="Source file"/>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 <param name="cond" size="30" type="integer" value="3" label="Number of principal components" help="See TIP below">
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 <validator type="empty_field" message="Enter a valid number of principal components, see syntax below for examples"/>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 </param>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 </inputs>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 <outputs>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 <data format="txt" name="output" />
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 </outputs>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 <tests>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 <test>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 <param name="input" value="matrix_generator_for_pc_and_lda_output.tabular"/>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 <output name="output" file="lda_analy_output.txt"/>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 <param name="cond" value="2"/>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 </test>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 </tests>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 <configfiles>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 <configfile name="script_file">
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 rm(list = objects() )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 ############# FORMAT X DATA #########################
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 format&lt;-function(data) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 ind=NULL
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 for(i in 1 : ncol(data)){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 if (is.na(data[nrow(data),i])) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 ind&lt;-c(ind,i)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 #print(is.null(ind))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 if (!is.null(ind)) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 data&lt;-data[,-c(ind)]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 data
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 ########GET RESPONSES ###############################
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 get_resp&lt;- function(data) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 resp1&lt;-as.vector(data[,ncol(data)])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 resp=numeric(length(resp1))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 for (i in 1:length(resp1)) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 if (resp1[i]=="Y ") {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 resp[i] = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 if (resp1[i]=="X ") {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 resp[i] = 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 return(resp)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 ######## CHARS TO NUMBERS ###########################
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 f_to_numbers&lt;- function(F) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 ind&lt;-NULL
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 G&lt;-matrix(0,nrow(F), ncol(F))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 for (i in 1:nrow(F)) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 for (j in 1:ncol(F)) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 G[i,j]&lt;-as.integer(F[i,j])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 return(G)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 ###################NORMALIZING#########################
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 norm &lt;- function(M, a=NULL, b=NULL) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 C&lt;-NULL
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 ind&lt;-NULL
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 for (i in 1: ncol(M)) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 if (sd(M[,i])!=0) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 M[,i]&lt;-(M[,i]-mean(M[,i]))/sd(M[,i])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 # else {print(mean(M[,i]))}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 return(M)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 ##### LDA DIRECTIONS #################################
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 lda_dec &lt;- function(data, k){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 priors=numeric(k)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 grandmean&lt;-numeric(ncol(data)-1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 means=matrix(0,k,ncol(data)-1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 B = matrix(0, ncol(data)-1, ncol(data)-1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 N=nrow(data)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 for (i in 1:k){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 priors[i]=sum(data[,1]==i)/N
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 grp=subset(data,data\$group==i)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 means[i,]=mean(grp[,2:ncol(data)])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 #print(means[i,])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 #print(priors[i])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 #print(priors[i]*means[i,])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 grandmean = priors[i]*means[i,] + grandmean
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 for (i in 1:k) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 B= B + priors[i]*((means[i,]-grandmean)%*%t(means[i,]-grandmean))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 W = var(data[,2:ncol(data)])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 svdW = svd(W)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 inv_sqrtW =solve(svdW\$v %*% diag(sqrt(svdW\$d)) %*% t(svdW\$v))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 B_star= t(inv_sqrtW)%*%B%*%inv_sqrtW
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 B_star_decomp = svd(B_star)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 directions = inv_sqrtW%*%B_star_decomp\$v
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 return( list(directions, B_star_decomp\$d) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 ################ NAIVE BAYES FOR 1D SIR OR LDA ##############
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 naive_bayes_classifier &lt;- function(resp, tr_data, test_data, k=2, tau) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 tr_data=data.frame(resp=resp, dir=tr_data)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 means=numeric(k)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 #print(k)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 cl=numeric(k)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 predclass=numeric(length(test_data))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 for (i in 1:k) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 grp = subset(tr_data, resp==i)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 means[i] = mean(grp\$dir)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 #print(i, means[i])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 cutoff = tau*means[1]+(1-tau)*means[2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 #print(tau)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 #print(means)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131 #print(cutoff)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 if (cutoff&gt;means[1]) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 cl[1]=1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 cl[2]=2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136 else {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137 cl[1]=2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 cl[2]=1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
141 for (i in 1:length(test_data)) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
142
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
143 if (test_data[i] &lt;= cutoff) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
144 predclass[i] = cl[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
145 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
146 else {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
147 predclass[i] = cl[2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
148 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
149 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
150 #print(means)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
151 #print(mean(means))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
152 #X11()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
153 #plot(test_data,pch=predclass, col=resp)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
154 predclass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
155 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
156
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
157 ################# EXTENDED ERROR RATES #################
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
158 ext_error_rate &lt;- function(predclass, actualclass,msg=c("you forgot the message"), pr=1) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
159 er=sum(predclass != actualclass)/length(predclass)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
160
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
161 matr&lt;-data.frame(predclass=predclass,actualclass=actualclass)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
162 escapes = subset(matr, actualclass==1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
163 subjects = subset(matr, actualclass==2)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
164 er_esc=sum(escapes\$predclass != escapes\$actualclass)/length(escapes\$predclass)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
165 er_subj=sum(subjects\$predclass != subjects\$actualclass)/length(subjects\$predclass)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
166
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
167 if (pr==1) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
168 # print(paste(c(msg, 'overall : ', (1-er)*100, "%."),collapse=" "))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
169 # print(paste(c(msg, 'within escapes : ', (1-er_esc)*100, "%."),collapse=" "))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
170 # print(paste(c(msg, 'within subjects: ', (1-er_subj)*100, "%."),collapse=" "))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
171 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
172 return(c((1-er)*100, (1-er_esc)*100, (1-er_subj)*100))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
173 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
174
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
175 ## Main Function ##
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
176
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
177 files&lt;-matrix("${input}", 1,1, byrow=T)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
178
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
179 d&lt;-"${cond}" # Number of PC
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
180
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
181 tau&lt;-seq(0,1, by=0.005)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
182 #tau&lt;-seq(0,1, by=0.1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
183 for_curve=matrix(-10, 3,length(tau))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
184
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
185 ##############################################################
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
186
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
187 test_data_whole_X &lt;-read.delim(files[1,1], row.names=1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
188
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
189 #### FORMAT TRAINING DATA ####################################
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
190 # get only necessary columns
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
191
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
192 test_data_whole_X&lt;-format(test_data_whole_X)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
193 oligo_labels&lt;-test_data_whole_X[1:(nrow(test_data_whole_X)-1),ncol(test_data_whole_X)]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
194 test_data_whole_X&lt;-test_data_whole_X[,1:(ncol(test_data_whole_X)-1)]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
195
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
196 X_names&lt;-colnames(test_data_whole_X)[1:ncol(test_data_whole_X)]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
197 test_data_whole_X&lt;-t(test_data_whole_X)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
198 resp&lt;-get_resp(test_data_whole_X)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
199 ldaqda_resp = resp + 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
200 a&lt;-sum(resp) # Number of Subject
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
201 b&lt;-length(resp) - a # Number of Escape
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
202 ## FREQUENCIES #################################################
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
203 F&lt;-test_data_whole_X[,1:(ncol(test_data_whole_X)-1)]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
204 F&lt;-f_to_numbers(F)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
205 FN&lt;-norm(F, a, b)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
206 ss&lt;-svd(FN)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
207 eigvar&lt;-NULL
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
208 eig&lt;-ss\$d^2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
209
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
210 for ( i in 1:length(ss\$d)) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
211 eigvar[i]&lt;-sum(eig[1:i])/sum(eig)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
212 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
213
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
214 #print(paste(c("Variance explained : ", eigvar[d]*100, "%"), collapse=""))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
215
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
216 Z&lt;-F%*%ss\$v
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
217
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
218 ldaqda_data &lt;- data.frame(group=ldaqda_resp,Z[,1:d])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
219 lda_dir&lt;-lda_dec(ldaqda_data,2)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
220 train_lda_pred &lt;-Z[,1:d]%*%lda_dir[[1]]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
221
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
222 ############# NAIVE BAYES CROSS-VALIDATION #############
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
223 ### LDA #####
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
224
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
225 y&lt;-ldaqda_resp
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
226 X&lt;-F
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
227 cv&lt;-matrix(c(rep('NA',nrow(test_data_whole_X))), nrow(test_data_whole_X), length(tau))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
228 for (i in 1:nrow(test_data_whole_X)) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
229 # print(i)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
230 resp&lt;-y[-i]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
231 p&lt;-matrix(X[-i,], dim(X)[1]-1, dim(X)[2])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
232 testdata&lt;-matrix(X[i,],1,dim(X)[2])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
233 p1&lt;-norm(p)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
234 sss&lt;-svd(p1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
235 pred&lt;-(p%*%sss\$v)[,1:d]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
236 test&lt;- (testdata%*%sss\$v)[,1:d]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
237 lda &lt;- lda_dec(data.frame(group=resp,pred),2)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
238 pred &lt;- pred[,1:d]%*%lda[[1]][,1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
239 test &lt;- test%*%lda[[1]][,1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
240 test&lt;-matrix(test, 1, length(test))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
241 for (t in 1:length(tau)) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
242 cv[i, t] &lt;- naive_bayes_classifier (resp, pred, test,k=2, tau[t])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
243 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
244 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
245
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
246 for (t in 1:length(tau)) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
247 tr_err&lt;-ext_error_rate(cv[,t], ldaqda_resp , c("CV"), 1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
248 for_curve[1:3,t]&lt;-tr_err
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
249 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
250
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
251 dput(for_curve, file="${output}")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
252
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
253
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
254 </configfile>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
255 </configfiles>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
256
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
257 <help>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
258
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
259 .. class:: infomark
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
260
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
261 **TIP:** If you want to perform Principal Component Analysis (PCA) on the give numeric input data (which corresponds to the "Source file First in "Generate A Matrix" tool), please use *Multivariate Analysis/Principal Component Analysis*
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
262
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
263 -----
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
264
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
265 .. class:: infomark
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
266
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
267 **What it does**
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
268
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
269 This tool consists of the module to perform the Linear Discriminant Analysis as described in Carrel et al., 2006 (PMID: 17009873)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
270
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
271 *Carrel L, Park C, Tyekucheva S, Dunn J, Chiaromonte F, et al. (2006) Genomic Environment Predicts Expression Patterns on the Human Inactive X Chromosome. PLoS Genet 2(9): e151. doi:10.1371/journal.pgen.0020151*
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
272
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
273 -----
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
274
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
275 .. class:: warningmark
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
276
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
277 **Note**
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
278
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
279 - Output from "Generate A Matrix" tool is used as input file for this tool
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
280 - Output of this tool contains LDA classification success rates for different values of the turning parameter tau (from 0 to 1 with 0.005 interval). This output file will be used to establish the ROC plot, and you can obtain more detail information from this plot.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
281
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
282
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
283 </help>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
284
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
285 </tool>