# set up coded factor levelsand pure factor errors codes = c(1, 1, 1, 1, -1, -1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1) responses = codes[-c(1, 9, 17, 25, 33, 41, 49)] errors = c(0.1135, 0.0095, -0.0075, 0.0330, -0.0060, 0.0055, 0.1000) # solve for responses R = matrix(data=responses, nrow=7, ncol=7, byrow=TRUE) E = matrix(data=errors*4 - 0.9899, nrow=7, ncol=1) S = solve(R, E) R1 = 0.9899 R2 = S[1, 1] R3 = S[2, 1] R4 = S[3, 1] R5 = S[4, 1] R6 = S[5, 1] R7 = S[6, 1] R8 = S[7, 1] # build the data with random noise added gap0 = rnorm(100, 0, 0.005) std1 = R1 + rnorm(100, 0, 0.005) gap1 = rnorm(100, 0, 0.005) std2 = R2 + rnorm(100, 0, 0.005) gap2 = rnorm(100, 0, 0.005) std3 = R3 + rnorm(100, 0, 0.005) gap3 = rnorm(100, 0, 0.005) std4 = R4 + rnorm(100, 0, 0.005) gap4 = rnorm(100, 0, 0.005) std5 = R5 + rnorm(100, 0, 0.005) gap5 = rnorm(101, 0, 0.005) std6 = R6 + rnorm(100, 0, 0.005) gap6 = rnorm(101, 0, 0.005) std7 = R7 + rnorm(100, 0, 0.005) gap7 = rnorm(101, 0, 0.005) std8 = R8 + rnorm(100, 0, 0.005) gap8 = rnorm(101, 0, 0.005) signal = c(gap0, std1, gap1, std2, gap2, std3, gap3, std4, gap4, std5, gap5, std6, gap6, std7, gap7, std8, gap8) # plot the data and annotate with responses plot(signal, type = "l", ylim = c(0, 1.5), xlab = "time", ylab = "absorbance of standard sample") r1 = mean(signal[101:200]) r2 = mean(signal[301:400]) r3 = mean(signal[501:600]) r4 = mean(signal[701:800]) r5 = mean(signal[901:1000]) r6 = mean(signal[1101:1200]) r7 = mean(signal[1301:1400]) r8 = mean(signal[1501:1600]) legend(x = "topleft", bty = "n", legend = c(paste0("R1: ", noquote(formatC(r1, format = "f", digits = 4))), paste0("R2: ", noquote(formatC(r2, format = "f", digits = 4))), paste0("R3: ", noquote(formatC(r3, format = "f", digits = 4))), paste0("R4: ", noquote(formatC(r4, format = "f", digits = 4))))) legend(x = "topright", bty = "n", legend = c(paste0("R5: ", noquote(formatC(r5, format = "f", digits = 4))), paste0("R6: ", noquote(formatC(r6, format = "f", digits = 4))), paste0("R7: ", noquote(formatC(r7, format = "f", digits = 4))), paste0("R8: ", noquote(formatC(r8, format = "f", digits = 4))))) C = matrix(data=codes, nrow=8, ncol=7, byrow=FALSE) colnames(C) = c("Factor A", "Factor B", "Factor C", "Factor D", "Factor E", "Factor F", "Factor G") rownames(C) = c("Expt 1", "Expt 2", "Expt 3", "Expt 4", "Expt 5", "Expt 6", "Expt 7", "Expt 8") rugtable = as.table(C) rugtable