0

I'm wondering if this is a bug.

I have the following piece of code:

h2 <- hist(c(rep(65, times=5), rep(25, times=5), rep(35, times=10), rep(45, times=4)))
model2 = nls(formula = log(counts[1:5]) ~a+log(mids[1:5])*gamma, start=list(gamma=-3,a=10),data=h2)

it breaks with the error:

Error in parse(text = x) : <text>:2:0: unexpected end of input
1: ~
  ^

But if I do:

h2 <- hist(c(rep(65, times=5), rep(25, times=5), rep(35, times=10), rep(45, times=4)))
model2 = nls(formula = log(counts[1:5]) ~a+log(breaks[1:5])*gamma, start=list(gamma=-3,a=10),data=h2)

it doesn't give the error (it cannot fit this particular data, but can fit the data I really have).

The thing is, for the work I'm doing, I need the mid of the histogram intervals, not the breaks.

EDIT: After the error, traceback is:

7: parse(text = x)
6: eval(parse(text = x)[[1L]])
5: formula(eval(parse(text = x)[[1L]]))
4: formula.character(object, env = baseenv())
3: formula(object, env = baseenv())
2: as.formula(paste("~", paste(varNames[varIndex], collapse = "+")), 
       env = environment(formula))
1: nls(formula = log(counts[1:5]) ~ a + log(mids[1:5]) * gamma, 
       start = list(gamma = -3, a = 10), data = h2)
jbssm
  • 6,861
  • 13
  • 54
  • 81
  • You sometimes use `h` and sometimes use `h2` in your code. Can you please clean that up so it makes sense...? – joran Jan 16 '13 at 18:56
  • @joran It's done now. I was using this from my code, thanks. The problem remains the same. – jbssm Jan 16 '13 at 19:00
  • What does `traceback` provide as output just after your error? In addition, I very much doubt this is a bug in R. `hist` is used daily by thousands of people, more likely you made a mistake yourself. – Paul Hiemstra Jan 16 '13 at 19:00
  • @PaulHiemstra I added the traceback now (I think it's this you asked, if not please let me know). I believe thousands of people use the his function everyday... I'm quite sceptic about how small is fraction that uses the kind of data I'm trying to get from it tough. – jbssm Jan 16 '13 at 19:21
  • 1
    Yeah, as I suspected, I think the problem is that your "formula" is really a horrible, horrible abuse of formulas. Put the data you want in a separate data frame and use a _real_ formula without all that crazy evaluation and subsetting. – joran Jan 16 '13 at 19:28
  • @PaulHiemstra I think I added the right traceback now... sorry, I'm new with R. – jbssm Jan 16 '13 at 19:29

1 Answers1

2

As I suspected:

d <- data.frame(counts = h2$counts[1:5],mids = h2$mids[1:5])
model2 = nls(formula = log(counts) ~a+log(mids)*gamma, start=list(gamma=-3,a=10),data=d)

runs without the formula parsing error (but of course still won't fit this small ill-formed data set).

joran
  • 169,992
  • 32
  • 429
  • 468
  • Thanks I did something similar after your comment and this works. The parser R uses is quite picky though. – jbssm Jan 16 '13 at 19:44
  • 1
    @jbssm "The parser R uses is quite picky though." I see no evidence for that claim. – joran Jan 16 '13 at 19:45
  • @joran Well, you just saw it here. 1st it picks one set of values (h2$counts and h2$breaks) but it decided it doesn't like h2$mids, 2nd it receives a set of data from a data.frame perfectly identified and cannot figure out what it is. I would call that picky... or worst, inconsistent. – jbssm Jan 16 '13 at 22:33
  • @joran Not really, cause if you try the example with: `model2 = nls(formula = log(counts[1:5]) ~a+log(mids[1:5])*gamma, start=list(gamma=-3,a=10),data=h2)` it still breaks with the same error, even not including the $ you talk about and without any reason to think h2 is the name of the variable in my formula, and that doesn't make sense since I'm clearly specifying the dataset and the values it should use. It's inconsistent. – jbssm Jan 16 '13 at 23:26
  • @joran Why for numerical reasons? The error message is exactly the same, as are the traceback() results. – jbssm Jan 16 '13 at 23:36
  • @jbssm There can be many different causes for the same error message. Particularly errors involving an eventual failure of a matrix decomp, which is what is happening here. – joran Jan 16 '13 at 23:42
  • @joran No, but this error is still a parsing error, not a problem inside the nls function as you can see if you run the example. R parser is inconsistent in this case and you can see that clearly. In any case your workaround solves the problem, thank you. – jbssm Jan 16 '13 at 23:45
  • @joran It's not me saying that makes it true, it's the output from traceback() that does. – jbssm Jan 17 '13 at 19:03