[R] loops

Brockagh john.marrinan at gmail.com
Fri Mar 23 09:35:47 CET 2012


Hi

I'm running QDA on some data and calculating the discriminant function. 
qda.res <- qda(type ~ npreg + glu + bp + skin + bmi + ped + age)
ind_yes <- c(1:N)[type == "Yes"]  
> ind_no <- c(1:N)[type == "No"]   
> cov_yes <- cov(table[ind_yes, 1:7] )
> cov_no <- cov(table[ind_no, 1:7] )
> covar<-list(cov_no, cov_yes) 
qdf<- function(x, prior, mu, covar)
+ {x<- matrix(as.numeric(A[i,]), ncol=1)
+ log(prior) -
(0.5%*%log(det(covar)))-0.5%*%(t(x-mu))%*%solve(covar)%*%(x-mu) } 


dfs<-rep(0,5)
for (i in 1:N) {
+ {dfs[i]<-qdf(A[i,], qda.res$prior[1], qda.res$mean[1,], covar[[1]])}
+ } }
A<- matrix(c(2, 88, 58, 26, 28.4, 0.766, 22, 9, 170, 74, 31, 44.0, 0.403,
43, 10, 101, 76, 48, 32.9, 0.171, 63, 5, 121, 72, 23, 26.2, 0.245, 30, 1,
93, 70, 31, 30.4, 0.315, 23), nrow=5, ncol=7, byrow=TRUE)


Then I want to apply this model to a new matrix of data, with results and
collect the misclassification rate. I can do the loop with i, but when i try
to add a loop with another value j corresponding to the prior, mean and
covariance, it will not work. Any ideas. I've got all sorts of errors. I
want to do something like this (this version won't work, of course)

dfs<-rep(0,5)
for (i in 1:N) {
for (j in 1:G) {
+ {dfs[i]<-qdf(A[i,], qda.res$prior[j], qda.res$mean[j,], covar[[j]])}
+ } }

Sorry if I'm not presenting this in an understandable wan...

Any ideas?

Thanks a million.


--
View this message in context: http://r.789695.n4.nabble.com/loops-tp4498141p4498141.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list