-1

I'm trying to fit some data to an equation and keep running into the same error : Error in nls(y ~ A + (B * exp(1) * ((-(x - K)^2)/(2 * (E)^2))) + (G * : parameters without starting value in 'data': B, K, E, G, H, J

 gausform <- function(x, A, B, K, E, G, H, J) {
  A + (B * exp(1) * ((-(x - K)^2)/(2 * (E)^2))) + (G * exp(1) *((-(x - H)^2)/(2 * (J)^2)))
}

new <- data.frame(x,y)
st1 <- list(c(A = min(y), B = mx1, K = cen1, E = width1, G = mx2,  H = cen2,  J = width2))
fit <- nls(y ~ gausform(x, A, B, K, E, G, H, J), data = new, start = st1)

Very new to R, so likely that something is terribly wrong, here is what I'm trying to do: x and y are my data A, B, K, E, G, H and J are values I'm trying to get from the fit mx1, cen1, width1, mx2, cen2 and width2 are all values that I've calculated from my data and want to use as my initial start values.

I'm not sure line(s) I messed up that is making the B through J not starting with what's set as st1.

Any hints or insight is very much appreciated!!

  • 2
    first do you mean to have `exp(-(x-k)^2/(2*E^2))` or you mean to have `-(x-k)^2/(2*E^2)* 2.718282` ?? because you have the second one. Also how can thecomputer distinguish B vs G, K vs H and E vs J?? Are you trying to solve a mixture of normals??? – Onyambu Sep 03 '20 at 02:52
  • Yes, I want to use eulers number in my formula (I'm trying to fit my data which looks like a double gaussian). I know of some code for Matlab that is able to fit the data to the formula, so I think it should be do-able but because I'm not familiar with any coding languages, I can't seem to figure out how to do it! Hope that clarifies! – Tram N. Sep 03 '20 at 03:06
  • 1
    It wont look like gaussian if you are using `euler's number`. Gaussian is `exp(-x^2/2)` not `-x^2/2*exp(1)` – Onyambu Sep 03 '20 at 03:24
  • Ah thank you Onyambu! – Tram N. Sep 03 '20 at 03:26

1 Answers1

1

The particular problem you are having is occurring because you have accidentally constructed a list containing a single vector rather than a list of parameters, i.e. list(c(...)). If you leave out c():

st1 <- list(A = min(y), B = mx1, K = cen1, E = width1,
            G = mx2,  H = cen2,  J = width2)

that will probably allow you to get to the next step - see @Onyambu's comments for additional useful information (i.e., it seems very unlikely that you want to multiply a quadratic term by exp(1) rather than exponentiating a quadratic term ...)

It would be a big help to provide a reproducible example ...

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453