I'm trying to reproduce the following code (nls function does not perform well), but with an extra implementation, using for loops, sprintf and as.formula(), that adds variables depending of the number of peaks in the given spectrum. To be more coherent among peaks, I vectorized the variable names for each peak, so peak number 1 has 'alfa[1]', 'peak[1]' and 'height[1]' related to it.
So far, I got the expected formula:
height[1]/(pi*alfa[1]*(1+((x-peak[1])/alfa[1])^2))+height[2]/(pi*alfa[2]*(1+((x-peak[2])/alfa[2])^2))+drift.a+drift.b*x
Nevertherless, I have some problems when I try to replicate the same system for the par
line. This should show:
par=c(alfa[1]=0.001,
peak[1]=2.156460,
height[1]=1,
alfa[2]=0.001,
peak[2]=2.170150,
height[2]=1,
drift.a=0,
driftb=0)
But instead, when I collapse all strings and used the as.formula
command afterwards, I got:
Error en parse(text = x) : <text>:1:15: unexpected '='
1: par=c( alfa[1]=
^
If I print the collapsed string, the character line is the one expected, so I'm thinking that it will be somehow linked to the as.formula command (i.e. it may not be the appropiated command)