Thursday, March 20, 2014

Function for using RM80 to detect/compare CSV data


RM80 <- function() {
require(plyr)
# RMF Media/ RMF Network Security 7:00 PM 3/20/2014. Tested on R 3.03
# Takes three CSV samples (One Control and Two Samples) from Aware Electronics RM-80 GM Counter.
# Configured to read data from "1 TBU per line" for any Time Base Unit
# Samples must have headers repleace with "Time" and "MicroRads_HR" like this:
#       Time MicroRads_HR
#1  41715.85        10.17
#2  41715.85        15.25
# ...


#s <- read.csv("RIMP_03.17s.2014.csv")
#b <- read.csv("RIMP_03.17b.2014.csv")
#c <- read.csv("RIMP_03.17c.2014.csv")
#TBU <- 1
#print("Time Base Unit is 1 seconds.")

s <- read.csv("RIMP_03.17s1.csv")
b <- read.csv("RIMP_03.17b1.csv")
c <- read.csv("RIMP_03.17c1.csv")
TBU <- 10
print("Time Base Unit is 10 seconds.")

cols <- as.data.frame(rbind(nrow(subset(s, select = MicroRads_HR)),
nrow(subset(b, select = MicroRads_HR)),
nrow(subset(c, select = MicroRads_HR))))

colZero <- as.data.frame(rbind(nrow(subset(s, MicroRads_HR ==0.00, select = MicroRads_HR)),
nrow(subset(b, MicroRads_HR ==0.00, select = MicroRads_HR)),
nrow(subset(c, MicroRads_HR ==0.00, select = MicroRads_HR))))

colmin <-  as.data.frame(rbind(min(subset(s, MicroRads_HR !=0.00, select = MicroRads_HR)),
min(subset(b, MicroRads_HR !=0.00, select = MicroRads_HR)),
min(subset(c, MicroRads_HR !=0.00, select = MicroRads_HR))))

colmax <-  as.data.frame(rbind( max(s$MicroRads_HR),max(b$MicroRads_HR), max(c$MicroRads_HR)))
colmean <-  as.data.frame(rbind( mean(s$MicroRads_HR),mean(b$MicroRads_HR), mean(c$MicroRads_HR)))
colsd <-  as.data.frame(rbind(sd(s$MicroRads_HR),sd(b$MicroRads_HR), sd(c$MicroRads_HR)))
colstats <- as.data.frame(c("Samples"=cols,"SampleEqZero"=colZero,"MinNot0"=colmin,"max"=colmax,"mean"=colmean,"sd"=colsd),check.names=TRUE,row.names=c("Control","Sample1","Sample2"))

print("Sample Statistics:")
print(colstats)

# Not Useful Enough 6:27 PM 3/20/2014
# print("Sending Four Graphs of Sample Statistics")
# barplot(colstats$MinNot0.V1, names.arg=c("Control", "Sample1", "Sample2"),horiz=TRUE,,xlab="Min != 0")
# barplot(colstats$max.V1, names.arg=c("Control", "Sample1", "Sample2"),horiz=TRUE,,xlab="Max Averaged MicroRads/HR per TBU")
# barplot(colstats$mean.V1, names.arg=c("Control", "Sample1", "Sample2"),horiz=TRUE,,xlab="Mean Averaged MicroRads/HR per TBU")
# barplot(colstats$sd.V1, names.arg=c("Control", "Sample1", "Sample2"),horiz=TRUE,,xlab="StDev Averaged MicroRads/HR per TBU")

cat("\n")
print("Matrix Information for Non Zero Data")
print("Control")
print(count(subset(s, MicroRads_HR !=0.00, select = MicroRads_HR)))
smat <- as.matrix(count(subset(s, MicroRads_HR !=0.00, select = MicroRads_HR)))
smatt <- (smat[1:nrow(smat),1]) %*% (smat[1:nrow(smat),2])

print("Sample1")
print(count(subset(b, MicroRads_HR !=0.00, select = MicroRads_HR)))
bmat <- as.matrix(count(subset(b, MicroRads_HR !=0.00, select = MicroRads_HR)))
bmatt <- (bmat[1:nrow(bmat),1]) %*% (bmat[1:nrow(bmat),2])

print("Sample2")
print(count(subset(c, MicroRads_HR !=0.00, select = MicroRads_HR)))
cmat <- as.matrix(count(subset(c, MicroRads_HR !=0.00, select = MicroRads_HR)))
cmatt <- (cmat[1:nrow(cmat),1]) %*% (cmat[1:nrow(cmat),2])

cat("\n")
print("Sending Three time-based Graphs of MicroRads_HR Averages Non Zero Data")
barplot(s$MicroRads_HR,xlab="Control", ylab="Averaged MicroRads/HR per TBU",legend.text=c(rev(smat[,1])))
barplot(b$MicroRads_HR,xlab="Sample 1",ylab="Averaged MicroRads/HR per TBU",legend.text=c(rev(bmat[,1])))
barplot(c$MicroRads_HR,xlab="Sample 2",ylab="Averaged MicroRads/HR per TBU",legend.text=c(rev(cmat[,1])))

print("Sending Three frequency-based Graphs of MicroRads_HR Averages for Non Zero Data")
barplot(as.matrix(count(subset(s, MicroRads_HR !=0.00, select = MicroRads_HR))),legend.text=c(smat[,1]),xlab="Stacked as per Legend", ylab="Control")
barplot(as.matrix(count(subset(b, MicroRads_HR !=0.00, select = MicroRads_HR))),legend.text=c(bmat[,1]),xlab="Stacked as per Legend", ylab="Sample 1")
barplot(as.matrix(count(subset(c, MicroRads_HR !=0.00, select = MicroRads_HR))),legend.text=c(cmat[,1]),xlab="Stacked as per Legend",  ylab="Sample 2")

cat("\n")
matstats <- data.frame("MR/HR.Mtrx.Mult"=rbind(smatt,bmatt,cmatt),check.names=TRUE,row.names=c("Control","Sample1","Sample2"))
print("(Matrix) Sums of row based multiplication for Samples.")
print(matstats)
}


No comments:

Post a Comment