Skip to content

Instantly share code, notes, and snippets.

@jnpaulson
Created November 23, 2014 22:20
Show Gist options
  • Save jnpaulson/9cfe56f091d96dd27521 to your computer and use it in GitHub Desktop.
Save jnpaulson/9cfe56f091d96dd27521 to your computer and use it in GitHub Desktop.
predict patient from k mds coordinates and accuracy
j = unlist(locals[2:3])
obj2 = obj[,j]
sampleID2 = sampleID[j]
mat = MRcounts(obj2,norm=TRUE,log=TRUE)
otusToKeep <- which(rowSums(mat)>0)
otuVars<-rowSds(mat[otusToKeep,])
otuIndices<-otusToKeep[order(otuVars,decreasing=TRUE)[1:1000]]
mat <- mat[otuIndices,]
mat = t(mat)
d <- dist(mat)
ord = cmdscale(d,k=10)
cl = factor(as.character(sampleID2))
# Arbitrarily chose 10 cooridnates
x = lda(ord[,1:10],grouping=cl,CV=TRUE)
x = x$post # posterior probs
kk = which(is.na(rowSums(x)))
x = x[-kk,]
cl = cl[-kk]
pred = sapply(1:nrow(x),function(i){
names(which(x[i,]==rowMax(x)[i]))
})
combo = cbind(truth = as.character(cl),pred)
sum(combo[,1]==combo[,2])/nrow(combo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment