I have working code to accomplish my goal, but as you will see it is not very elegant. I've tried writing it with for loops but my coding knowledge is relatively basic. Could some generous person help me simplify my code and hopefully annotate it so I can learn how to do it myself? I know this is a big ask but I appreciate any help, thanks!
The end goal is to produce a table, to export, that contains A0 and kobs values for each fit (along with their respective Std. errors).
The kobs values are then passed to another equation to obtain three additional parameters. In the provided data the [100] data set was excluded from the final analysis (the exclusion is justified by external factors). I am new here so if I am formatting something wrong or violating some standards please let me know and I will fix it.
Data (copied from a .csv file):
Time,[0]1,[0]2,[0]3,[1],[2.5],[6],[16],[40],[100]
0,1.008,,0.963,1.038,0.979,0.973,0.973,0.906,0.979
0,0.992,1.000,1.037,0.962,1.021,1.027,1.027,1.094,1.021
5,0.813,0.968,0.961,0.704,0.667,0.470,,,
5,0.861,0.971,0.913,0.713,0.645,0.512,0.353,0.306,0.351
10,0.820,0.868,0.888,0.613,0.407,0.262,,0.158,0.236
10,0.851,0.857,0.890,0.563,0.444,0.250,0.197,0.169,0.275
15,0.778,0.773,0.798,,,,0.154,0.145,0.204
15,0.778,0.752,0.894,0.552,0.308,0.184,0.109,0.146,0.238
20,0.610,0.727,0.806,0.441,0.247,0.180,0.114,0.143,0.269
20,0.747,0.784,0.806,0.426,0.257,0.176,0.138,0.116,0.345
30,,,,0.321,0.164,0.091,0.127,0.100,0.279
30,0.563,0.642,0.633,0.268,0.146,0.082,0.096,0.096,0.275
40,0.556,0.524,0.529,0.286,0.106,0.082,0.037,0.079,0.249
40,0.581,0.485,0.487,0.266,0.119,0.045,,,0.041
## Assign Time (x) data
t <- D1$Time
## Assign Response (y) data
R1 <- D1$`[0]1`
R2 <- D1$`[0]2`
R3 <- D1$`[0]3`
R4 <- D1$`[1]`
R5 <- D1$`[2.5]`
R6 <- D1$`[6]`
R7 <- D1$`[16]`
R8 <- D1$`[40]`
R9 <- D1$`[100]`
## Fit data
F1 <- nls(R1 ~ A1 * exp(-k1 * t), start = list(A1 = 1, k1 = 0.02))
P1 <- summary(F1)$parameters[,1:2]
F2 <- nls(R2 ~ A1 * exp(-k1 * t), start = list(A1 = 1, k1 = 0.02))
P2 <- summary(F2)$parameters[,1:2]
F3 <- nls(R3 ~ A1 * exp(-k1 * t), start = list(A1 = 1, k1 = 0.02))
P3 <- summary(F3)$parameters[,1:2]
F4 <- nls(R4 ~ A1 * exp(-k1 * t), start = list(A1 = 1, k1 = 0.02))
P4 <- summary(F4)$parameters[,1:2]
F5 <- nls(R5 ~ A1 * exp(-k1 * t), start = list(A1 = 1, k1 = 0.02))
P5 <- summary(F5)$parameters[,1:2]
F6 <- nls(R6 ~ A1 * exp(-k1 * t), start = list(A1 = 1, k1 = 0.02))
P6 <- summary(F6)$parameters[,1:2]
F7 <- nls(R7 ~ A1 * exp(-k1 * t), start = list(A1 = 1, k1 = 0.02))
P7 <- summary(F7)$parameters[,1:2]
F8 <- nls(R8 ~ A1 * exp(-k1 * t), start = list(A1 = 1, k1 = 0.02))
P8 <- summary(F8)$parameters[,1:2]
F9 <- nls(R9 ~ A1 * exp(-k1 * t), start = list(A1 = 1, k1 = 0.02))
P9 <- summary(F9)$parameters[,1:2]
## Assemble Table
SS <- c(colnames(D1)[2],colnames(D1)[3],colnames(D1)[4],colnames(D1)[5],colnames(D1)[6],colnames(D1)[7],colnames(D1)[8],colnames(D1)[9],colnames(D1)[10])
A0 <- c(P1[1,1],P2[1,1],P3[1,1],P4[1,1],P5[1,1],P6[1,1],P7[1,1],P8[1,1],P9[1,1])
SEA0 <- c(P1[1,2],P2[1,2],P3[1,2],P4[1,2],P5[1,2],P6[1,2],P7[1,2],P8[1,2],P9[1,2])
kobs <- c(P1[2,1],P2[2,1],P3[2,1],P4[2,1],P5[2,1],P6[2,1],P7[2,1],P8[2,1],P9[2,1])
SEkobs <- c(P1[2,2],P2[2,2],P3[2,2],P4[2,2],P5[2,2],P6[2,2],P7[2,2],P8[2,2],P9[2,2])
ExTab <- cbind(SS, A0, SEA0, kobs, SEkobs)
write_clip(ExTab)
conI <- c(0,0,0,0.5,1.5,4,12,35)
kobsA <- c(P1[2,1],P2[2,1],P3[2,1],P4[2,1],P5[2,1],P6[2,1],P7[2,1],P8[2,1])
kFit <- nls(kobsA ~ k0 + ((kin*conI)/(KI+conI)), start = list(k0 = 0.1, kin = 0.2, KI = 3))
summary(kFit)
My inclination is to use for loops to reduce the repetition, but I am unable to write working ones. I would also like to eliminated the ##Assign Response (y) data
section so that I can apply the code to generic data sets with different concentrations in the column names but using D1[2]
in place of R1
inside the nls function produces an error.