# basic commands for creating a matrix m1 = matrix(data = seq(1:9), nrow = 3, ncol = 3, byrow = FALSE) m1 m2 = matrix(data = c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3, byrow = TRUE) m2 a = c(1, 2, 3) b = c(4, 5, 6) df = data.frame(a, b) df m3 = as.matrix(df) m3 # basic ways to manipulate a matrix (transpose, inverse, multiplication) m3.t = t(m3) m3 m3.t m4 = m3.t %*% m3 m4 m4.inv = solve(m4) m4.inv m5 = m4.inv %*% m4 m5 m6 = m5 %*% m4 m6 # load MLR.RData and MLRScript.R and examine load("MLR.RData") source("MLRScript.R") head(metals) pure head(samples) standards # use absorbance values and concentrations for standards to predict eb findeb(pure, standards) eb # use predicted eb values and absorbance values for samples to predict concentrations findconc(samples, eb) head(pred.conc) # compare predicted and actual concentrations and evaluate error diff = pred.conc - real.conc head(diff) help(apply) means = apply(diff, 2, mean) means sds = apply(diff, 2, sd) sds conf.int = abs(qt(0.05/2, 23)) * sds conf.int diff$cobalt[abs(diff$cobalt) > conf.int[1]] diff$nickel[abs(diff$nickel) > conf.int[2]] diff$copper[abs(diff$copper) > conf.int[3]] diff # examine results for nickel class(real.conc) class(pred.conc) predict = data.frame(pred.conc) plot(real.conc$nickel, predict$nickel, pch = 19, col = "blue", xlab = "actual concentration of Ni", ylab = "predicted concentration of Ni" ) ni.lm = lm(predict$nickel ~ real.conc$nickel) summary(ni.lm) abline(ni.lm, col = "blue", lty = 2) plot(real.conc$nickel, resid(ni.lm), pch = 19, col = "blue", ylim = c(-1.2e-3, 1.2e-3)) abline(h = means[2], col = "red", lty = 2) abline(h = means[2] + conf.int[2], col = "red", lty = 2) abline(h = means[2] - conf.int[2], col = "red", lty = 2) identify(real.conc$nickel, resid(ni.lm), labels = c(2:7, 9:24, 26, 27))